mongoid_slug 0.4.3 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mongoid_slug.rb +17 -8
- data/spec/models/person.rb +1 -0
- data/spec/mongoid_slug_spec.rb +91 -28
- metadata +4 -4
data/lib/mongoid_slug.rb
CHANGED
@@ -12,6 +12,7 @@ module Mongoid::Slug
|
|
12
12
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
13
13
|
self.slug_name = options[:as] || :slug
|
14
14
|
self.slugged_fields = args
|
15
|
+
|
15
16
|
field slug_name
|
16
17
|
index slug_name, :unique => true
|
17
18
|
before_save :generate_slug
|
@@ -19,25 +20,33 @@ module Mongoid::Slug
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def to_param
|
22
|
-
|
23
|
+
self.send slug_name
|
23
24
|
end
|
24
25
|
|
25
26
|
private
|
26
27
|
|
27
|
-
def
|
28
|
+
def find_(slug, stack=[])
|
28
29
|
if embedded?
|
29
|
-
|
30
|
-
_parent.send :
|
30
|
+
stack << association_name
|
31
|
+
_parent.send :find_, slug, stack
|
31
32
|
else
|
32
|
-
|
33
|
-
|
33
|
+
stack.reverse!
|
34
|
+
path = (stack + [slug_name]).join(".")
|
35
|
+
found = collection.find(path => slug).to_a
|
36
|
+
|
37
|
+
stack.each do |name|
|
38
|
+
if found.any?
|
39
|
+
found = found.first.send(name)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
found
|
34
44
|
end
|
35
45
|
end
|
36
46
|
|
37
47
|
def find_unique_slug(suffix='')
|
38
48
|
slug = ("#{slug_base} #{suffix}").parameterize
|
39
|
-
|
40
|
-
if duplicate_of(slug).reject{ |doc| doc.id == self.id }.empty?
|
49
|
+
if find_(slug).to_a.reject{ |doc| doc.id == self.id }.empty?
|
41
50
|
slug
|
42
51
|
else
|
43
52
|
suffix = suffix.blank? ? '1' : "#{suffix.to_i + 1}"
|
data/spec/models/person.rb
CHANGED
data/spec/mongoid_slug_spec.rb
CHANGED
@@ -24,27 +24,27 @@ describe Mongoid::Slug do
|
|
24
24
|
|
25
25
|
it "appends a counter when slug is not unique" do
|
26
26
|
similar_book = Book.create(:title => @book.title)
|
27
|
-
similar_book.
|
27
|
+
similar_book.to_param.should match /\d$/
|
28
28
|
end
|
29
29
|
|
30
30
|
it "does not append a counter when slug is unique" do
|
31
|
-
@book.
|
31
|
+
@book.to_param.should_not match /\d$/
|
32
32
|
end
|
33
33
|
|
34
34
|
it "does not update slug if slugged fields have not changed" do
|
35
|
-
former_slug = @book.
|
35
|
+
former_slug = @book.to_param
|
36
36
|
@book.update_attributes(:isbn => "9785545858118")
|
37
|
-
@book.
|
37
|
+
@book.to_param.should eql former_slug
|
38
38
|
end
|
39
39
|
|
40
40
|
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
41
|
-
former_slug = @book.
|
41
|
+
former_slug = @book.to_param
|
42
42
|
@book.update_attributes(:title => "A thousand plateaus")
|
43
|
-
@book.
|
43
|
+
@book.to_param.should eql former_slug
|
44
44
|
end
|
45
45
|
|
46
46
|
it "finds by slug" do
|
47
|
-
Book.where(:slug => @book.
|
47
|
+
Book.where(:slug => @book.to_param).first.should eql @book
|
48
48
|
end
|
49
49
|
|
50
50
|
end
|
@@ -71,21 +71,27 @@ describe Mongoid::Slug do
|
|
71
71
|
|
72
72
|
it "appends a counter when slug is not unique" do
|
73
73
|
similar_subject = @book.subjects.create(:name => @subject.name)
|
74
|
-
similar_subject.
|
74
|
+
similar_subject.to_param.should match /\d$/
|
75
75
|
end
|
76
76
|
|
77
77
|
it "does not append a counter when slug is unique" do
|
78
|
-
@subject.
|
78
|
+
@subject.to_param.should_not match /\d$/
|
79
79
|
end
|
80
80
|
|
81
|
-
it "does not update slug if slugged
|
82
|
-
former_slug = @subject.
|
81
|
+
it "does not update slug if slugged fields have not changed" do
|
82
|
+
former_slug = @subject.to_param
|
83
83
|
@subject.update_attributes(:description => "Lorem ipsum dolor sit amet")
|
84
|
-
@subject.
|
84
|
+
@subject.to_param.should eql former_slug
|
85
|
+
end
|
86
|
+
|
87
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
88
|
+
former_slug = @subject.to_param
|
89
|
+
@subject.update_attributes(:title => "PSYCHOANALYSIS")
|
90
|
+
@subject.to_param.should eql former_slug
|
85
91
|
end
|
86
92
|
|
87
93
|
it "finds by slug" do
|
88
|
-
@book.subjects.where(:slug => @subject.
|
94
|
+
@book.subjects.where(:slug => @subject.to_param).first.should eql @subject
|
89
95
|
end
|
90
96
|
|
91
97
|
end
|
@@ -105,10 +111,16 @@ describe Mongoid::Slug do
|
|
105
111
|
@publisher.to_param.should eql "Harvard UP".parameterize
|
106
112
|
end
|
107
113
|
|
108
|
-
it "does not update slug if slugged
|
109
|
-
former_slug = @publisher.
|
114
|
+
it "does not update slug if slugged fields have not changed" do
|
115
|
+
former_slug = @publisher.to_param
|
110
116
|
@publisher.update_attributes(:year => 2001)
|
111
|
-
@publisher.
|
117
|
+
@publisher.to_param.should eql former_slug
|
118
|
+
end
|
119
|
+
|
120
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
121
|
+
former_slug = @publisher.to_param
|
122
|
+
@publisher.update_attributes(:name => "oup")
|
123
|
+
@publisher.to_param.should eql former_slug
|
112
124
|
end
|
113
125
|
|
114
126
|
end
|
@@ -136,11 +148,17 @@ describe Mongoid::Slug do
|
|
136
148
|
it "appends a counter when slug is not unique" do
|
137
149
|
similar_author = Author.create(:first_name => @author.first_name,
|
138
150
|
:last_name => @author.last_name)
|
139
|
-
similar_author.
|
151
|
+
similar_author.to_param.should match /\d$/
|
140
152
|
end
|
141
153
|
|
142
154
|
it "does not append a counter when slug is unique" do
|
143
|
-
@author.
|
155
|
+
@author.to_param.should_not match /\d$/
|
156
|
+
end
|
157
|
+
|
158
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
159
|
+
former_slug = @author.to_param
|
160
|
+
@author.update_attributes(:first_name => "gilles", :last_name => "DELEUZE")
|
161
|
+
@author.to_param.should eql former_slug
|
144
162
|
end
|
145
163
|
|
146
164
|
it "finds by slug" do
|
@@ -174,21 +192,27 @@ describe Mongoid::Slug do
|
|
174
192
|
|
175
193
|
it "appends a counter when slug is not unique" do
|
176
194
|
similar_baz = @bar.bazes.create(:name => @baz.name)
|
177
|
-
similar_baz.
|
195
|
+
similar_baz.to_param.should match /\d$/
|
178
196
|
end
|
179
197
|
|
180
198
|
it "does not append a counter when slug is unique" do
|
181
|
-
@baz.
|
199
|
+
@baz.to_param.should_not match /\d$/
|
182
200
|
end
|
183
201
|
|
184
|
-
it "does not update slug if slugged
|
185
|
-
former_slug = @baz.
|
202
|
+
it "does not update slug if slugged fields have not changed" do
|
203
|
+
former_slug = @baz.to_param
|
186
204
|
@baz.update_attributes(:other => "Lorem ipsum dolor sit amet")
|
187
|
-
@baz.
|
205
|
+
@baz.to_param.should eql former_slug
|
206
|
+
end
|
207
|
+
|
208
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
209
|
+
former_slug = @baz.to_param
|
210
|
+
@baz.update_attributes(:name => "BAZ")
|
211
|
+
@baz.to_param.should eql former_slug
|
188
212
|
end
|
189
213
|
|
190
214
|
it "finds by slug" do
|
191
|
-
@bar.bazes.where(:slug => @baz.
|
215
|
+
@bar.bazes.where(:slug => @baz.to_param).first.should eql @baz
|
192
216
|
end
|
193
217
|
|
194
218
|
end
|
@@ -204,9 +228,48 @@ describe Mongoid::Slug do
|
|
204
228
|
@person.send(:permalink).should eql "john-doe"
|
205
229
|
end
|
206
230
|
|
231
|
+
it "generates slug" do
|
232
|
+
@person.to_param.should eql(@person.name.parameterize)
|
233
|
+
end
|
234
|
+
|
235
|
+
it "updates slug" do
|
236
|
+
@person.update_attributes(:name => "Jane Doe")
|
237
|
+
@person.to_param.should eql "Jane Doe".parameterize
|
238
|
+
end
|
239
|
+
|
240
|
+
it "generates a unique slug" do
|
241
|
+
similar_person = Person.create(:name => @person.name)
|
242
|
+
similar_person.to_param.should_not eql @person.to_param
|
243
|
+
end
|
244
|
+
|
245
|
+
it "appends a counter when slug is not unique" do
|
246
|
+
similar_person = Person.create(:name => @person.name)
|
247
|
+
similar_person.to_param.should match /\d$/
|
248
|
+
end
|
249
|
+
|
250
|
+
it "does not append a counter when slug is unique" do
|
251
|
+
@person.to_param.should_not match /\d$/
|
252
|
+
end
|
253
|
+
|
254
|
+
it "does not update slug if slugged fields have not changed" do
|
255
|
+
former_slug = @person.to_param
|
256
|
+
@person.update_attributes(:age => 31)
|
257
|
+
@person.to_param.should eql former_slug
|
258
|
+
end
|
259
|
+
|
260
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
261
|
+
former_slug = @person.to_param
|
262
|
+
@person.update_attributes(:name => "JOHN DOE")
|
263
|
+
@person.to_param.should eql former_slug
|
264
|
+
end
|
265
|
+
|
266
|
+
it "finds by slug" do
|
267
|
+
Person.where(:permalink => @person.to_param).first.should eql @person
|
268
|
+
end
|
269
|
+
|
207
270
|
end
|
208
271
|
|
209
|
-
context "#
|
272
|
+
context "#find_" do
|
210
273
|
|
211
274
|
before(:each) do
|
212
275
|
@foo = Foo.create(:name => "foo")
|
@@ -215,15 +278,15 @@ describe Mongoid::Slug do
|
|
215
278
|
end
|
216
279
|
|
217
280
|
it "finds duplicate slug of a root document" do
|
218
|
-
@foo.send(:
|
281
|
+
@foo.send(:find_, @foo.to_param).count.should eql 1
|
219
282
|
end
|
220
283
|
|
221
284
|
it "finds duplicate slug of an embedded document" do
|
222
|
-
@bar.send(:
|
285
|
+
@bar.send(:find_, @bar.to_param).count.should eql 1
|
223
286
|
end
|
224
287
|
|
225
288
|
it "finds duplicate slug of a deeply-embedded document" do
|
226
|
-
@baz.send(:
|
289
|
+
@baz.send(:find_, @baz.to_param).count.should eql 1
|
227
290
|
end
|
228
291
|
|
229
292
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_slug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 4
|
10
|
+
version: 0.4.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Hakan Ensari
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-07-
|
19
|
+
date: 2010-07-08 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|