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,342 +1,342 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Attributes::Update do
|
4
|
-
describe '#attributes' do
|
5
|
-
describe '#insert_embeds_one_changes' do
|
6
|
-
context 'Case: relation without alias' do
|
7
|
-
before :each do
|
8
|
-
class ModelOne
|
9
|
-
include Mongoid::Document
|
10
|
-
include Mongoid::History::Trackable
|
11
|
-
|
12
|
-
store_in collection: :model_ones
|
13
|
-
embeds_one :emb_one
|
14
|
-
|
15
|
-
track_history on: :fields
|
16
|
-
end
|
17
|
-
|
18
|
-
class EmbOne
|
19
|
-
include Mongoid::Document
|
20
|
-
|
21
|
-
field :em_foo
|
22
|
-
field :em_bar
|
23
|
-
|
24
|
-
embedded_in :model_one
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
after :each do
|
29
|
-
Object.send(:remove_const, :ModelOne)
|
30
|
-
Object.send(:remove_const, :EmbOne)
|
31
|
-
end
|
32
|
-
|
33
|
-
before :each do
|
34
|
-
allow(base).to receive(:changes) { changes }
|
35
|
-
end
|
36
|
-
|
37
|
-
let(:obj_one) { ModelOne.new }
|
38
|
-
let(:base) { described_class.new(obj_one) }
|
39
|
-
let(:changes) do
|
40
|
-
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
41
|
-
end
|
42
|
-
subject { base.attributes }
|
43
|
-
|
44
|
-
context 'with permitted attributes' do
|
45
|
-
before :each do
|
46
|
-
ModelOne.track_history on: { emb_one: :em_foo }
|
47
|
-
end
|
48
|
-
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new' }] }
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'without permitted attributes' do
|
52
|
-
before :each do
|
53
|
-
ModelOne.track_history on: :emb_one
|
54
|
-
end
|
55
|
-
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'when old value soft-deleted' do
|
59
|
-
before :each do
|
60
|
-
ModelOne.track_history on: :emb_one
|
61
|
-
end
|
62
|
-
let(:changes) do
|
63
|
-
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
64
|
-
end
|
65
|
-
it { expect(subject['emb_one']).to eq [{}, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'when new value soft-deleted' do
|
69
|
-
before :each do
|
70
|
-
ModelOne.track_history on: :emb_one
|
71
|
-
end
|
72
|
-
let(:changes) do
|
73
|
-
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'deleted_at' => Time.now }] }
|
74
|
-
end
|
75
|
-
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, {}] }
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'when not tracked' do
|
79
|
-
before :each do
|
80
|
-
ModelOne.track_history on: :fields
|
81
|
-
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
82
|
-
end
|
83
|
-
it { expect(subject['emb_one']).to be_nil }
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context 'Case: relation with alias' do
|
88
|
-
before :each do
|
89
|
-
class ModelOne
|
90
|
-
include Mongoid::Document
|
91
|
-
include Mongoid::History::Trackable
|
92
|
-
store_in collection: :model_ones
|
93
|
-
embeds_one :emb_one, store_as: :eon
|
94
|
-
track_history on: :fields
|
95
|
-
end
|
96
|
-
|
97
|
-
class EmbOne
|
98
|
-
include Mongoid::Document
|
99
|
-
field :em_foo
|
100
|
-
field :em_bar
|
101
|
-
embedded_in :model_one
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
after :each do
|
106
|
-
Object.send(:remove_const, :ModelOne)
|
107
|
-
Object.send(:remove_const, :EmbOne)
|
108
|
-
end
|
109
|
-
|
110
|
-
before :each do
|
111
|
-
ModelOne.track_history on: :emb_one
|
112
|
-
allow(base).to receive(:changes) { changes }
|
113
|
-
end
|
114
|
-
|
115
|
-
let(:obj_one) { ModelOne.new }
|
116
|
-
let(:base) { described_class.new(obj_one) }
|
117
|
-
let(:changes) do
|
118
|
-
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
119
|
-
end
|
120
|
-
subject { base.attributes }
|
121
|
-
it { expect(subject['eon']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
122
|
-
end
|
123
|
-
|
124
|
-
context 'when original and modified value same' do
|
125
|
-
before :each do
|
126
|
-
class DummyUpdateModel
|
127
|
-
include Mongoid::Document
|
128
|
-
include Mongoid::History::Trackable
|
129
|
-
store_in collection: :dummy_update_models
|
130
|
-
embeds_one :dummy_embedded_model
|
131
|
-
track_history on: :fields
|
132
|
-
end
|
133
|
-
|
134
|
-
class DummyEmbeddedModel
|
135
|
-
include Mongoid::Document
|
136
|
-
field :em_foo
|
137
|
-
field :em_bar
|
138
|
-
embedded_in :dummy_update_model
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
after :each do
|
143
|
-
Object.send(:remove_const, :DummyUpdateModel)
|
144
|
-
Object.send(:remove_const, :DummyEmbeddedModel)
|
145
|
-
end
|
146
|
-
|
147
|
-
before :each do
|
148
|
-
allow(base).to receive(:changes) { changes }
|
149
|
-
DummyUpdateModel.track_history on: :dummy_embedded_model
|
150
|
-
end
|
151
|
-
|
152
|
-
let(:obj_one) { DummyUpdateModel.new }
|
153
|
-
let(:base) { described_class.new(obj_one) }
|
154
|
-
let(:changes) do
|
155
|
-
{ 'dummy_embedded_model' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }] }
|
156
|
-
end
|
157
|
-
subject { base.attributes }
|
158
|
-
it { expect(subject.keys).to_not include 'dummy_embedded_model' }
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
describe '#insert_embeds_many_changes' do
|
163
|
-
context 'Case: relation without alias' do
|
164
|
-
before :each do
|
165
|
-
class ModelOne
|
166
|
-
include Mongoid::Document
|
167
|
-
include Mongoid::History::Trackable
|
168
|
-
store_in collection: :model_ones
|
169
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
170
|
-
embeds_many :emb_ones
|
171
|
-
else
|
172
|
-
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
173
|
-
end
|
174
|
-
track_history on: :fields
|
175
|
-
end
|
176
|
-
|
177
|
-
class EmbOne
|
178
|
-
include Mongoid::Document
|
179
|
-
field :em_foo
|
180
|
-
field :em_bar
|
181
|
-
embedded_in :model_one
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
before :each do
|
186
|
-
allow(base).to receive(:changes) { changes }
|
187
|
-
end
|
188
|
-
|
189
|
-
let(:obj_one) { ModelOne.new }
|
190
|
-
let(:base) { described_class.new(obj_one) }
|
191
|
-
subject { base.attributes }
|
192
|
-
|
193
|
-
context 'with whitelist attributes' do
|
194
|
-
before :each do
|
195
|
-
ModelOne.track_history on: { emb_ones: :em_foo }
|
196
|
-
end
|
197
|
-
let(:changes) do
|
198
|
-
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
199
|
-
end
|
200
|
-
it 'should track only whitelisted attributes' do
|
201
|
-
expect(subject['emb_ones']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new' }]]
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
context 'without whitelist attributes' do
|
206
|
-
before :each do
|
207
|
-
ModelOne.track_history(on: :emb_ones)
|
208
|
-
end
|
209
|
-
let(:changes) do
|
210
|
-
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
211
|
-
end
|
212
|
-
it 'should ignore soft-deleted objects' do
|
213
|
-
expect(subject['emb_ones']).to eq [[], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
after :each do
|
218
|
-
Object.send(:remove_const, :ModelOne)
|
219
|
-
Object.send(:remove_const, :EmbOne)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
context 'Case: relation with alias' do
|
224
|
-
before :each do
|
225
|
-
class ModelOne
|
226
|
-
include Mongoid::Document
|
227
|
-
include Mongoid::History::Trackable
|
228
|
-
store_in collection: :model_ones
|
229
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
230
|
-
embeds_many :emb_ones, store_as: :eons
|
231
|
-
else
|
232
|
-
embeds_many :emb_ones, store_as: :eons, inverse_class_name: 'EmbOne'
|
233
|
-
end
|
234
|
-
track_history on: :fields
|
235
|
-
end
|
236
|
-
|
237
|
-
class EmbOne
|
238
|
-
include Mongoid::Document
|
239
|
-
field :em_foo
|
240
|
-
field :em_bar
|
241
|
-
embedded_in :model_one
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
before :each do
|
246
|
-
ModelOne.track_history on: :emb_ones
|
247
|
-
allow(base).to receive(:changes) { changes }
|
248
|
-
end
|
249
|
-
|
250
|
-
let(:obj_one) { ModelOne.new }
|
251
|
-
let(:base) { described_class.new(obj_one) }
|
252
|
-
let(:changes) do
|
253
|
-
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
254
|
-
end
|
255
|
-
subject { base.attributes }
|
256
|
-
it 'should save audit history under relation alias' do
|
257
|
-
expect(subject['eons']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
|
258
|
-
end
|
259
|
-
|
260
|
-
after :each do
|
261
|
-
Object.send(:remove_const, :ModelOne)
|
262
|
-
Object.send(:remove_const, :EmbOne)
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
context 'when original and modified value same' do
|
267
|
-
before :each do
|
268
|
-
class ModelOne
|
269
|
-
include Mongoid::Document
|
270
|
-
include Mongoid::History::Trackable
|
271
|
-
store_in collection: :model_ones
|
272
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
273
|
-
embeds_many :emb_ones
|
274
|
-
else
|
275
|
-
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
276
|
-
end
|
277
|
-
track_history on: :fields
|
278
|
-
end
|
279
|
-
|
280
|
-
class EmbOne
|
281
|
-
include Mongoid::Document
|
282
|
-
field :em_foo
|
283
|
-
field :em_bar
|
284
|
-
embedded_in :model_one
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
before :each do
|
289
|
-
allow(base).to receive(:changes) { changes }
|
290
|
-
ModelOne.track_history on: :emb_ones
|
291
|
-
end
|
292
|
-
|
293
|
-
let(:obj_one) { ModelOne.new }
|
294
|
-
let(:base) { described_class.new(obj_one) }
|
295
|
-
let(:changes) do
|
296
|
-
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }]] }
|
297
|
-
end
|
298
|
-
subject { base.attributes }
|
299
|
-
it { expect(subject.keys).to_not include 'emb_ones' }
|
300
|
-
|
301
|
-
after :each do
|
302
|
-
Object.send(:remove_const, :ModelOne)
|
303
|
-
Object.send(:remove_const, :EmbOne)
|
304
|
-
end
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
context 'when original and modified values blank' do
|
309
|
-
before :each do
|
310
|
-
class DummyParent
|
311
|
-
include Mongoid::Document
|
312
|
-
include Mongoid::History::Trackable
|
313
|
-
store_in collection: :dummy_parents
|
314
|
-
has_and_belongs_to_many :other_dummy_parents
|
315
|
-
track_history on: :fields
|
316
|
-
end
|
317
|
-
|
318
|
-
class OtherDummyParent
|
319
|
-
include Mongoid::Document
|
320
|
-
has_and_belongs_to_many :dummy_parents
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
before :each do
|
325
|
-
allow(base).to receive(:changes) { changes }
|
326
|
-
DummyParent.track_history on: :other_dummy_parent_ids
|
327
|
-
end
|
328
|
-
|
329
|
-
let(:base) { described_class.new(DummyParent.new) }
|
330
|
-
let(:changes) do
|
331
|
-
{ 'other_dummy_parent_ids' => [nil, []] }
|
332
|
-
end
|
333
|
-
subject { base.attributes }
|
334
|
-
it { expect(subject.keys).to_not include 'other_dummy_parent_ids' }
|
335
|
-
|
336
|
-
after :each do
|
337
|
-
Object.send(:remove_const, :DummyParent)
|
338
|
-
Object.send(:remove_const, :OtherDummyParent)
|
339
|
-
end
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Attributes::Update do
|
4
|
+
describe '#attributes' do
|
5
|
+
describe '#insert_embeds_one_changes' do
|
6
|
+
context 'Case: relation without alias' do
|
7
|
+
before :each do
|
8
|
+
class ModelOne
|
9
|
+
include Mongoid::Document
|
10
|
+
include Mongoid::History::Trackable
|
11
|
+
|
12
|
+
store_in collection: :model_ones
|
13
|
+
embeds_one :emb_one
|
14
|
+
|
15
|
+
track_history on: :fields
|
16
|
+
end
|
17
|
+
|
18
|
+
class EmbOne
|
19
|
+
include Mongoid::Document
|
20
|
+
|
21
|
+
field :em_foo
|
22
|
+
field :em_bar
|
23
|
+
|
24
|
+
embedded_in :model_one
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
after :each do
|
29
|
+
Object.send(:remove_const, :ModelOne)
|
30
|
+
Object.send(:remove_const, :EmbOne)
|
31
|
+
end
|
32
|
+
|
33
|
+
before :each do
|
34
|
+
allow(base).to receive(:changes) { changes }
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:obj_one) { ModelOne.new }
|
38
|
+
let(:base) { described_class.new(obj_one) }
|
39
|
+
let(:changes) do
|
40
|
+
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
41
|
+
end
|
42
|
+
subject { base.attributes }
|
43
|
+
|
44
|
+
context 'with permitted attributes' do
|
45
|
+
before :each do
|
46
|
+
ModelOne.track_history on: { emb_one: :em_foo }
|
47
|
+
end
|
48
|
+
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new' }] }
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'without permitted attributes' do
|
52
|
+
before :each do
|
53
|
+
ModelOne.track_history on: :emb_one
|
54
|
+
end
|
55
|
+
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when old value soft-deleted' do
|
59
|
+
before :each do
|
60
|
+
ModelOne.track_history on: :emb_one
|
61
|
+
end
|
62
|
+
let(:changes) do
|
63
|
+
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
64
|
+
end
|
65
|
+
it { expect(subject['emb_one']).to eq [{}, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when new value soft-deleted' do
|
69
|
+
before :each do
|
70
|
+
ModelOne.track_history on: :emb_one
|
71
|
+
end
|
72
|
+
let(:changes) do
|
73
|
+
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'deleted_at' => Time.now }] }
|
74
|
+
end
|
75
|
+
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, {}] }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when not tracked' do
|
79
|
+
before :each do
|
80
|
+
ModelOne.track_history on: :fields
|
81
|
+
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
82
|
+
end
|
83
|
+
it { expect(subject['emb_one']).to be_nil }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'Case: relation with alias' do
|
88
|
+
before :each do
|
89
|
+
class ModelOne
|
90
|
+
include Mongoid::Document
|
91
|
+
include Mongoid::History::Trackable
|
92
|
+
store_in collection: :model_ones
|
93
|
+
embeds_one :emb_one, store_as: :eon
|
94
|
+
track_history on: :fields
|
95
|
+
end
|
96
|
+
|
97
|
+
class EmbOne
|
98
|
+
include Mongoid::Document
|
99
|
+
field :em_foo
|
100
|
+
field :em_bar
|
101
|
+
embedded_in :model_one
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
after :each do
|
106
|
+
Object.send(:remove_const, :ModelOne)
|
107
|
+
Object.send(:remove_const, :EmbOne)
|
108
|
+
end
|
109
|
+
|
110
|
+
before :each do
|
111
|
+
ModelOne.track_history on: :emb_one
|
112
|
+
allow(base).to receive(:changes) { changes }
|
113
|
+
end
|
114
|
+
|
115
|
+
let(:obj_one) { ModelOne.new }
|
116
|
+
let(:base) { described_class.new(obj_one) }
|
117
|
+
let(:changes) do
|
118
|
+
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
119
|
+
end
|
120
|
+
subject { base.attributes }
|
121
|
+
it { expect(subject['eon']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'when original and modified value same' do
|
125
|
+
before :each do
|
126
|
+
class DummyUpdateModel
|
127
|
+
include Mongoid::Document
|
128
|
+
include Mongoid::History::Trackable
|
129
|
+
store_in collection: :dummy_update_models
|
130
|
+
embeds_one :dummy_embedded_model
|
131
|
+
track_history on: :fields
|
132
|
+
end
|
133
|
+
|
134
|
+
class DummyEmbeddedModel
|
135
|
+
include Mongoid::Document
|
136
|
+
field :em_foo
|
137
|
+
field :em_bar
|
138
|
+
embedded_in :dummy_update_model
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
after :each do
|
143
|
+
Object.send(:remove_const, :DummyUpdateModel)
|
144
|
+
Object.send(:remove_const, :DummyEmbeddedModel)
|
145
|
+
end
|
146
|
+
|
147
|
+
before :each do
|
148
|
+
allow(base).to receive(:changes) { changes }
|
149
|
+
DummyUpdateModel.track_history on: :dummy_embedded_model
|
150
|
+
end
|
151
|
+
|
152
|
+
let(:obj_one) { DummyUpdateModel.new }
|
153
|
+
let(:base) { described_class.new(obj_one) }
|
154
|
+
let(:changes) do
|
155
|
+
{ 'dummy_embedded_model' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }] }
|
156
|
+
end
|
157
|
+
subject { base.attributes }
|
158
|
+
it { expect(subject.keys).to_not include 'dummy_embedded_model' }
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#insert_embeds_many_changes' do
|
163
|
+
context 'Case: relation without alias' do
|
164
|
+
before :each do
|
165
|
+
class ModelOne
|
166
|
+
include Mongoid::Document
|
167
|
+
include Mongoid::History::Trackable
|
168
|
+
store_in collection: :model_ones
|
169
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
170
|
+
embeds_many :emb_ones
|
171
|
+
else
|
172
|
+
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
173
|
+
end
|
174
|
+
track_history on: :fields
|
175
|
+
end
|
176
|
+
|
177
|
+
class EmbOne
|
178
|
+
include Mongoid::Document
|
179
|
+
field :em_foo
|
180
|
+
field :em_bar
|
181
|
+
embedded_in :model_one
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
before :each do
|
186
|
+
allow(base).to receive(:changes) { changes }
|
187
|
+
end
|
188
|
+
|
189
|
+
let(:obj_one) { ModelOne.new }
|
190
|
+
let(:base) { described_class.new(obj_one) }
|
191
|
+
subject { base.attributes }
|
192
|
+
|
193
|
+
context 'with whitelist attributes' do
|
194
|
+
before :each do
|
195
|
+
ModelOne.track_history on: { emb_ones: :em_foo }
|
196
|
+
end
|
197
|
+
let(:changes) do
|
198
|
+
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
199
|
+
end
|
200
|
+
it 'should track only whitelisted attributes' do
|
201
|
+
expect(subject['emb_ones']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new' }]]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
context 'without whitelist attributes' do
|
206
|
+
before :each do
|
207
|
+
ModelOne.track_history(on: :emb_ones)
|
208
|
+
end
|
209
|
+
let(:changes) do
|
210
|
+
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
211
|
+
end
|
212
|
+
it 'should ignore soft-deleted objects' do
|
213
|
+
expect(subject['emb_ones']).to eq [[], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
after :each do
|
218
|
+
Object.send(:remove_const, :ModelOne)
|
219
|
+
Object.send(:remove_const, :EmbOne)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'Case: relation with alias' do
|
224
|
+
before :each do
|
225
|
+
class ModelOne
|
226
|
+
include Mongoid::Document
|
227
|
+
include Mongoid::History::Trackable
|
228
|
+
store_in collection: :model_ones
|
229
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
230
|
+
embeds_many :emb_ones, store_as: :eons
|
231
|
+
else
|
232
|
+
embeds_many :emb_ones, store_as: :eons, inverse_class_name: 'EmbOne'
|
233
|
+
end
|
234
|
+
track_history on: :fields
|
235
|
+
end
|
236
|
+
|
237
|
+
class EmbOne
|
238
|
+
include Mongoid::Document
|
239
|
+
field :em_foo
|
240
|
+
field :em_bar
|
241
|
+
embedded_in :model_one
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
before :each do
|
246
|
+
ModelOne.track_history on: :emb_ones
|
247
|
+
allow(base).to receive(:changes) { changes }
|
248
|
+
end
|
249
|
+
|
250
|
+
let(:obj_one) { ModelOne.new }
|
251
|
+
let(:base) { described_class.new(obj_one) }
|
252
|
+
let(:changes) do
|
253
|
+
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
|
254
|
+
end
|
255
|
+
subject { base.attributes }
|
256
|
+
it 'should save audit history under relation alias' do
|
257
|
+
expect(subject['eons']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
|
258
|
+
end
|
259
|
+
|
260
|
+
after :each do
|
261
|
+
Object.send(:remove_const, :ModelOne)
|
262
|
+
Object.send(:remove_const, :EmbOne)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'when original and modified value same' do
|
267
|
+
before :each do
|
268
|
+
class ModelOne
|
269
|
+
include Mongoid::Document
|
270
|
+
include Mongoid::History::Trackable
|
271
|
+
store_in collection: :model_ones
|
272
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
273
|
+
embeds_many :emb_ones
|
274
|
+
else
|
275
|
+
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
|
276
|
+
end
|
277
|
+
track_history on: :fields
|
278
|
+
end
|
279
|
+
|
280
|
+
class EmbOne
|
281
|
+
include Mongoid::Document
|
282
|
+
field :em_foo
|
283
|
+
field :em_bar
|
284
|
+
embedded_in :model_one
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
before :each do
|
289
|
+
allow(base).to receive(:changes) { changes }
|
290
|
+
ModelOne.track_history on: :emb_ones
|
291
|
+
end
|
292
|
+
|
293
|
+
let(:obj_one) { ModelOne.new }
|
294
|
+
let(:base) { described_class.new(obj_one) }
|
295
|
+
let(:changes) do
|
296
|
+
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }]] }
|
297
|
+
end
|
298
|
+
subject { base.attributes }
|
299
|
+
it { expect(subject.keys).to_not include 'emb_ones' }
|
300
|
+
|
301
|
+
after :each do
|
302
|
+
Object.send(:remove_const, :ModelOne)
|
303
|
+
Object.send(:remove_const, :EmbOne)
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context 'when original and modified values blank' do
|
309
|
+
before :each do
|
310
|
+
class DummyParent
|
311
|
+
include Mongoid::Document
|
312
|
+
include Mongoid::History::Trackable
|
313
|
+
store_in collection: :dummy_parents
|
314
|
+
has_and_belongs_to_many :other_dummy_parents
|
315
|
+
track_history on: :fields
|
316
|
+
end
|
317
|
+
|
318
|
+
class OtherDummyParent
|
319
|
+
include Mongoid::Document
|
320
|
+
has_and_belongs_to_many :dummy_parents
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
before :each do
|
325
|
+
allow(base).to receive(:changes) { changes }
|
326
|
+
DummyParent.track_history on: :other_dummy_parent_ids
|
327
|
+
end
|
328
|
+
|
329
|
+
let(:base) { described_class.new(DummyParent.new) }
|
330
|
+
let(:changes) do
|
331
|
+
{ 'other_dummy_parent_ids' => [nil, []] }
|
332
|
+
end
|
333
|
+
subject { base.attributes }
|
334
|
+
it { expect(subject.keys).to_not include 'other_dummy_parent_ids' }
|
335
|
+
|
336
|
+
after :each do
|
337
|
+
Object.send(:remove_const, :DummyParent)
|
338
|
+
Object.send(:remove_const, :OtherDummyParent)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|