mongoid-history 0.8.3 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -1
- data/.document +5 -5
- data/.github/workflows/test.yml +72 -0
- data/.gitignore +46 -46
- data/.rspec +2 -2
- data/.rubocop.yml +6 -6
- data/.rubocop_todo.yml +99 -99
- data/CHANGELOG.md +173 -163
- data/CONTRIBUTING.md +117 -118
- data/Dangerfile +1 -1
- data/Gemfile +49 -40
- data/LICENSE.txt +20 -20
- data/README.md +609 -608
- data/RELEASING.md +66 -67
- data/Rakefile +24 -24
- data/UPGRADING.md +53 -53
- data/lib/mongoid/history/attributes/base.rb +72 -72
- data/lib/mongoid/history/attributes/create.rb +45 -45
- data/lib/mongoid/history/attributes/destroy.rb +34 -34
- data/lib/mongoid/history/attributes/update.rb +104 -104
- data/lib/mongoid/history/options.rb +177 -177
- data/lib/mongoid/history/trackable.rb +588 -583
- data/lib/mongoid/history/tracker.rb +247 -247
- data/lib/mongoid/history/version.rb +5 -5
- data/lib/mongoid/history.rb +77 -77
- data/lib/mongoid-history.rb +1 -1
- data/mongoid-history.gemspec +25 -25
- data/perf/benchmark_modified_attributes_for_create.rb +65 -65
- data/perf/gc_suite.rb +21 -21
- data/spec/integration/embedded_in_polymorphic_spec.rb +112 -112
- data/spec/integration/integration_spec.rb +976 -976
- data/spec/integration/multi_relation_spec.rb +47 -47
- data/spec/integration/multiple_trackers_spec.rb +68 -68
- data/spec/integration/nested_embedded_documents_spec.rb +64 -64
- data/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb +124 -124
- data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +115 -115
- data/spec/integration/subclasses_spec.rb +47 -47
- data/spec/integration/track_history_order_spec.rb +84 -84
- data/spec/integration/validation_failure_spec.rb +76 -76
- data/spec/spec_helper.rb +32 -30
- data/spec/support/error_helpers.rb +7 -0
- data/spec/support/mongoid.rb +11 -11
- data/spec/support/mongoid_history.rb +12 -12
- data/spec/unit/attributes/base_spec.rb +141 -141
- data/spec/unit/attributes/create_spec.rb +342 -342
- data/spec/unit/attributes/destroy_spec.rb +228 -228
- data/spec/unit/attributes/update_spec.rb +342 -342
- data/spec/unit/callback_options_spec.rb +165 -165
- data/spec/unit/embedded_methods_spec.rb +87 -87
- data/spec/unit/history_spec.rb +58 -58
- data/spec/unit/my_instance_methods_spec.rb +555 -555
- data/spec/unit/options_spec.rb +365 -365
- data/spec/unit/singleton_methods_spec.rb +406 -406
- data/spec/unit/store/default_store_spec.rb +11 -11
- data/spec/unit/store/request_store_spec.rb +13 -13
- data/spec/unit/trackable_spec.rb +1057 -987
- data/spec/unit/tracker_spec.rb +190 -190
- metadata +9 -7
- data/.travis.yml +0 -36
@@ -1,406 +1,406 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Trackable do
|
4
|
-
describe 'SingletonMethods' do
|
5
|
-
before :each do
|
6
|
-
class MyTrackableModel
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::History::Trackable
|
9
|
-
|
10
|
-
field :foo
|
11
|
-
field :b, as: :bar
|
12
|
-
|
13
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
14
|
-
embeds_one :my_embed_one_model
|
15
|
-
embeds_one :my_untracked_embed_one_model
|
16
|
-
embeds_many :my_embed_many_models
|
17
|
-
else
|
18
|
-
embeds_one :my_embed_one_model, inverse_class_name: 'MyEmbedOneModel'
|
19
|
-
embeds_one :my_untracked_embed_one_model, inverse_class_name: 'MyUntrackedEmbedOneModel'
|
20
|
-
embeds_many :my_embed_many_models, inverse_class_name: 'MyEmbedManyModel'
|
21
|
-
end
|
22
|
-
|
23
|
-
track_history on: %i[foo my_embed_one_model my_embed_many_models my_dynamic_field]
|
24
|
-
end
|
25
|
-
|
26
|
-
class MyEmbedOneModel
|
27
|
-
include Mongoid::Document
|
28
|
-
|
29
|
-
field :baz
|
30
|
-
embedded_in :my_trackable_model
|
31
|
-
end
|
32
|
-
|
33
|
-
class MyUntrackedEmbedOneModel
|
34
|
-
include Mongoid::Document
|
35
|
-
|
36
|
-
field :baz
|
37
|
-
embedded_in :my_trackable_model
|
38
|
-
end
|
39
|
-
|
40
|
-
class MyEmbedManyModel
|
41
|
-
include Mongoid::Document
|
42
|
-
|
43
|
-
field :bla
|
44
|
-
embedded_in :my_trackable_model
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
after :each do
|
49
|
-
Object.send(:remove_const, :MyTrackableModel)
|
50
|
-
Object.send(:remove_const, :MyEmbedOneModel)
|
51
|
-
Object.send(:remove_const, :MyUntrackedEmbedOneModel)
|
52
|
-
Object.send(:remove_const, :MyEmbedManyModel)
|
53
|
-
end
|
54
|
-
|
55
|
-
describe '#tracked?' do
|
56
|
-
before { allow(MyTrackableModel).to receive(:dynamic_enabled?) { false } }
|
57
|
-
it { expect(MyTrackableModel.tracked?(:foo)).to be true }
|
58
|
-
it { expect(MyTrackableModel.tracked?(:bar)).to be false }
|
59
|
-
it { expect(MyTrackableModel.tracked?(:my_embed_one_model)).to be true }
|
60
|
-
it { expect(MyTrackableModel.tracked?(:my_untracked_embed_one_model)).to be false }
|
61
|
-
it { expect(MyTrackableModel.tracked?(:my_embed_many_models)).to be true }
|
62
|
-
it { expect(MyTrackableModel.tracked?(:my_dynamic_field)).to be true }
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#dynamic_field?' do
|
66
|
-
before :each do
|
67
|
-
class EmbOne
|
68
|
-
include Mongoid::Document
|
69
|
-
|
70
|
-
embedded_in :my_model
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
after :each do
|
75
|
-
Object.send(:remove_const, :EmbOne)
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'when dynamic enabled' do
|
79
|
-
context 'with embeds one relation' do
|
80
|
-
before :each do
|
81
|
-
class MyModel
|
82
|
-
include Mongoid::Document
|
83
|
-
include Mongoid::History::Trackable
|
84
|
-
|
85
|
-
store_in collection: :my_models
|
86
|
-
|
87
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
88
|
-
embeds_one :emb_one
|
89
|
-
else
|
90
|
-
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
91
|
-
end
|
92
|
-
|
93
|
-
track_history
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
after :each do
|
98
|
-
Object.send(:remove_const, :MyModel)
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'should track dynamic field' do
|
102
|
-
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
103
|
-
expect(MyModel.dynamic_field?(:foo)).to be true
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should not track embeds_one relation' do
|
107
|
-
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
108
|
-
expect(MyModel.dynamic_field?(:emb_one)).to be false
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context 'with embeds one relation and alias' do
|
113
|
-
before :each do
|
114
|
-
class MyModel
|
115
|
-
include Mongoid::Document
|
116
|
-
include Mongoid::History::Trackable
|
117
|
-
|
118
|
-
store_in collection: :my_models
|
119
|
-
|
120
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
121
|
-
embeds_one :emb_one, store_as: :emo
|
122
|
-
else
|
123
|
-
embeds_one :emb_one, inverse_class_name: 'EmbOne', store_as: :emo
|
124
|
-
end
|
125
|
-
|
126
|
-
track_history
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
after :each do
|
131
|
-
Object.send(:remove_const, :MyModel)
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'should not track embeds_one relation' do
|
135
|
-
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
136
|
-
expect(MyModel.dynamic_field?(:emo)).to be false
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
context 'with embeds many relation' do
|
141
|
-
before :each do
|
142
|
-
class MyModel
|
143
|
-
include Mongoid::Document
|
144
|
-
include Mongoid::History::Trackable
|
145
|
-
|
146
|
-
store_in collection: :my_models
|
147
|
-
|
148
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
149
|
-
embeds_many :emb_ones
|
150
|
-
else
|
151
|
-
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
152
|
-
end
|
153
|
-
|
154
|
-
track_history
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
after :each do
|
159
|
-
Object.send(:remove_const, :MyModel)
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'should not track embeds_many relation' do
|
163
|
-
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
164
|
-
expect(MyModel.dynamic_field?(:emb_ones)).to be false
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context 'with embeds many relation and alias' do
|
169
|
-
before :each do
|
170
|
-
class MyModel
|
171
|
-
include Mongoid::Document
|
172
|
-
include Mongoid::History::Trackable
|
173
|
-
|
174
|
-
store_in collection: :my_models
|
175
|
-
|
176
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
177
|
-
embeds_many :emb_ones, store_as: :emos
|
178
|
-
else
|
179
|
-
embeds_many :emb_ones, store_as: :emos, inverse_class_name: 'EmbOne'
|
180
|
-
end
|
181
|
-
track_history
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
after :each do
|
186
|
-
Object.send(:remove_const, :MyModel)
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'should not track embeds_many relation' do
|
190
|
-
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
191
|
-
expect(MyModel.dynamic_field?(:emos)).to be false
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
describe '#tracked_fields' do
|
198
|
-
it 'should include fields and dynamic fields' do
|
199
|
-
expect(MyTrackableModel.tracked_fields).to eq %w[foo my_dynamic_field]
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
describe '#tracked_relation?' do
|
204
|
-
it 'should return true if a relation is tracked' do
|
205
|
-
expect(MyTrackableModel.tracked_relation?(:my_embed_one_model)).to be true
|
206
|
-
expect(MyTrackableModel.tracked_relation?(:my_untracked_embed_one_model)).to be false
|
207
|
-
expect(MyTrackableModel.tracked_relation?(:my_embed_many_models)).to be true
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe '#tracked_embeds_one?' do
|
212
|
-
it { expect(MyTrackableModel.tracked_embeds_one?(:my_embed_one_model)).to be true }
|
213
|
-
it { expect(MyTrackableModel.tracked_embeds_one?(:my_untracked_embed_one_model)).to be false }
|
214
|
-
it { expect(MyTrackableModel.tracked_embeds_one?(:my_embed_many_models)).to be false }
|
215
|
-
end
|
216
|
-
|
217
|
-
describe '#tracked_embeds_one' do
|
218
|
-
it { expect(MyTrackableModel.tracked_embeds_one).to include 'my_embed_one_model' }
|
219
|
-
it { expect(MyTrackableModel.tracked_embeds_one).to_not include 'my_untracked_embed_one_model' }
|
220
|
-
end
|
221
|
-
|
222
|
-
describe '#tracked_embeds_one_attributes' do
|
223
|
-
before :each do
|
224
|
-
class ModelOne
|
225
|
-
include Mongoid::Document
|
226
|
-
include Mongoid::History::Trackable
|
227
|
-
|
228
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
229
|
-
embeds_one :emb_one
|
230
|
-
embeds_one :emb_two, store_as: :emt
|
231
|
-
embeds_one :emb_three
|
232
|
-
else
|
233
|
-
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
234
|
-
embeds_one :emb_two, store_as: :emt, inverse_class_name: 'EmbTwo'
|
235
|
-
embeds_one :emb_three, inverse_class_name: 'EmbThree'
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
class EmbOne
|
240
|
-
include Mongoid::Document
|
241
|
-
|
242
|
-
field :em_foo
|
243
|
-
field :em_bar
|
244
|
-
|
245
|
-
embedded_in :model_one
|
246
|
-
end
|
247
|
-
|
248
|
-
class EmbTwo
|
249
|
-
include Mongoid::Document
|
250
|
-
|
251
|
-
field :em_bar
|
252
|
-
embedded_in :model_one
|
253
|
-
end
|
254
|
-
|
255
|
-
class EmbThree
|
256
|
-
include Mongoid::Document
|
257
|
-
|
258
|
-
field :em_baz
|
259
|
-
embedded_in :model_one
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
after :each do
|
264
|
-
Object.send(:remove_const, :ModelOne)
|
265
|
-
Object.send(:remove_const, :EmbOne)
|
266
|
-
Object.send(:remove_const, :EmbTwo)
|
267
|
-
Object.send(:remove_const, :EmbThree)
|
268
|
-
end
|
269
|
-
|
270
|
-
context 'when relation tracked' do
|
271
|
-
before(:each) { ModelOne.track_history(on: :emb_one) }
|
272
|
-
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo em_bar] }
|
273
|
-
end
|
274
|
-
|
275
|
-
context 'when relation tracked with alias' do
|
276
|
-
before(:each) { ModelOne.track_history(on: :emb_two) }
|
277
|
-
it { expect(ModelOne.tracked_embeds_one_attributes('emb_two')).to eq %w[_id em_bar] }
|
278
|
-
end
|
279
|
-
|
280
|
-
context 'when relation tracked with attributes' do
|
281
|
-
before(:each) { ModelOne.track_history(on: { emb_one: :em_foo }) }
|
282
|
-
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo] }
|
283
|
-
end
|
284
|
-
|
285
|
-
context 'when relation not tracked' do
|
286
|
-
before(:each) { ModelOne.track_history(on: :fields) }
|
287
|
-
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to be_nil }
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
describe '#tracked_embeds_many?' do
|
292
|
-
it { expect(MyTrackableModel.tracked_embeds_many?(:my_embed_one_model)).to be false }
|
293
|
-
it { expect(MyTrackableModel.tracked_embeds_many?(:my_untracked_embed_one_model)).to be false }
|
294
|
-
it { expect(MyTrackableModel.tracked_embeds_many?(:my_embed_many_models)).to be true }
|
295
|
-
end
|
296
|
-
|
297
|
-
describe '#tracked_embeds_many' do
|
298
|
-
it { expect(MyTrackableModel.tracked_embeds_many).to eq ['my_embed_many_models'] }
|
299
|
-
end
|
300
|
-
|
301
|
-
describe '#tracked_embeds_many_attributes' do
|
302
|
-
before :each do
|
303
|
-
class ModelOne
|
304
|
-
include Mongoid::Document
|
305
|
-
include Mongoid::History::Trackable
|
306
|
-
|
307
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
308
|
-
embeds_many :emb_ones
|
309
|
-
embeds_many :emb_twos, store_as: :emts
|
310
|
-
embeds_many :emb_threes
|
311
|
-
else
|
312
|
-
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
313
|
-
embeds_many :emb_twos, store_as: :emts, inverse_class_name: 'EmbTwo'
|
314
|
-
embeds_many :emb_threes, inverse_class_name: 'EmbThree'
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
class EmbOne
|
319
|
-
include Mongoid::Document
|
320
|
-
|
321
|
-
field :em_foo
|
322
|
-
field :em_bar
|
323
|
-
|
324
|
-
embedded_in :model_one
|
325
|
-
end
|
326
|
-
|
327
|
-
class EmbTwo
|
328
|
-
include Mongoid::Document
|
329
|
-
|
330
|
-
field :em_bar
|
331
|
-
embedded_in :model_one
|
332
|
-
end
|
333
|
-
|
334
|
-
class EmbThree
|
335
|
-
include Mongoid::Document
|
336
|
-
|
337
|
-
field :em_baz
|
338
|
-
embedded_in :model_one
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
after :each do
|
343
|
-
Object.send(:remove_const, :ModelOne)
|
344
|
-
Object.send(:remove_const, :EmbOne)
|
345
|
-
Object.send(:remove_const, :EmbTwo)
|
346
|
-
Object.send(:remove_const, :EmbThree)
|
347
|
-
end
|
348
|
-
|
349
|
-
context 'when relation tracked' do
|
350
|
-
before(:each) { ModelOne.track_history(on: :emb_ones) }
|
351
|
-
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo em_bar] }
|
352
|
-
end
|
353
|
-
|
354
|
-
context 'when relation tracked with alias' do
|
355
|
-
before(:each) { ModelOne.track_history(on: :emb_twos) }
|
356
|
-
it { expect(ModelOne.tracked_embeds_many_attributes('emb_twos')).to eq %w[_id em_bar] }
|
357
|
-
end
|
358
|
-
|
359
|
-
context 'when relation tracked with attributes' do
|
360
|
-
before(:each) { ModelOne.track_history(on: { emb_ones: :em_foo }) }
|
361
|
-
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo] }
|
362
|
-
end
|
363
|
-
|
364
|
-
context 'when relation not tracked' do
|
365
|
-
before(:each) { ModelOne.track_history(on: :fields) }
|
366
|
-
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to be_nil }
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
|
-
describe '#trackable_scope' do
|
371
|
-
before :each do
|
372
|
-
class ModelOne
|
373
|
-
include Mongoid::Document
|
374
|
-
include Mongoid::History::Trackable
|
375
|
-
|
376
|
-
store_in collection: :model_ones
|
377
|
-
|
378
|
-
track_history
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
|
-
it { expect(ModelOne.trackable_scope).to eq(:model_one) }
|
383
|
-
end
|
384
|
-
|
385
|
-
describe '#clear_trackable_memoization' do
|
386
|
-
before :each do
|
387
|
-
MyTrackableModel.instance_variable_set(:@reserved_tracked_fields, %w[_id _type])
|
388
|
-
MyTrackableModel.instance_variable_set(:@history_trackable_options, on: %w[fields])
|
389
|
-
MyTrackableModel.instance_variable_set(:@trackable_settings, paranoia_field: 'deleted_at')
|
390
|
-
MyTrackableModel.instance_variable_set(:@tracked_fields, %w[foo])
|
391
|
-
MyTrackableModel.instance_variable_set(:@tracked_embeds_one, %w[my_embed_one_model])
|
392
|
-
MyTrackableModel.instance_variable_set(:@tracked_embeds_many, %w[my_embed_many_models])
|
393
|
-
MyTrackableModel.clear_trackable_memoization
|
394
|
-
end
|
395
|
-
|
396
|
-
it 'should clear all the trackable memoization' do
|
397
|
-
expect(MyTrackableModel.instance_variable_get(:@reserved_tracked_fields)).to be_nil
|
398
|
-
expect(MyTrackableModel.instance_variable_get(:@history_trackable_options)).to be_nil
|
399
|
-
expect(MyTrackableModel.instance_variable_get(:@trackable_settings)).to be_nil
|
400
|
-
expect(MyTrackableModel.instance_variable_get(:@tracked_fields)).to be_nil
|
401
|
-
expect(MyTrackableModel.instance_variable_get(:@tracked_embeds_one)).to be_nil
|
402
|
-
expect(MyTrackableModel.instance_variable_get(:@tracked_embeds_many)).to be_nil
|
403
|
-
end
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Trackable do
|
4
|
+
describe 'SingletonMethods' do
|
5
|
+
before :each do
|
6
|
+
class MyTrackableModel
|
7
|
+
include Mongoid::Document
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
|
10
|
+
field :foo
|
11
|
+
field :b, as: :bar
|
12
|
+
|
13
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
14
|
+
embeds_one :my_embed_one_model
|
15
|
+
embeds_one :my_untracked_embed_one_model
|
16
|
+
embeds_many :my_embed_many_models
|
17
|
+
else
|
18
|
+
embeds_one :my_embed_one_model, inverse_class_name: 'MyEmbedOneModel'
|
19
|
+
embeds_one :my_untracked_embed_one_model, inverse_class_name: 'MyUntrackedEmbedOneModel'
|
20
|
+
embeds_many :my_embed_many_models, inverse_class_name: 'MyEmbedManyModel'
|
21
|
+
end
|
22
|
+
|
23
|
+
track_history on: %i[foo my_embed_one_model my_embed_many_models my_dynamic_field]
|
24
|
+
end
|
25
|
+
|
26
|
+
class MyEmbedOneModel
|
27
|
+
include Mongoid::Document
|
28
|
+
|
29
|
+
field :baz
|
30
|
+
embedded_in :my_trackable_model
|
31
|
+
end
|
32
|
+
|
33
|
+
class MyUntrackedEmbedOneModel
|
34
|
+
include Mongoid::Document
|
35
|
+
|
36
|
+
field :baz
|
37
|
+
embedded_in :my_trackable_model
|
38
|
+
end
|
39
|
+
|
40
|
+
class MyEmbedManyModel
|
41
|
+
include Mongoid::Document
|
42
|
+
|
43
|
+
field :bla
|
44
|
+
embedded_in :my_trackable_model
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
after :each do
|
49
|
+
Object.send(:remove_const, :MyTrackableModel)
|
50
|
+
Object.send(:remove_const, :MyEmbedOneModel)
|
51
|
+
Object.send(:remove_const, :MyUntrackedEmbedOneModel)
|
52
|
+
Object.send(:remove_const, :MyEmbedManyModel)
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#tracked?' do
|
56
|
+
before { allow(MyTrackableModel).to receive(:dynamic_enabled?) { false } }
|
57
|
+
it { expect(MyTrackableModel.tracked?(:foo)).to be true }
|
58
|
+
it { expect(MyTrackableModel.tracked?(:bar)).to be false }
|
59
|
+
it { expect(MyTrackableModel.tracked?(:my_embed_one_model)).to be true }
|
60
|
+
it { expect(MyTrackableModel.tracked?(:my_untracked_embed_one_model)).to be false }
|
61
|
+
it { expect(MyTrackableModel.tracked?(:my_embed_many_models)).to be true }
|
62
|
+
it { expect(MyTrackableModel.tracked?(:my_dynamic_field)).to be true }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#dynamic_field?' do
|
66
|
+
before :each do
|
67
|
+
class EmbOne
|
68
|
+
include Mongoid::Document
|
69
|
+
|
70
|
+
embedded_in :my_model
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
after :each do
|
75
|
+
Object.send(:remove_const, :EmbOne)
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when dynamic enabled' do
|
79
|
+
context 'with embeds one relation' do
|
80
|
+
before :each do
|
81
|
+
class MyModel
|
82
|
+
include Mongoid::Document
|
83
|
+
include Mongoid::History::Trackable
|
84
|
+
|
85
|
+
store_in collection: :my_models
|
86
|
+
|
87
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
88
|
+
embeds_one :emb_one
|
89
|
+
else
|
90
|
+
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
91
|
+
end
|
92
|
+
|
93
|
+
track_history
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
after :each do
|
98
|
+
Object.send(:remove_const, :MyModel)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should track dynamic field' do
|
102
|
+
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
103
|
+
expect(MyModel.dynamic_field?(:foo)).to be true
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should not track embeds_one relation' do
|
107
|
+
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
108
|
+
expect(MyModel.dynamic_field?(:emb_one)).to be false
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'with embeds one relation and alias' do
|
113
|
+
before :each do
|
114
|
+
class MyModel
|
115
|
+
include Mongoid::Document
|
116
|
+
include Mongoid::History::Trackable
|
117
|
+
|
118
|
+
store_in collection: :my_models
|
119
|
+
|
120
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
121
|
+
embeds_one :emb_one, store_as: :emo
|
122
|
+
else
|
123
|
+
embeds_one :emb_one, inverse_class_name: 'EmbOne', store_as: :emo
|
124
|
+
end
|
125
|
+
|
126
|
+
track_history
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
after :each do
|
131
|
+
Object.send(:remove_const, :MyModel)
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should not track embeds_one relation' do
|
135
|
+
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
136
|
+
expect(MyModel.dynamic_field?(:emo)).to be false
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'with embeds many relation' do
|
141
|
+
before :each do
|
142
|
+
class MyModel
|
143
|
+
include Mongoid::Document
|
144
|
+
include Mongoid::History::Trackable
|
145
|
+
|
146
|
+
store_in collection: :my_models
|
147
|
+
|
148
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
149
|
+
embeds_many :emb_ones
|
150
|
+
else
|
151
|
+
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
152
|
+
end
|
153
|
+
|
154
|
+
track_history
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
after :each do
|
159
|
+
Object.send(:remove_const, :MyModel)
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should not track embeds_many relation' do
|
163
|
+
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
164
|
+
expect(MyModel.dynamic_field?(:emb_ones)).to be false
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context 'with embeds many relation and alias' do
|
169
|
+
before :each do
|
170
|
+
class MyModel
|
171
|
+
include Mongoid::Document
|
172
|
+
include Mongoid::History::Trackable
|
173
|
+
|
174
|
+
store_in collection: :my_models
|
175
|
+
|
176
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
177
|
+
embeds_many :emb_ones, store_as: :emos
|
178
|
+
else
|
179
|
+
embeds_many :emb_ones, store_as: :emos, inverse_class_name: 'EmbOne'
|
180
|
+
end
|
181
|
+
track_history
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
after :each do
|
186
|
+
Object.send(:remove_const, :MyModel)
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'should not track embeds_many relation' do
|
190
|
+
allow(MyModel).to receive(:dynamic_enabled?) { true }
|
191
|
+
expect(MyModel.dynamic_field?(:emos)).to be false
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe '#tracked_fields' do
|
198
|
+
it 'should include fields and dynamic fields' do
|
199
|
+
expect(MyTrackableModel.tracked_fields).to eq %w[foo my_dynamic_field]
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe '#tracked_relation?' do
|
204
|
+
it 'should return true if a relation is tracked' do
|
205
|
+
expect(MyTrackableModel.tracked_relation?(:my_embed_one_model)).to be true
|
206
|
+
expect(MyTrackableModel.tracked_relation?(:my_untracked_embed_one_model)).to be false
|
207
|
+
expect(MyTrackableModel.tracked_relation?(:my_embed_many_models)).to be true
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe '#tracked_embeds_one?' do
|
212
|
+
it { expect(MyTrackableModel.tracked_embeds_one?(:my_embed_one_model)).to be true }
|
213
|
+
it { expect(MyTrackableModel.tracked_embeds_one?(:my_untracked_embed_one_model)).to be false }
|
214
|
+
it { expect(MyTrackableModel.tracked_embeds_one?(:my_embed_many_models)).to be false }
|
215
|
+
end
|
216
|
+
|
217
|
+
describe '#tracked_embeds_one' do
|
218
|
+
it { expect(MyTrackableModel.tracked_embeds_one).to include 'my_embed_one_model' }
|
219
|
+
it { expect(MyTrackableModel.tracked_embeds_one).to_not include 'my_untracked_embed_one_model' }
|
220
|
+
end
|
221
|
+
|
222
|
+
describe '#tracked_embeds_one_attributes' do
|
223
|
+
before :each do
|
224
|
+
class ModelOne
|
225
|
+
include Mongoid::Document
|
226
|
+
include Mongoid::History::Trackable
|
227
|
+
|
228
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
229
|
+
embeds_one :emb_one
|
230
|
+
embeds_one :emb_two, store_as: :emt
|
231
|
+
embeds_one :emb_three
|
232
|
+
else
|
233
|
+
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
234
|
+
embeds_one :emb_two, store_as: :emt, inverse_class_name: 'EmbTwo'
|
235
|
+
embeds_one :emb_three, inverse_class_name: 'EmbThree'
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
class EmbOne
|
240
|
+
include Mongoid::Document
|
241
|
+
|
242
|
+
field :em_foo
|
243
|
+
field :em_bar
|
244
|
+
|
245
|
+
embedded_in :model_one
|
246
|
+
end
|
247
|
+
|
248
|
+
class EmbTwo
|
249
|
+
include Mongoid::Document
|
250
|
+
|
251
|
+
field :em_bar
|
252
|
+
embedded_in :model_one
|
253
|
+
end
|
254
|
+
|
255
|
+
class EmbThree
|
256
|
+
include Mongoid::Document
|
257
|
+
|
258
|
+
field :em_baz
|
259
|
+
embedded_in :model_one
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
after :each do
|
264
|
+
Object.send(:remove_const, :ModelOne)
|
265
|
+
Object.send(:remove_const, :EmbOne)
|
266
|
+
Object.send(:remove_const, :EmbTwo)
|
267
|
+
Object.send(:remove_const, :EmbThree)
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'when relation tracked' do
|
271
|
+
before(:each) { ModelOne.track_history(on: :emb_one) }
|
272
|
+
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo em_bar] }
|
273
|
+
end
|
274
|
+
|
275
|
+
context 'when relation tracked with alias' do
|
276
|
+
before(:each) { ModelOne.track_history(on: :emb_two) }
|
277
|
+
it { expect(ModelOne.tracked_embeds_one_attributes('emb_two')).to eq %w[_id em_bar] }
|
278
|
+
end
|
279
|
+
|
280
|
+
context 'when relation tracked with attributes' do
|
281
|
+
before(:each) { ModelOne.track_history(on: { emb_one: :em_foo }) }
|
282
|
+
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo] }
|
283
|
+
end
|
284
|
+
|
285
|
+
context 'when relation not tracked' do
|
286
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
287
|
+
it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to be_nil }
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe '#tracked_embeds_many?' do
|
292
|
+
it { expect(MyTrackableModel.tracked_embeds_many?(:my_embed_one_model)).to be false }
|
293
|
+
it { expect(MyTrackableModel.tracked_embeds_many?(:my_untracked_embed_one_model)).to be false }
|
294
|
+
it { expect(MyTrackableModel.tracked_embeds_many?(:my_embed_many_models)).to be true }
|
295
|
+
end
|
296
|
+
|
297
|
+
describe '#tracked_embeds_many' do
|
298
|
+
it { expect(MyTrackableModel.tracked_embeds_many).to eq ['my_embed_many_models'] }
|
299
|
+
end
|
300
|
+
|
301
|
+
describe '#tracked_embeds_many_attributes' do
|
302
|
+
before :each do
|
303
|
+
class ModelOne
|
304
|
+
include Mongoid::Document
|
305
|
+
include Mongoid::History::Trackable
|
306
|
+
|
307
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
308
|
+
embeds_many :emb_ones
|
309
|
+
embeds_many :emb_twos, store_as: :emts
|
310
|
+
embeds_many :emb_threes
|
311
|
+
else
|
312
|
+
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
313
|
+
embeds_many :emb_twos, store_as: :emts, inverse_class_name: 'EmbTwo'
|
314
|
+
embeds_many :emb_threes, inverse_class_name: 'EmbThree'
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
class EmbOne
|
319
|
+
include Mongoid::Document
|
320
|
+
|
321
|
+
field :em_foo
|
322
|
+
field :em_bar
|
323
|
+
|
324
|
+
embedded_in :model_one
|
325
|
+
end
|
326
|
+
|
327
|
+
class EmbTwo
|
328
|
+
include Mongoid::Document
|
329
|
+
|
330
|
+
field :em_bar
|
331
|
+
embedded_in :model_one
|
332
|
+
end
|
333
|
+
|
334
|
+
class EmbThree
|
335
|
+
include Mongoid::Document
|
336
|
+
|
337
|
+
field :em_baz
|
338
|
+
embedded_in :model_one
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
after :each do
|
343
|
+
Object.send(:remove_const, :ModelOne)
|
344
|
+
Object.send(:remove_const, :EmbOne)
|
345
|
+
Object.send(:remove_const, :EmbTwo)
|
346
|
+
Object.send(:remove_const, :EmbThree)
|
347
|
+
end
|
348
|
+
|
349
|
+
context 'when relation tracked' do
|
350
|
+
before(:each) { ModelOne.track_history(on: :emb_ones) }
|
351
|
+
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo em_bar] }
|
352
|
+
end
|
353
|
+
|
354
|
+
context 'when relation tracked with alias' do
|
355
|
+
before(:each) { ModelOne.track_history(on: :emb_twos) }
|
356
|
+
it { expect(ModelOne.tracked_embeds_many_attributes('emb_twos')).to eq %w[_id em_bar] }
|
357
|
+
end
|
358
|
+
|
359
|
+
context 'when relation tracked with attributes' do
|
360
|
+
before(:each) { ModelOne.track_history(on: { emb_ones: :em_foo }) }
|
361
|
+
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo] }
|
362
|
+
end
|
363
|
+
|
364
|
+
context 'when relation not tracked' do
|
365
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
366
|
+
it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to be_nil }
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
describe '#trackable_scope' do
|
371
|
+
before :each do
|
372
|
+
class ModelOne
|
373
|
+
include Mongoid::Document
|
374
|
+
include Mongoid::History::Trackable
|
375
|
+
|
376
|
+
store_in collection: :model_ones
|
377
|
+
|
378
|
+
track_history
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
it { expect(ModelOne.trackable_scope).to eq(:model_one) }
|
383
|
+
end
|
384
|
+
|
385
|
+
describe '#clear_trackable_memoization' do
|
386
|
+
before :each do
|
387
|
+
MyTrackableModel.instance_variable_set(:@reserved_tracked_fields, %w[_id _type])
|
388
|
+
MyTrackableModel.instance_variable_set(:@history_trackable_options, on: %w[fields])
|
389
|
+
MyTrackableModel.instance_variable_set(:@trackable_settings, paranoia_field: 'deleted_at')
|
390
|
+
MyTrackableModel.instance_variable_set(:@tracked_fields, %w[foo])
|
391
|
+
MyTrackableModel.instance_variable_set(:@tracked_embeds_one, %w[my_embed_one_model])
|
392
|
+
MyTrackableModel.instance_variable_set(:@tracked_embeds_many, %w[my_embed_many_models])
|
393
|
+
MyTrackableModel.clear_trackable_memoization
|
394
|
+
end
|
395
|
+
|
396
|
+
it 'should clear all the trackable memoization' do
|
397
|
+
expect(MyTrackableModel.instance_variable_get(:@reserved_tracked_fields)).to be_nil
|
398
|
+
expect(MyTrackableModel.instance_variable_get(:@history_trackable_options)).to be_nil
|
399
|
+
expect(MyTrackableModel.instance_variable_get(:@trackable_settings)).to be_nil
|
400
|
+
expect(MyTrackableModel.instance_variable_get(:@tracked_fields)).to be_nil
|
401
|
+
expect(MyTrackableModel.instance_variable_get(:@tracked_embeds_one)).to be_nil
|
402
|
+
expect(MyTrackableModel.instance_variable_get(:@tracked_embeds_many)).to be_nil
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|