mongoid-slug 4.0.0 → 5.0.0
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.
- checksums.yaml +13 -5
- data/LICENSE +1 -1
- data/README.md +18 -18
- data/lib/mongoid/slug.rb +48 -44
- data/lib/mongoid/slug/criteria.rb +14 -11
- data/lib/mongoid/slug/index.rb +5 -8
- data/lib/mongoid/slug/paranoia.rb +0 -2
- data/lib/mongoid/slug/slug_id_strategy.rb +1 -1
- data/lib/mongoid/slug/unique_slug.rb +27 -29
- data/lib/mongoid/slug/version.rb +1 -1
- data/spec/models/alias.rb +2 -2
- data/spec/models/article.rb +1 -1
- data/spec/models/author.rb +3 -3
- data/spec/models/author_polymorphic.rb +3 -3
- data/spec/models/book.rb +1 -1
- data/spec/models/book_polymorphic.rb +1 -1
- data/spec/models/caption.rb +1 -1
- data/spec/models/entity.rb +2 -2
- data/spec/models/friend.rb +2 -2
- data/spec/models/incorrect_slug_persistence.rb +5 -5
- data/spec/models/integer_id.rb +1 -1
- data/spec/models/magazine.rb +1 -1
- data/spec/models/page.rb +3 -3
- data/spec/models/page_localize.rb +3 -3
- data/spec/models/page_slug_localized.rb +3 -3
- data/spec/models/page_slug_localized_custom.rb +0 -1
- data/spec/models/page_slug_localized_history.rb +3 -3
- data/spec/models/paranoid_document.rb +1 -1
- data/spec/models/paranoid_permanent.rb +1 -1
- data/spec/models/partner.rb +1 -1
- data/spec/models/person.rb +2 -2
- data/spec/models/relationship.rb +1 -1
- data/spec/models/string_id.rb +1 -1
- data/spec/models/subject.rb +1 -1
- data/spec/models/without_slug.rb +1 -1
- data/spec/mongoid/criteria_spec.rb +109 -109
- data/spec/mongoid/index_spec.rb +12 -14
- data/spec/mongoid/paranoia_spec.rb +78 -90
- data/spec/mongoid/slug_spec.rb +493 -492
- data/spec/shared/indexes.rb +13 -13
- data/spec/spec_helper.rb +18 -14
- metadata +51 -26
- data/spec/mongoid/slug_spec.rb.b00 +0 -1101
data/lib/mongoid/slug/version.rb
CHANGED
data/spec/models/alias.rb
CHANGED
data/spec/models/article.rb
CHANGED
data/spec/models/author.rb
CHANGED
@@ -3,9 +3,9 @@ class Author
|
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :first_name
|
5
5
|
field :last_name
|
6
|
-
slug
|
6
|
+
slug :first_name, :last_name, scope: :book
|
7
7
|
belongs_to :book
|
8
8
|
has_many :characters,
|
9
|
-
:
|
10
|
-
:
|
9
|
+
class_name: 'Person',
|
10
|
+
foreign_key: :author_id
|
11
11
|
end
|
@@ -3,9 +3,9 @@ class AuthorPolymorphic
|
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :first_name
|
5
5
|
field :last_name
|
6
|
-
slug
|
6
|
+
slug :first_name, :last_name, scope: :book_polymorphic
|
7
7
|
belongs_to :book_polymorphic
|
8
8
|
has_many :characters,
|
9
|
-
:
|
10
|
-
:
|
9
|
+
class_name: 'Person',
|
10
|
+
foreign_key: :author_id
|
11
11
|
end
|
data/spec/models/book.rb
CHANGED
data/spec/models/caption.rb
CHANGED
data/spec/models/entity.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#encoding: utf-8
|
1
|
+
# encoding: utf-8
|
2
2
|
class Entity
|
3
3
|
include Mongoid::Document
|
4
4
|
include Mongoid::Slug
|
@@ -8,5 +8,5 @@ class Entity
|
|
8
8
|
field :name
|
9
9
|
field :user_edited_variation
|
10
10
|
|
11
|
-
slug
|
11
|
+
slug :user_edited_variation, history: true
|
12
12
|
end
|
data/spec/models/friend.rb
CHANGED
@@ -2,6 +2,6 @@ class Friend
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :name
|
5
|
-
field :slug_history, :
|
6
|
-
slug
|
5
|
+
field :slug_history, type: Array
|
6
|
+
slug :name, reserve: ['foo', 'bar', /^[a-z]{2}$/i], history: true
|
7
7
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class IncorrectSlugPersistence
|
2
|
-
|
3
|
-
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Slug
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
field :name
|
6
|
+
slug :name, history: true
|
7
7
|
|
8
|
-
|
8
|
+
validates_length_of :name, minimum: 4, maximum: 5, allow_blank: true
|
9
9
|
end
|
data/spec/models/integer_id.rb
CHANGED
data/spec/models/magazine.rb
CHANGED
data/spec/models/page.rb
CHANGED
@@ -3,7 +3,7 @@ class PageSlugLocalized
|
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :title, localize: true
|
5
5
|
field :content
|
6
|
-
field :order, :
|
7
|
-
slug
|
8
|
-
default_scope ->{ asc(:order) }
|
6
|
+
field :order, type: Integer
|
7
|
+
slug :title, localize: true
|
8
|
+
default_scope -> { asc(:order) }
|
9
9
|
end
|
@@ -3,7 +3,7 @@ class PageSlugLocalizedHistory
|
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :title, localize: true
|
5
5
|
field :content
|
6
|
-
field :order, :
|
7
|
-
slug
|
8
|
-
default_scope ->{ asc(:order) }
|
6
|
+
field :order, type: Integer
|
7
|
+
slug :title, localize: true, history: true
|
8
|
+
default_scope -> { asc(:order) }
|
9
9
|
end
|
data/spec/models/partner.rb
CHANGED
data/spec/models/person.rb
CHANGED
@@ -2,7 +2,7 @@ class Person
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Slug
|
4
4
|
field :name
|
5
|
-
slug
|
5
|
+
slug :name, permanent: true, scope: :author
|
6
6
|
embeds_many :relationships
|
7
|
-
belongs_to :author, :
|
7
|
+
belongs_to :author, inverse_of: :characters
|
8
8
|
end
|
data/spec/models/relationship.rb
CHANGED
data/spec/models/string_id.rb
CHANGED
data/spec/models/subject.rb
CHANGED
data/spec/models/without_slug.rb
CHANGED
@@ -1,188 +1,188 @@
|
|
1
|
-
#encoding: utf-8
|
2
|
-
require
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Mongoid::Slug::Criteria do
|
5
|
-
describe
|
6
|
-
let!(:book) { Book.create(:
|
7
|
-
let!(:book2) { Book.create(:
|
8
|
-
let!(:friend) { Friend.create(:
|
9
|
-
let!(:friend2) { Friend.create(:
|
10
|
-
let!(:integer_id) { IntegerId.new(:
|
11
|
-
let!(:integer_id2) { IntegerId.new(:
|
12
|
-
let!(:string_id) { StringId.new(:
|
13
|
-
let!(:string_id2) { StringId.new(:
|
14
|
-
let!(:subject) { Subject.create(:
|
15
|
-
let!(:subject2) { Subject.create(:
|
16
|
-
let!(:without_slug) { WithoutSlug.new
|
17
|
-
|
18
|
-
context
|
5
|
+
describe '.find' do
|
6
|
+
let!(:book) { Book.create(title: 'A Working Title').tap { |d| d.update_attribute(:title, 'A Thousand Plateaus') } }
|
7
|
+
let!(:book2) { Book.create(title: 'Difference and Repetition') }
|
8
|
+
let!(:friend) { Friend.create(name: 'Jim Bob') }
|
9
|
+
let!(:friend2) { Friend.create(name: friend.id.to_s) }
|
10
|
+
let!(:integer_id) { IntegerId.new(name: 'I have integer ids').tap { |d| d.id = 123; d.save! } }
|
11
|
+
let!(:integer_id2) { IntegerId.new(name: integer_id.id.to_s).tap { |d| d.id = 456; d.save! } }
|
12
|
+
let!(:string_id) { StringId.new(name: 'I have string ids').tap { |d| d.id = 'abc'; d.save! } }
|
13
|
+
let!(:string_id2) { StringId.new(name: string_id.id.to_s).tap { |d| d.id = 'def'; d.save! } }
|
14
|
+
let!(:subject) { Subject.create(name: 'A Subject', book: book) }
|
15
|
+
let!(:subject2) { Subject.create(name: 'A Subject', book: book2) }
|
16
|
+
let!(:without_slug) { WithoutSlug.new.tap { |d| d.id = 456; d.save! } }
|
17
|
+
|
18
|
+
context 'when the model does not use mongoid slugs' do
|
19
19
|
it "should not use mongoid slug's custom find methods" do
|
20
|
-
Mongoid::Slug::Criteria.
|
21
|
-
WithoutSlug.find(without_slug.id.to_s).
|
20
|
+
expect_any_instance_of(Mongoid::Slug::Criteria).not_to receive(:find)
|
21
|
+
expect(WithoutSlug.find(without_slug.id.to_s)).to eq(without_slug)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
context
|
26
|
-
context
|
27
|
-
context
|
28
|
-
it
|
29
|
-
Book.find(book.slugs.first).
|
25
|
+
context 'using slugs' do
|
26
|
+
context '(single)' do
|
27
|
+
context 'and a document is found' do
|
28
|
+
it 'returns the document as an object' do
|
29
|
+
expect(Book.find(book.slugs.first)).to eq(book)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
context
|
34
|
-
it
|
35
|
-
|
36
|
-
Book.find(
|
37
|
-
|
33
|
+
context 'but no document is found' do
|
34
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
35
|
+
expect do
|
36
|
+
Book.find('Anti Oedipus')
|
37
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
context
|
43
|
-
context
|
44
|
-
it
|
45
|
-
Book.find(book.slugs + book2.slugs).
|
42
|
+
context '(multiple)' do
|
43
|
+
context 'and all documents are found' do
|
44
|
+
it 'returns the documents as an array without duplication' do
|
45
|
+
expect(Book.find(book.slugs + book2.slugs)).to match_array([book, book2])
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
context
|
50
|
-
it
|
51
|
-
|
49
|
+
context 'but not all documents are found' do
|
50
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
51
|
+
expect do
|
52
52
|
Book.find(book.slugs + ['something-nonexistent'])
|
53
|
-
|
53
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
context
|
59
|
-
it
|
60
|
-
|
61
|
-
Book.find(
|
62
|
-
|
58
|
+
context 'when no documents match' do
|
59
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
60
|
+
expect do
|
61
|
+
Book.find('Anti Oedipus')
|
62
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
context
|
67
|
-
it
|
68
|
-
Friend.find(friend.id.to_s).
|
66
|
+
context 'when ids are BSON::ObjectIds and the supplied argument looks like a BSON::ObjectId' do
|
67
|
+
it 'it should find based on ids not slugs' do # i.e. it should type cast the argument
|
68
|
+
expect(Friend.find(friend.id.to_s)).to eq(friend)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
context
|
73
|
-
it
|
74
|
-
StringId.find(string_id.id.to_s).
|
72
|
+
context 'when ids are Strings' do
|
73
|
+
it 'it should find based on ids not slugs' do # i.e. string ids should take precedence over string slugs
|
74
|
+
expect(StringId.find(string_id.id.to_s)).to eq(string_id)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
context
|
79
|
-
it
|
80
|
-
IntegerId.find(integer_id.id.to_s).
|
78
|
+
context 'when ids are Integers and the supplied arguments looks like an Integer' do
|
79
|
+
it 'it should find based on slugs not ids' do # i.e. it should not type cast the argument
|
80
|
+
expect(IntegerId.find(integer_id.id.to_s)).to eq(integer_id2)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
|
-
context
|
85
|
-
it
|
86
|
-
WithoutSlug.find(without_slug.id.to_s).
|
84
|
+
context 'models that does not use slugs, should find using the original find' do
|
85
|
+
it 'it should find based on ids' do # i.e. it should not type cast the argument
|
86
|
+
expect(WithoutSlug.find(without_slug.id.to_s)).to eq(without_slug)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
context
|
91
|
-
context
|
92
|
-
it
|
93
|
-
book.subjects.find(subject.slugs.first).
|
94
|
-
book2.subjects.find(subject.slugs.first).
|
90
|
+
context 'when scoped' do
|
91
|
+
context 'and a document is found' do
|
92
|
+
it 'returns the document as an object' do
|
93
|
+
expect(book.subjects.find(subject.slugs.first)).to eq(subject)
|
94
|
+
expect(book2.subjects.find(subject.slugs.first)).to eq(subject2)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
context
|
99
|
-
it
|
100
|
-
|
98
|
+
context 'but no document is found' do
|
99
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
100
|
+
expect do
|
101
101
|
book.subjects.find('Another Subject')
|
102
|
-
|
102
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
context
|
109
|
-
it
|
110
|
-
|
108
|
+
context 'using ids' do
|
109
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error if no document is found' do
|
110
|
+
expect do
|
111
111
|
Book.find(friend.id)
|
112
|
-
|
112
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
113
113
|
end
|
114
114
|
|
115
|
-
context
|
116
|
-
it
|
117
|
-
Friend.find(friend.id).
|
115
|
+
context 'given a single document' do
|
116
|
+
it 'returns the document' do
|
117
|
+
expect(Friend.find(friend.id)).to eq(friend)
|
118
118
|
end
|
119
119
|
end
|
120
120
|
|
121
|
-
context
|
122
|
-
it
|
123
|
-
Book.find([book.id, book2.id]).
|
121
|
+
context 'given multiple documents' do
|
122
|
+
it 'returns the documents' do
|
123
|
+
expect(Book.find([book.id, book2.id])).to match_array([book, book2])
|
124
124
|
end
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
|
-
describe
|
130
|
-
let!(:book) { Book.create(:
|
131
|
-
let!(:book2) { Book.create(:
|
132
|
-
let!(:friend) { Friend.create(:
|
133
|
-
let!(:friend2) { Friend.create(:
|
134
|
-
let!(:integer_id) { IntegerId.new(:
|
135
|
-
let!(:integer_id2) { IntegerId.new(:
|
136
|
-
let!(:string_id) { StringId.new(:
|
137
|
-
let!(:string_id2) { StringId.new(:
|
138
|
-
let!(:subject) { Subject.create(:
|
139
|
-
let!(:subject2) { Subject.create(:
|
129
|
+
describe '.find_by_slug!' do
|
130
|
+
let!(:book) { Book.create(title: 'A Working Title').tap { |d| d.update_attribute(:title, 'A Thousand Plateaus') } }
|
131
|
+
let!(:book2) { Book.create(title: 'Difference and Repetition') }
|
132
|
+
let!(:friend) { Friend.create(name: 'Jim Bob') }
|
133
|
+
let!(:friend2) { Friend.create(name: friend.id.to_s) }
|
134
|
+
let!(:integer_id) { IntegerId.new(name: 'I have integer ids').tap { |d| d.id = 123; d.save! } }
|
135
|
+
let!(:integer_id2) { IntegerId.new(name: integer_id.id.to_s).tap { |d| d.id = 456; d.save! } }
|
136
|
+
let!(:string_id) { StringId.new(name: 'I have string ids').tap { |d| d.id = 'abc'; d.save! } }
|
137
|
+
let!(:string_id2) { StringId.new(name: string_id.id.to_s).tap { |d| d.id = 'def'; d.save! } }
|
138
|
+
let!(:subject) { Subject.create(name: 'A Subject', book: book) }
|
139
|
+
let!(:subject2) { Subject.create(name: 'A Subject', book: book2) }
|
140
140
|
|
141
|
-
context
|
142
|
-
context
|
143
|
-
it
|
144
|
-
Book.find_by_slug!(book.slugs.first).
|
141
|
+
context '(single)' do
|
142
|
+
context 'and a document is found' do
|
143
|
+
it 'returns the document as an object' do
|
144
|
+
expect(Book.find_by_slug!(book.slugs.first)).to eq(book)
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
148
|
-
context
|
149
|
-
it
|
150
|
-
|
151
|
-
Book.find_by_slug!(
|
152
|
-
|
148
|
+
context 'but no document is found' do
|
149
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
150
|
+
expect do
|
151
|
+
Book.find_by_slug!('Anti Oedipus')
|
152
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
153
153
|
end
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
|
-
context
|
158
|
-
context
|
159
|
-
it
|
160
|
-
Book.find_by_slug!(book.slugs + book2.slugs).
|
157
|
+
context '(multiple)' do
|
158
|
+
context 'and all documents are found' do
|
159
|
+
it 'returns the documents as an array without duplication' do
|
160
|
+
expect(Book.find_by_slug!(book.slugs + book2.slugs)).to match_array([book, book2])
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
164
|
-
context
|
165
|
-
it
|
166
|
-
|
164
|
+
context 'but not all documents are found' do
|
165
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
166
|
+
expect do
|
167
167
|
Book.find_by_slug!(book.slugs + ['something-nonexistent'])
|
168
|
-
|
168
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
169
169
|
end
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
173
|
-
context
|
174
|
-
context
|
175
|
-
it
|
176
|
-
book.subjects.find_by_slug!(subject.slugs.first).
|
177
|
-
book2.subjects.find_by_slug!(subject.slugs.first).
|
173
|
+
context 'when scoped' do
|
174
|
+
context 'and a document is found' do
|
175
|
+
it 'returns the document as an object' do
|
176
|
+
expect(book.subjects.find_by_slug!(subject.slugs.first)).to eq(subject)
|
177
|
+
expect(book2.subjects.find_by_slug!(subject.slugs.first)).to eq(subject2)
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
181
|
-
context
|
182
|
-
it
|
183
|
-
|
181
|
+
context 'but no document is found' do
|
182
|
+
it 'raises a Mongoid::Errors::DocumentNotFound error' do
|
183
|
+
expect do
|
184
184
|
book.subjects.find_by_slug!('Another Subject')
|
185
|
-
|
185
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
186
186
|
end
|
187
187
|
end
|
188
188
|
end
|