mongoid_slug 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +47 -44
- data/lib/mongoid/slug.rb +5 -2
- data/lib/mongoid/slug/version.rb +1 -1
- data/spec/models/comic_book.rb +3 -0
- data/spec/mongoid/slug_spec.rb +19 -13
- metadata +18 -23
data/README.md
CHANGED
@@ -1,53 +1,56 @@
|
|
1
1
|
Mongoid Slug
|
2
2
|
============
|
3
3
|
|
4
|
-
Mongoid Slug generates a URL slug or permalink based on one or more
|
4
|
+
Mongoid Slug generates a URL slug or permalink based on one or more
|
5
|
+
fields in a Mongoid model.
|
6
|
+
|
7
|
+
It sits idly on top of [stringex](https://github.com/rsl/stringex) and
|
8
|
+
works with non-Latin characters.
|
5
9
|
|
6
10
|
Quick Start
|
7
|
-
|
11
|
+
-----------
|
8
12
|
|
9
|
-
|
13
|
+
Add mongoid_slug to your Gemfile:
|
10
14
|
|
11
15
|
gem 'mongoid_slug', :require => 'mongoid/slug'
|
12
16
|
|
13
|
-
|
17
|
+
Set up slugs in models like this:
|
14
18
|
|
15
19
|
class Book
|
16
20
|
include Mongoid::Document
|
17
21
|
include Mongoid::Slug
|
22
|
+
|
18
23
|
field :title
|
19
|
-
slug :title
|
20
24
|
embeds_many :authors
|
25
|
+
|
26
|
+
slug :title
|
21
27
|
end
|
22
28
|
|
23
29
|
class Author
|
24
30
|
include Mongoid::Document
|
25
31
|
include Mongoid::Slug
|
26
|
-
|
27
|
-
field :
|
28
|
-
|
32
|
+
|
33
|
+
field :first
|
34
|
+
field :last
|
29
35
|
embedded_in :book, :inverse_of => :authors
|
36
|
+
|
37
|
+
slug :first, :last, :as => :name
|
30
38
|
end
|
31
39
|
|
32
|
-
|
40
|
+
Finder
|
41
|
+
------
|
33
42
|
|
34
|
-
|
35
|
-
book.authors.find_by_slug(params[:id])
|
43
|
+
In your controller, throw in some minimal magic:
|
36
44
|
|
37
|
-
|
45
|
+
# GET /books/a-thousand-plateaus/authors/authors/gilles-deleuze
|
46
|
+
author = Book.find_by_slug(params[:book_id]).
|
47
|
+
authors.
|
48
|
+
find_by_name(params[:id])
|
38
49
|
|
39
|
-
|
40
|
-
|
41
|
-
include Mongoid::Slug
|
42
|
-
field :name
|
43
|
-
slug :name, :as => :permalink
|
44
|
-
end
|
50
|
+
Permanence
|
51
|
+
----------
|
45
52
|
|
46
|
-
|
47
|
-
|
48
|
-
Person.find_by_permalink(params[:id])
|
49
|
-
|
50
|
-
To demo some more functionality in the console:
|
53
|
+
By default, slugs are not permanent:
|
51
54
|
|
52
55
|
>> book = Book.create(:title => "A Thousand Plateaus")
|
53
56
|
>> book.to_param
|
@@ -56,47 +59,47 @@ To demo some more functionality in the console:
|
|
56
59
|
>> book.save
|
57
60
|
>> book.to_param
|
58
61
|
"anti-oedipus"
|
59
|
-
>> author = book.authors.create(:first_name => "Gilles", :last_name => "Deleuze")
|
60
|
-
>> author.to_param
|
61
|
-
=> "gilles-deleuze"
|
62
|
-
>> author.update_attributes(:first_name => "Félix", :last_name => "Guattari")
|
63
|
-
>> author.to_param
|
64
|
-
=> "felix-guattari"
|
65
62
|
|
66
|
-
|
67
|
-
|
63
|
+
If you require permanent slugs, pass the `:permanent` option when
|
64
|
+
defining the slug.
|
65
|
+
|
66
|
+
Scope
|
67
|
+
-----
|
68
|
+
|
69
|
+
Embedded objects that are automatically scoped by their parent.
|
68
70
|
|
69
|
-
|
71
|
+
To scope an object by a reference association, pass `:scope`:
|
70
72
|
|
71
73
|
class Company
|
72
74
|
include Mongoid::Document
|
73
|
-
field :name
|
74
75
|
references_many :employees
|
75
76
|
end
|
76
77
|
|
77
78
|
class Employee
|
78
79
|
include Mongoid::Document
|
79
80
|
include Mongoid::Slug
|
80
|
-
field :
|
81
|
-
|
82
|
-
slug :first_name, :last_name, :scope => :company
|
81
|
+
field :name
|
82
|
+
slug :name, :scope => :company
|
83
83
|
referenced_in :company
|
84
84
|
end
|
85
85
|
|
86
|
-
In this example, if you create an employee without associating it with
|
86
|
+
In this example, if you create an employee without associating it with
|
87
|
+
any company, the scope will fall back to the root employees collection.
|
87
88
|
|
88
|
-
|
89
|
+
Currently, if you have an irregular association name, you **must**
|
90
|
+
specify the `:inverse_of` option on the other side of the assocation.
|
89
91
|
|
90
|
-
|
92
|
+
Indexes
|
93
|
+
-------
|
91
94
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
You may optionally pass an `:index` option to generate an index on the slug in top-level objects.
|
95
|
+
You may optionally pass an `:index` option to define an index on top-level
|
96
|
+
slugs.
|
96
97
|
|
97
98
|
class Book
|
98
99
|
field :title
|
99
100
|
slug :title, :index => true
|
100
101
|
end
|
101
102
|
|
102
|
-
Indexes on
|
103
|
+
Indexes on unscoped slugs will be unique.
|
104
|
+
|
105
|
+
This option has no effect if the object is embedded.
|
data/lib/mongoid/slug.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
#encoding: utf-8
|
2
1
|
require 'stringex'
|
3
2
|
|
4
3
|
module Mongoid #:nodoc:
|
@@ -125,7 +124,11 @@ module Mongoid #:nodoc:
|
|
125
124
|
metadata = reflect_on_all_associations(:embedded_in).first
|
126
125
|
_parent.send(metadata.inverse_of)
|
127
126
|
else
|
128
|
-
self.class
|
127
|
+
appropriate_class = self.class
|
128
|
+
while (appropriate_class.superclass.include?(Mongoid::Document))
|
129
|
+
appropriate_class = appropriate_class.superclass
|
130
|
+
end
|
131
|
+
appropriate_class
|
129
132
|
end
|
130
133
|
end
|
131
134
|
end
|
data/lib/mongoid/slug/version.rb
CHANGED
data/spec/mongoid/slug_spec.rb
CHANGED
@@ -159,12 +159,12 @@ module Mongoid
|
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
|
-
context "when
|
162
|
+
context "when :as is passed as an argument" do
|
163
163
|
let!(:person) do
|
164
164
|
Person.create(:name => "John Doe")
|
165
165
|
end
|
166
166
|
|
167
|
-
it "sets
|
167
|
+
it "sets an alternative slug field name" do
|
168
168
|
person.should respond_to(:permalink)
|
169
169
|
person.permalink.should eql "john-doe"
|
170
170
|
end
|
@@ -174,12 +174,12 @@ module Mongoid
|
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
177
|
-
context "when
|
177
|
+
context "when :permanent is passed as an argument" do
|
178
178
|
let(:person) do
|
179
179
|
Person.create(:name => "John Doe")
|
180
180
|
end
|
181
181
|
|
182
|
-
it "does not
|
182
|
+
it "does not update the slug when the slugged fields change" do
|
183
183
|
person.name = "Jane Doe"
|
184
184
|
person.save
|
185
185
|
person.to_param.should eql "john-doe"
|
@@ -251,41 +251,47 @@ module Mongoid
|
|
251
251
|
end
|
252
252
|
end
|
253
253
|
|
254
|
-
context "when :index is
|
254
|
+
context "when :index is passed as an argument" do
|
255
255
|
before do
|
256
256
|
Book.collection.drop_indexes
|
257
257
|
Author.collection.drop_indexes
|
258
258
|
end
|
259
259
|
|
260
|
-
it "
|
260
|
+
it "defines an index on the slug in top-level objects" do
|
261
261
|
Book.create_indexes
|
262
262
|
Book.collection.index_information.should have_key "slug_1"
|
263
263
|
end
|
264
264
|
|
265
265
|
context "when slug is scoped by a reference association" do
|
266
|
-
it "
|
266
|
+
it "defines a non-unique index" do
|
267
267
|
Author.create_indexes
|
268
268
|
Author.index_information["slug_1"]["unique"].should be_false
|
269
269
|
end
|
270
270
|
end
|
271
271
|
|
272
272
|
context "when slug is not scoped by a reference association" do
|
273
|
-
it "
|
273
|
+
it "defines a unique index" do
|
274
274
|
Book.create_indexes
|
275
275
|
Book.index_information["slug_1"]["unique"].should be_true
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
it "
|
280
|
-
pending "Would such an option even make sense?"
|
281
|
-
end
|
279
|
+
it "has no effect in embedded objects"
|
282
280
|
end
|
283
281
|
|
284
|
-
context "when :index is not
|
285
|
-
it "does not index slug" do
|
282
|
+
context "when :index is not passed as an argument" do
|
283
|
+
it "does not define an index on the slug" do
|
286
284
|
Person.create_indexes
|
287
285
|
Person.collection.index_information.should_not have_key "permalink_1"
|
288
286
|
end
|
289
287
|
end
|
288
|
+
|
289
|
+
context "when the object has STI" do
|
290
|
+
it "scopes by the superclass" do
|
291
|
+
book = Book.create(:title => "Anti Oedipus")
|
292
|
+
comic_book = ComicBook.create(:title => "Anti Oedipus")
|
293
|
+
comic_book.slug.should_not eql(book.title)
|
294
|
+
end
|
295
|
+
end
|
290
296
|
end
|
291
297
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_slug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 5
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 6
|
8
|
-
-
|
9
|
-
version: 0.6.
|
9
|
+
- 1
|
10
|
+
version: 0.6.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Paper Cavalier
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-28 00:00:00 +00:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,13 +26,14 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 15424089
|
28
30
|
segments:
|
29
31
|
- 2
|
30
32
|
- 0
|
31
33
|
- 0
|
32
34
|
- rc
|
33
|
-
-
|
34
|
-
version: 2.0.0.rc.
|
35
|
+
- 6
|
36
|
+
version: 2.0.0.rc.6
|
35
37
|
type: :runtime
|
36
38
|
version_requirements: *id001
|
37
39
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +44,7 @@ dependencies:
|
|
42
44
|
requirements:
|
43
45
|
- - ~>
|
44
46
|
- !ruby/object:Gem::Version
|
47
|
+
hash: 31
|
45
48
|
segments:
|
46
49
|
- 1
|
47
50
|
- 2
|
@@ -57,11 +60,12 @@ dependencies:
|
|
57
60
|
requirements:
|
58
61
|
- - ~>
|
59
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 31
|
60
64
|
segments:
|
61
65
|
- 1
|
62
|
-
-
|
63
|
-
-
|
64
|
-
version: 1.
|
66
|
+
- 2
|
67
|
+
- 0
|
68
|
+
version: 1.2.0
|
65
69
|
type: :development
|
66
70
|
version_requirements: *id003
|
67
71
|
- !ruby/object:Gem::Dependency
|
@@ -72,6 +76,7 @@ dependencies:
|
|
72
76
|
requirements:
|
73
77
|
- - ~>
|
74
78
|
- !ruby/object:Gem::Version
|
79
|
+
hash: 7
|
75
80
|
segments:
|
76
81
|
- 0
|
77
82
|
- 6
|
@@ -87,6 +92,7 @@ dependencies:
|
|
87
92
|
requirements:
|
88
93
|
- - ~>
|
89
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 31
|
90
96
|
segments:
|
91
97
|
- 2
|
92
98
|
- 4
|
@@ -94,21 +100,6 @@ dependencies:
|
|
94
100
|
version: 2.4.0
|
95
101
|
type: :development
|
96
102
|
version_requirements: *id005
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: ruby-debug19
|
99
|
-
prerelease: false
|
100
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
|
-
requirements:
|
103
|
-
- - ~>
|
104
|
-
- !ruby/object:Gem::Version
|
105
|
-
segments:
|
106
|
-
- 0
|
107
|
-
- 11
|
108
|
-
- 0
|
109
|
-
version: 0.11.0
|
110
|
-
type: :development
|
111
|
-
version_requirements: *id006
|
112
103
|
description: Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model.
|
113
104
|
email:
|
114
105
|
- code@papercavalier.com
|
@@ -125,6 +116,7 @@ files:
|
|
125
116
|
- README.md
|
126
117
|
- spec/models/author.rb
|
127
118
|
- spec/models/book.rb
|
119
|
+
- spec/models/comic_book.rb
|
128
120
|
- spec/models/partner.rb
|
129
121
|
- spec/models/person.rb
|
130
122
|
- spec/models/relationship.rb
|
@@ -145,6 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
137
|
requirements:
|
146
138
|
- - ">="
|
147
139
|
- !ruby/object:Gem::Version
|
140
|
+
hash: 3
|
148
141
|
segments:
|
149
142
|
- 0
|
150
143
|
version: "0"
|
@@ -153,6 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
146
|
requirements:
|
154
147
|
- - ">="
|
155
148
|
- !ruby/object:Gem::Version
|
149
|
+
hash: 3
|
156
150
|
segments:
|
157
151
|
- 0
|
158
152
|
version: "0"
|
@@ -166,6 +160,7 @@ summary: Generates a URL slug
|
|
166
160
|
test_files:
|
167
161
|
- spec/models/author.rb
|
168
162
|
- spec/models/book.rb
|
163
|
+
- spec/models/comic_book.rb
|
169
164
|
- spec/models/partner.rb
|
170
165
|
- spec/models/person.rb
|
171
166
|
- spec/models/relationship.rb
|