mongoid-slug 5.3.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +20 -20
  3. data/README.md +361 -360
  4. data/lib/mongoid/slug.rb +328 -366
  5. data/lib/mongoid/slug/criteria.rb +107 -107
  6. data/lib/mongoid/slug/{index.rb → index_builder.rb} +67 -51
  7. data/lib/mongoid/slug/railtie.rb +9 -9
  8. data/lib/mongoid/slug/slug_id_strategy.rb +3 -3
  9. data/lib/mongoid/slug/unique_slug.rb +173 -154
  10. data/lib/mongoid/slug/version.rb +5 -5
  11. data/lib/mongoid_slug.rb +2 -2
  12. data/lib/tasks/mongoid_slug.rake +15 -19
  13. data/spec/models/alias.rb +6 -6
  14. data/spec/models/article.rb +9 -9
  15. data/spec/models/artist.rb +8 -8
  16. data/spec/models/artwork.rb +10 -10
  17. data/spec/models/author.rb +15 -15
  18. data/spec/models/author_polymorphic.rb +15 -15
  19. data/spec/models/book.rb +12 -12
  20. data/spec/models/book_polymorphic.rb +12 -12
  21. data/spec/models/caption.rb +17 -17
  22. data/spec/models/entity.rb +11 -12
  23. data/spec/models/friend.rb +7 -7
  24. data/spec/models/incorrect_slug_persistence.rb +9 -9
  25. data/spec/models/integer_id.rb +9 -9
  26. data/spec/models/magazine.rb +7 -7
  27. data/spec/models/page.rb +9 -9
  28. data/spec/models/page_localize.rb +9 -9
  29. data/spec/models/page_slug_localized.rb +9 -9
  30. data/spec/models/page_slug_localized_custom.rb +10 -10
  31. data/spec/models/page_slug_localized_history.rb +9 -9
  32. data/spec/models/partner.rb +7 -7
  33. data/spec/models/person.rb +12 -12
  34. data/spec/models/relationship.rb +8 -8
  35. data/spec/models/string_id.rb +9 -9
  36. data/spec/models/subject.rb +7 -7
  37. data/spec/models/without_slug.rb +5 -5
  38. data/spec/mongoid/criteria_spec.rb +207 -208
  39. data/spec/mongoid/index_builder_spec.rb +105 -0
  40. data/spec/mongoid/slug_spec.rb +1175 -1170
  41. data/spec/shared/indexes.rb +41 -41
  42. data/spec/spec_helper.rb +61 -67
  43. data/spec/tasks/mongoid_slug_rake_spec.rb +73 -73
  44. metadata +14 -24
  45. data/lib/mongoid/slug/paranoia.rb +0 -20
  46. data/spec/models/document_paranoid.rb +0 -9
  47. data/spec/models/paranoid_document.rb +0 -8
  48. data/spec/models/paranoid_permanent.rb +0 -10
  49. data/spec/mongoid/index_spec.rb +0 -34
  50. data/spec/mongoid/paranoia_spec.rb +0 -230
@@ -1,20 +0,0 @@
1
- module Mongoid
2
- module Slug
3
- # Lightweight compatibility shim which adds the :restore callback to
4
- # older versions of Mongoid::Paranoia
5
- module Paranoia
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- define_model_callbacks :restore
10
-
11
- def restore_with_callbacks
12
- run_callbacks(:restore) do
13
- restore_without_callbacks
14
- end
15
- end
16
- alias_method_chain :restore, :callbacks
17
- end
18
- end
19
- end
20
- end
@@ -1,9 +0,0 @@
1
- class DocumentParanoid
2
- include Mongoid::Document
3
- # slug, then paranoia
4
- include Mongoid::Slug
5
- include Mongoid::Paranoia
6
-
7
- field :title
8
- slug :title
9
- end
@@ -1,8 +0,0 @@
1
- class ParanoidDocument
2
- include Mongoid::Document
3
- include Mongoid::Paranoia
4
- include Mongoid::Slug
5
-
6
- field :title
7
- slug :title
8
- end
@@ -1,10 +0,0 @@
1
- class ParanoidPermanent
2
- include Mongoid::Document
3
- include Mongoid::Paranoia
4
- include Mongoid::Slug
5
-
6
- field :title
7
- field :foo
8
-
9
- slug :title, scope: :foo, permanent: true
10
- end
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Mongoid::Slug::Index do
5
- let(:scope_key) { nil }
6
- let(:by_model_type) { false }
7
- subject { Mongoid::Slug::Index.build_index(scope_key, by_model_type) }
8
-
9
- context 'when scope_key is set' do
10
- let(:scope_key) { :foo }
11
-
12
- context 'when by_model_type is true' do
13
- let(:by_model_type) { true }
14
-
15
- it { is_expected.to eq [{ _slugs: 1, foo: 1, _type: 1 }, {}] }
16
- end
17
-
18
- context 'when by_model_type is false' do
19
- it { is_expected.to eq [{ _slugs: 1, foo: 1 }, {}] }
20
- end
21
- end
22
-
23
- context 'when scope_key is not set' do
24
- context 'when by_model_type is true' do
25
- let(:by_model_type) { true }
26
-
27
- it { is_expected.to eq [{ _slugs: 1, _type: 1 }, {}] }
28
- end
29
-
30
- context 'when by_model_type is false' do
31
- it { is_expected.to eq [{ _slugs: 1 }, { unique: true, sparse: true }] }
32
- end
33
- end
34
- end
@@ -1,230 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe 'Mongoid::Paranoia with Mongoid::Slug' do
5
- let(:paranoid_doc) { ParanoidDocument.create!(title: 'slug') }
6
- let(:paranoid_doc_2) { ParanoidDocument.create!(title: 'slug') }
7
- let(:paranoid_perm) { ParanoidPermanent.create!(title: 'slug') }
8
- let(:paranoid_perm_2) { ParanoidPermanent.create!(title: 'slug') }
9
- let(:non_paranoid_doc) { Article.create!(title: 'slug') }
10
- subject { paranoid_doc }
11
-
12
- describe '.paranoid?' do
13
- context 'when Mongoid::Paranoia is included' do
14
- subject { paranoid_doc.class }
15
- specify { expect(subject.is_paranoid_doc?).to be true }
16
- end
17
-
18
- context 'when Mongoid::Paranoia not included' do
19
- subject { non_paranoid_doc.class }
20
- specify { expect(subject.is_paranoid_doc?).to be false }
21
- end
22
- end
23
-
24
- describe '#paranoid_deleted?' do
25
- context 'when Mongoid::Paranoia is included' do
26
- context 'when not destroyed' do
27
- specify { expect(subject.paranoid_deleted?).to be false }
28
- end
29
-
30
- context 'when destroyed' do
31
- before { subject.destroy }
32
- specify { expect(subject.paranoid_deleted?).to be true }
33
- end
34
- end
35
-
36
- context 'when Mongoid::Paranoia not included' do
37
- subject { non_paranoid_doc }
38
- specify { expect(subject.paranoid_deleted?).to be false }
39
- end
40
- end
41
-
42
- describe 'restore callbacks' do
43
- context 'when Mongoid::Paranoia is included' do
44
- subject { paranoid_doc.class }
45
- it { is_expected.to respond_to(:before_restore) }
46
- it { is_expected.to respond_to(:after_restore) }
47
- end
48
-
49
- context 'when Mongoid::Paranoia not included' do
50
- it { is_expected.to_not respond_to(:before_restore) }
51
- it { is_expected.to_not respond_to(:after_restore) }
52
- end
53
- end
54
-
55
- describe 'index' do
56
- context 'simple index' do
57
- before { ParanoidDocument.create_indexes }
58
- after { ParanoidDocument.remove_indexes }
59
- subject { ParanoidDocument }
60
-
61
- it_should_behave_like 'has an index', { _slugs: 1 }, unique: true, sparse: true
62
- end
63
-
64
- context 'compound index' do
65
- before { ParanoidPermanent.create_indexes }
66
- after { ParanoidPermanent.remove_indexes }
67
- subject { ParanoidPermanent }
68
-
69
- it_should_behave_like 'has an index', { foo: 1, _slugs: 1 }, unique: nil, sparse: nil
70
- end
71
- end
72
-
73
- shared_examples_for 'paranoid slugs' do
74
- context 'querying' do
75
- it 'returns paranoid_doc for correct slug' do
76
- expect(subject.class.find(subject.slug)).to eq subject
77
- end
78
- end
79
-
80
- context 'delete (callbacks not fired)' do
81
- before { subject.delete }
82
-
83
- it 'retains slug value' do
84
- expect(subject.slug).to eq 'slug'
85
- expect(subject.class.unscoped.find('slug')).to eq subject
86
- end
87
- end
88
-
89
- context 'destroy' do
90
- before { subject.destroy }
91
-
92
- it 'unsets slug value when destroyed' do
93
- expect(subject._slugs).to eq []
94
- expect(subject.slug).to be_nil
95
- end
96
-
97
- it 'persists the removed slug' do
98
- expect(subject.reload._slugs).to be nil
99
- expect(subject.reload.slug).to eq subject._id.to_s
100
- end
101
-
102
- it 'persists the removed slug in the database' do
103
- expect(subject.class.unscoped.exists(_slugs: false).first).to eq subject
104
- expect { subject.class.unscoped.find('slug') }.to raise_error(Mongoid::Errors::DocumentNotFound)
105
- end
106
-
107
- context 'when saving the doc again' do
108
- before { subject.save }
109
-
110
- it 'should have the default slug value' do
111
- expect(subject._slugs).to eq []
112
- expect(subject.slug).to be_nil
113
- end
114
-
115
- it 'the slug remains unset in the database' do
116
- expect(subject.class.unscoped.exists(_slugs: false).first).to eq subject
117
- expect { subject.class.unscoped.find('slug') }.to raise_error(Mongoid::Errors::DocumentNotFound)
118
- end
119
- end
120
- end
121
-
122
- context 'restore' do
123
- before do
124
- subject.destroy
125
- subject.restore
126
- end
127
-
128
- it 'resets slug value when restored' do
129
- expect(subject.slug).to eq 'slug'
130
- expect(subject.reload.slug).to eq 'slug'
131
- end
132
- end
133
-
134
- context 'multiple documents' do
135
- it 'new documents should be able to use the slug of destroyed documents' do
136
- expect(subject.slug).to eq 'slug'
137
- subject.destroy
138
- expect(subject.reload.slug).to eq subject._id.to_s
139
- expect(other_doc.slug).to eq 'slug'
140
- subject.restore
141
- expect(subject.slug).to eq 'slug-1'
142
- expect(subject.reload.slug).to eq 'slug-1'
143
- end
144
-
145
- it 'should allow multiple documents to be destroyed without index conflict' do
146
- expect(subject.slug).to eq 'slug'
147
- subject.destroy
148
- expect(subject.reload.slug).to eq subject._id.to_s
149
- expect(other_doc.slug).to eq 'slug'
150
- other_doc.destroy
151
- expect(other_doc.reload.slug).to eq other_doc._id.to_s
152
- end
153
- end
154
- end
155
-
156
- [ParanoidDocument, DocumentParanoid].each do |paranoid_klass|
157
- context paranoid_klass.to_s do
158
- let(:paranoid_doc) { paranoid_klass.create!(title: 'slug') }
159
- let(:non_paranoid_doc) { Article.create!(title: 'slug') }
160
-
161
- subject { paranoid_doc }
162
-
163
- describe '.paranoid?' do
164
- context 'when Mongoid::Paranoia is included' do
165
- subject { paranoid_doc.class }
166
- its(:is_paranoid_doc?) { is_expected.to be_truthy }
167
- end
168
-
169
- context 'when Mongoid::Paranoia not included' do
170
- subject { non_paranoid_doc.class }
171
- its(:is_paranoid_doc?) { is_expected.to be_falsey }
172
- end
173
- end
174
-
175
- describe '#paranoid_deleted?' do
176
- context 'when Mongoid::Paranoia is included' do
177
- context 'when not destroyed' do
178
- its(:paranoid_deleted?) { is_expected.to be_falsey }
179
- end
180
-
181
- context 'when destroyed' do
182
- before { subject.destroy }
183
- its(:paranoid_deleted?) { is_expected.to be_truthy }
184
- end
185
- end
186
-
187
- context 'when Mongoid::Paranoia not included' do
188
- subject { non_paranoid_doc }
189
- its(:paranoid_deleted?) { is_expected.to be_falsey }
190
- end
191
- end
192
-
193
- describe 'restore callbacks' do
194
- context 'when Mongoid::Paranoia is included' do
195
- subject { paranoid_doc.class }
196
- it { is_expected.to respond_to(:before_restore) }
197
- it { is_expected.to respond_to(:after_restore) }
198
- end
199
-
200
- context 'when Mongoid::Paranoia not included' do
201
- it { is_expected.not_to respond_to(:before_restore) }
202
- it { is_expected.not_to respond_to(:after_restore) }
203
- end
204
- end
205
-
206
- describe 'index' do
207
- before { paranoid_klass.create_indexes }
208
- after { paranoid_klass.remove_indexes }
209
- subject { paranoid_klass }
210
-
211
- it_should_behave_like 'has an index', { _slugs: 1 }, unique: true, sparse: true
212
- end
213
-
214
- context 'non-permanent slug' do
215
- let(:paranoid_doc_2) { paranoid_klass.create!(title: 'slug') }
216
- subject { paranoid_doc }
217
- let(:other_doc) { paranoid_doc_2 }
218
- it_behaves_like 'paranoid slugs'
219
- end
220
- end
221
- end
222
-
223
- context 'permanent slug' do
224
- let(:paranoid_perm) { ParanoidPermanent.create!(title: 'slug') }
225
- let(:paranoid_perm_2) { ParanoidPermanent.create!(title: 'slug') }
226
- subject { paranoid_perm }
227
- let(:other_doc) { paranoid_perm_2 }
228
- it_behaves_like 'paranoid slugs'
229
- end
230
- end