mongoid-slugify 0.0.2 → 0.0.3
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/.travis.yml +4 -2
- data/Gemfile +0 -1
- data/lib/mongoid/slugify/version.rb +1 -1
- data/lib/mongoid/slugify.rb +37 -32
- data/spec/mongoid/slugify_spec.rb +67 -16
- data/spec/spec_helper.rb +2 -3
- metadata +12 -12
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/lib/mongoid/slugify.rb
CHANGED
@@ -9,39 +9,9 @@ module Mongoid
|
|
9
9
|
included do
|
10
10
|
field :slug
|
11
11
|
index :slug, :unique => true
|
12
|
-
before_save :assign_slug
|
12
|
+
before_save :assign_slug, :if => :assign_slug?
|
13
13
|
end
|
14
14
|
|
15
|
-
def to_param
|
16
|
-
slug || super
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
def generate_slug
|
21
|
-
raise NotImplementedError
|
22
|
-
end
|
23
|
-
|
24
|
-
def generate_unique_slug
|
25
|
-
current_slug = generate_slug
|
26
|
-
pattern = /^#{Regexp.escape(current_slug)}(?:-(\d+))?$/
|
27
|
-
|
28
|
-
appropriate_class = self.class
|
29
|
-
while (appropriate_class.superclass.include?(Mongoid::Document))
|
30
|
-
appropriate_class = appropriate_class.superclass
|
31
|
-
end
|
32
|
-
|
33
|
-
existing_slugs = appropriate_class.where(:slug => pattern, :_id.ne => _id).only(:slug).map { |record| record.slug }
|
34
|
-
if existing_slugs.count > 0
|
35
|
-
max_counter = existing_slugs.map { |slug| (pattern.match(slug)[1] || 0).to_i }.max
|
36
|
-
current_slug += "-#{max_counter + 1}"
|
37
|
-
end
|
38
|
-
current_slug
|
39
|
-
end
|
40
|
-
|
41
|
-
def assign_slug
|
42
|
-
self.slug = generate_unique_slug
|
43
|
-
end
|
44
|
-
|
45
15
|
module ClassMethods
|
46
16
|
def find_by_slug(slug)
|
47
17
|
where(:slug => slug).first
|
@@ -52,12 +22,47 @@ module Mongoid
|
|
52
22
|
end
|
53
23
|
|
54
24
|
def find_by_slug_or_id(slug_or_id)
|
55
|
-
find_by_slug(slug_or_id) || where(:
|
25
|
+
find_by_slug(slug_or_id) || where(:_id => slug_or_id).first
|
56
26
|
end
|
57
27
|
|
58
28
|
def find_by_slug_or_id!(slug_or_id)
|
59
29
|
find_by_slug(slug_or_id) || find(slug_or_id)
|
60
30
|
end
|
61
31
|
end
|
32
|
+
|
33
|
+
def to_param
|
34
|
+
slug || super
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def assign_slug?
|
39
|
+
true
|
40
|
+
end
|
41
|
+
|
42
|
+
def generate_slug
|
43
|
+
raise NotImplementedError
|
44
|
+
end
|
45
|
+
|
46
|
+
def generate_unique_slug
|
47
|
+
current_slug = generate_slug
|
48
|
+
pattern = /^#{Regexp.escape(current_slug)}(?:-(\d+))?$/
|
49
|
+
|
50
|
+
appropriate_class = self.class
|
51
|
+
while (appropriate_class.superclass.include?(Mongoid::Document))
|
52
|
+
appropriate_class = appropriate_class.superclass
|
53
|
+
end
|
54
|
+
|
55
|
+
existing_slugs = appropriate_class.where(:slug => pattern, :_id.ne => _id).only(:slug).map { |record| record.slug }
|
56
|
+
if existing_slugs.count > 0
|
57
|
+
max_counter = existing_slugs.map { |slug| (pattern.match(slug)[1] || 0).to_i }.max
|
58
|
+
current_slug << "-#{max_counter + 1}"
|
59
|
+
end
|
60
|
+
|
61
|
+
current_slug
|
62
|
+
end
|
63
|
+
|
64
|
+
def assign_slug
|
65
|
+
self.slug = generate_unique_slug
|
66
|
+
end
|
62
67
|
end
|
63
68
|
end
|
@@ -32,6 +32,13 @@ end
|
|
32
32
|
class ComicBook < Book
|
33
33
|
end
|
34
34
|
|
35
|
+
class CookBook < Book
|
36
|
+
private
|
37
|
+
def assign_slug?
|
38
|
+
slug.blank?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
35
42
|
class Person
|
36
43
|
include Mongoid::Document
|
37
44
|
include Mongoid::Slugify
|
@@ -98,6 +105,36 @@ module Mongoid
|
|
98
105
|
end
|
99
106
|
end
|
100
107
|
|
108
|
+
context "when assign_slug? is configured to work only when slug is empty" do
|
109
|
+
let(:book) do
|
110
|
+
CookBook.create(:title => "A Thousand Plateaus")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "generates a slug" do
|
114
|
+
book.to_param.should eql "a-thousand-plateaus"
|
115
|
+
end
|
116
|
+
|
117
|
+
it "does not update the slug" do
|
118
|
+
book.title = "Anti Oedipus"
|
119
|
+
book.save
|
120
|
+
book.to_param.should eql "a-thousand-plateaus"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "updates the slug if it's nil" do
|
124
|
+
book.title = "Anti Oedipus"
|
125
|
+
book.slug = nil
|
126
|
+
book.save
|
127
|
+
book.to_param.should eql "anti-oedipus"
|
128
|
+
end
|
129
|
+
|
130
|
+
it "updates the slug if it's empty" do
|
131
|
+
book.title = "Anti Oedipus"
|
132
|
+
book.slug = ""
|
133
|
+
book.save
|
134
|
+
book.to_param.should eql "anti-oedipus"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
101
138
|
context "when the slug is composed of multiple fields" do
|
102
139
|
let!(:author) do
|
103
140
|
Author.create(
|
@@ -203,29 +240,43 @@ module Mongoid
|
|
203
240
|
end
|
204
241
|
end
|
205
242
|
|
206
|
-
describe
|
207
|
-
let
|
243
|
+
describe 'ClassMethods' do
|
244
|
+
let(:book) { Book.create(:title => "A Thousand Plateaus") }
|
208
245
|
|
209
|
-
|
210
|
-
|
211
|
-
|
246
|
+
describe ".find_by_slug" do
|
247
|
+
it "returns nil if no document is found" do
|
248
|
+
Book.find_by_slug("Anti Oedipus").should be_nil
|
249
|
+
end
|
212
250
|
|
213
|
-
|
214
|
-
|
251
|
+
it "returns the document if it is found" do
|
252
|
+
Book.find_by_slug(book.slug).should == book
|
253
|
+
end
|
215
254
|
end
|
216
|
-
end
|
217
255
|
|
218
|
-
|
219
|
-
|
256
|
+
describe ".find_by_slug!" do
|
257
|
+
it "raises a Mongoid::Errors::DocumentNotFound error if no document is found" do
|
258
|
+
lambda {
|
259
|
+
Book.find_by_slug!("Anti Oedipus")
|
260
|
+
}.should raise_error(Mongoid::Errors::DocumentNotFound)
|
261
|
+
end
|
220
262
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
}.should raise_error(Mongoid::Errors::DocumentNotFound)
|
263
|
+
it "returns the document when it is found" do
|
264
|
+
Book.find_by_slug!(book.slug).should == book
|
265
|
+
end
|
225
266
|
end
|
226
267
|
|
227
|
-
|
228
|
-
|
268
|
+
describe ".find_by_slug_or_id" do
|
269
|
+
it "returns nil if no document is found" do
|
270
|
+
Book.find_by_slug_or_id("Anti Oedipus").should be_nil
|
271
|
+
end
|
272
|
+
|
273
|
+
it "returns the document when it is found by slug" do
|
274
|
+
Book.find_by_slug_or_id(book.slug).should == book
|
275
|
+
end
|
276
|
+
|
277
|
+
it "returns the document when it is found by id" do
|
278
|
+
Book.find_by_slug_or_id(book.id).should == book
|
279
|
+
end
|
229
280
|
end
|
230
281
|
end
|
231
282
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,8 +2,7 @@ require "bundler/setup"
|
|
2
2
|
Bundler.require(:default, :development)
|
3
3
|
|
4
4
|
Mongoid.configure do |config|
|
5
|
-
|
6
|
-
config.master = Mongo::Connection.new.db(name)
|
5
|
+
config.master = Mongo::Connection.new.db("mongoid_slugify_test")
|
7
6
|
end
|
8
7
|
|
9
8
|
DatabaseCleaner.strategy = :truncation
|
@@ -13,7 +12,7 @@ RSpec.configure do |config|
|
|
13
12
|
config.before :each do
|
14
13
|
DatabaseCleaner.start
|
15
14
|
end
|
16
|
-
|
15
|
+
|
17
16
|
config.after :each do
|
18
17
|
DatabaseCleaner.clean
|
19
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-slugify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70279909356200 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70279909356200
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mongoid
|
27
|
-
requirement: &
|
27
|
+
requirement: &70279909355580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70279909355580
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70279909354960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70279909354960
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70279909354460 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70279909354460
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: database_cleaner
|
60
|
-
requirement: &
|
60
|
+
requirement: &70279909353820 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70279909353820
|
69
69
|
description: Provides a simple way to add slug generation to a Mongoid model
|
70
70
|
email:
|
71
71
|
- fxposter@gmail.com
|