mongoid-history 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +16 -3
- data/.travis.yml +23 -8
- data/CHANGELOG.md +20 -6
- data/Dangerfile +1 -0
- data/Gemfile +22 -1
- data/LICENSE.txt +1 -1
- data/README.md +130 -18
- data/lib/mongoid/history.rb +32 -15
- data/lib/mongoid/history/attributes/base.rb +31 -0
- data/lib/mongoid/history/attributes/create.rb +52 -0
- data/lib/mongoid/history/attributes/destroy.rb +36 -0
- data/lib/mongoid/history/attributes/update.rb +43 -0
- data/lib/mongoid/history/options.rb +153 -0
- data/lib/mongoid/history/trackable.rb +170 -72
- data/lib/mongoid/history/tracker.rb +39 -23
- data/lib/mongoid/history/version.rb +1 -1
- data/mongoid-history.gemspec +0 -8
- data/spec/integration/embedded_in_polymorphic_spec.rb +8 -0
- data/spec/integration/integration_spec.rb +4 -0
- data/spec/integration/nested_embedded_documents_spec.rb +13 -6
- data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +24 -24
- data/spec/spec_helper.rb +6 -0
- data/spec/unit/attributes/base_spec.rb +54 -0
- data/spec/unit/attributes/create_spec.rb +315 -0
- data/spec/unit/attributes/destroy_spec.rb +218 -0
- data/spec/unit/attributes/update_spec.rb +210 -0
- data/spec/unit/embedded_methods_spec.rb +69 -0
- data/spec/unit/history_spec.rb +35 -0
- data/spec/unit/my_instance_methods_spec.rb +485 -0
- data/spec/unit/options_spec.rb +294 -0
- data/spec/unit/singleton_methods_spec.rb +338 -0
- data/spec/unit/store/default_store_spec.rb +11 -0
- data/spec/unit/store/request_store_spec.rb +13 -0
- data/spec/unit/trackable_spec.rb +335 -68
- data/spec/unit/tracker_spec.rb +153 -0
- metadata +31 -102
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History do
|
4
|
+
it { is_expected.to respond_to(:trackable_settings) }
|
5
|
+
it { is_expected.to respond_to(:trackable_settings=) }
|
6
|
+
|
7
|
+
describe '#default_settings' do
|
8
|
+
let(:default_settings) { { paranoia_field: 'deleted_at' } }
|
9
|
+
it { expect(described_class.default_settings).to eq(default_settings) }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#trackable_class_settings' do
|
13
|
+
before(:each) { Mongoid::History.trackable_settings = {} }
|
14
|
+
let(:model_one) do
|
15
|
+
Class.new do
|
16
|
+
include Mongoid::Document
|
17
|
+
include Mongoid::History::Trackable
|
18
|
+
store_in collection: :model_ones
|
19
|
+
|
20
|
+
def self.name
|
21
|
+
'ModelOne'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when present' do
|
27
|
+
before(:each) { model_one.history_settings paranoia_field: :annuled_at }
|
28
|
+
it { expect(described_class.trackable_class_settings(model_one)).to eq(paranoia_field: 'annuled_at') }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when not present' do
|
32
|
+
it { expect(described_class.trackable_class_settings(model_one)).to eq(paranoia_field: 'deleted_at') }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,485 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Trackable do
|
4
|
+
describe 'MyInstanceMethods' do
|
5
|
+
before :all do
|
6
|
+
ModelOne = Class.new do
|
7
|
+
include Mongoid::Document
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
store_in collection: :model_ones
|
10
|
+
field :foo
|
11
|
+
field :b, as: :bar
|
12
|
+
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
13
|
+
embeds_one :emb_two, store_as: :emt, inverse_class_name: 'EmbTwo'
|
14
|
+
embeds_many :emb_threes, inverse_class_name: 'EmbThree'
|
15
|
+
embeds_many :emb_fours, store_as: :emfs, inverse_class_name: 'EmbFour'
|
16
|
+
end
|
17
|
+
|
18
|
+
EmbOne = Class.new do
|
19
|
+
include Mongoid::Document
|
20
|
+
include Mongoid::History::Trackable
|
21
|
+
field :f_em_foo
|
22
|
+
field :fmb, as: :f_em_bar
|
23
|
+
embedded_in :model_one
|
24
|
+
end
|
25
|
+
|
26
|
+
EmbTwo = Class.new do
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::History::Trackable
|
29
|
+
field :baz
|
30
|
+
embedded_in :model_one
|
31
|
+
end
|
32
|
+
|
33
|
+
EmbThree = Class.new do
|
34
|
+
include Mongoid::Document
|
35
|
+
include Mongoid::History::Trackable
|
36
|
+
field :f_em_foo
|
37
|
+
field :fmb, as: :f_em_bar
|
38
|
+
embedded_in :model_one
|
39
|
+
end
|
40
|
+
|
41
|
+
EmbFour = Class.new do
|
42
|
+
include Mongoid::Document
|
43
|
+
include Mongoid::History::Trackable
|
44
|
+
field :baz
|
45
|
+
embedded_in :model_one
|
46
|
+
end
|
47
|
+
|
48
|
+
ModelOne.track_history(on: %i(foo emb_one emb_threes))
|
49
|
+
@persisted_history_options = Mongoid::History.trackable_class_options
|
50
|
+
end
|
51
|
+
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
52
|
+
|
53
|
+
let(:bson_class) { defined?(BSON::ObjectId) ? BSON::ObjectId : Moped::BSON::ObjectId }
|
54
|
+
|
55
|
+
let(:emb_one) { EmbOne.new(f_em_foo: 'Foo', f_em_bar: 'Bar') }
|
56
|
+
let(:emb_threes) { [EmbThree.new(f_em_foo: 'Foo', f_em_bar: 'Bar')] }
|
57
|
+
let(:model_one) do
|
58
|
+
ModelOne.new(foo: 'Foo',
|
59
|
+
bar: 'Bar',
|
60
|
+
emb_one: emb_one,
|
61
|
+
emb_threes: emb_threes)
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#modified_attributes_for_create' do
|
65
|
+
before(:each) { ModelOne.clear_trackable_memoization }
|
66
|
+
subject { model_one.send(:modified_attributes_for_create) }
|
67
|
+
|
68
|
+
context 'with tracked embeds_one object' do
|
69
|
+
before(:each) { ModelOne.track_history(on: { emb_one: :f_em_foo }) }
|
70
|
+
it 'should include tracked attributes only' do
|
71
|
+
expect(subject['emb_one'][0]).to be_nil
|
72
|
+
|
73
|
+
expect(subject['emb_one'][1].keys.size).to eq 2
|
74
|
+
expect(subject['emb_one'][1]['_id']).to eq emb_one._id
|
75
|
+
expect(subject['emb_one'][1]['f_em_foo']).to eq 'Foo'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with untracked embeds_one object' do
|
80
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
81
|
+
it 'should not include embeds_one attributes' do
|
82
|
+
expect(subject['emb_one']).to be_nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with tracked embeds_many objects' do
|
87
|
+
before(:each) { ModelOne.track_history(on: { emb_threes: :f_em_foo }) }
|
88
|
+
it 'should include tracked attributes only' do
|
89
|
+
expect(subject['emb_threes'][0]).to be_nil
|
90
|
+
|
91
|
+
expect(subject['emb_threes'][1][0].keys.count).to eq 2
|
92
|
+
expect(subject['emb_threes'][1][0]['_id']).to eq emb_threes.first._id
|
93
|
+
expect(subject['emb_threes'][1][0]['f_em_foo']).to eq 'Foo'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'with untracked embeds_many objects' do
|
98
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
99
|
+
it 'should include not tracked embeds_many attributes' do
|
100
|
+
expect(subject['emb_threes']).to be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when embeds_one object blank' do
|
105
|
+
let(:emb_one) { nil }
|
106
|
+
|
107
|
+
it 'should not include embeds_one model key' do
|
108
|
+
expect(subject.keys).to_not include 'emb_one'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe 'embeds_one' do
|
113
|
+
before(:all) do
|
114
|
+
Mail = Class.new do
|
115
|
+
include Mongoid::Document
|
116
|
+
include Mongoid::History::Trackable
|
117
|
+
store_in collection: :mails
|
118
|
+
field :provider
|
119
|
+
embeds_one :mail_subject, inverse_class_name: 'MailSubject'
|
120
|
+
end
|
121
|
+
|
122
|
+
MailSubject = Class.new do
|
123
|
+
include Mongoid::Document
|
124
|
+
field :content
|
125
|
+
embedded_in :mail
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
before(:each) do
|
130
|
+
Mail.instance_variable_set(:@history_trackable_options, nil)
|
131
|
+
Mail.track_history(on: :mail_subject)
|
132
|
+
end
|
133
|
+
|
134
|
+
let(:mail) { Mail.new(mail_subject: mail_subject) }
|
135
|
+
let(:mail_subject) { nil }
|
136
|
+
subject { mail.send(:modified_attributes_for_create)['mail_subject'] }
|
137
|
+
|
138
|
+
context 'when obj not built' do
|
139
|
+
it { is_expected.to be_nil }
|
140
|
+
end
|
141
|
+
|
142
|
+
context 'when obj does not respond to paranoia_field' do
|
143
|
+
let(:mail_subject) { MailSubject.new(content: 'Content') }
|
144
|
+
it { is_expected.to eq [nil, { '_id' => mail_subject._id, 'content' => 'Content' }] }
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'when obj not soft-deleted' do
|
148
|
+
before(:each) { allow(mail_subject).to receive(:deleted_at) { nil } }
|
149
|
+
let(:mail_subject) { MailSubject.new(content: 'Content') }
|
150
|
+
it { is_expected.to eq [nil, { '_id' => mail_subject._id, 'content' => 'Content' }] }
|
151
|
+
end
|
152
|
+
|
153
|
+
context 'when obj soft-deleted' do
|
154
|
+
before(:each) { allow(mail_subject).to receive(:deleted_at) { Time.now } }
|
155
|
+
let(:mail_subject) { MailSubject.new(content: 'Content') }
|
156
|
+
it { is_expected.to be_nil }
|
157
|
+
end
|
158
|
+
|
159
|
+
after(:all) do
|
160
|
+
Object.send(:remove_const, :MailSubject)
|
161
|
+
Object.send(:remove_const, :Mail)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe 'paranoia' do
|
166
|
+
before(:all) do
|
167
|
+
ModelParanoia = Class.new do
|
168
|
+
include Mongoid::Document
|
169
|
+
include Mongoid::History::Trackable
|
170
|
+
store_in collection: :model_paranoias
|
171
|
+
embeds_many :emb_para_ones, inverse_class_name: 'EmbParaOne'
|
172
|
+
end
|
173
|
+
|
174
|
+
EmbParaOne = Class.new do
|
175
|
+
include Mongoid::Document
|
176
|
+
field :em_foo
|
177
|
+
field :deleted_at
|
178
|
+
embedded_in :model_paranoia
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
let(:emb_para_one) { EmbParaOne.new(em_foo: 'Em-Foo') }
|
183
|
+
let(:model_paranoia) { ModelParanoia.new(emb_para_ones: [emb_para_one]) }
|
184
|
+
|
185
|
+
context 'when does not respond to paranoia_field' do
|
186
|
+
before(:each) do
|
187
|
+
ModelParanoia.instance_variable_set(:@history_trackable_options, nil)
|
188
|
+
ModelParanoia.track_history(on: :emb_para_ones)
|
189
|
+
end
|
190
|
+
|
191
|
+
subject { model_paranoia.send(:modified_attributes_for_create) }
|
192
|
+
|
193
|
+
it 'should include tracked embeds_many objects attributes' do
|
194
|
+
expect(subject['emb_para_ones'][0]).to be_nil
|
195
|
+
expect(subject['emb_para_ones'][1].size).to eq 1
|
196
|
+
expect(subject['emb_para_ones'][1][0]['_id']).to be_a bson_class
|
197
|
+
expect(subject['emb_para_ones'][1][0]['em_foo']).to eq 'Em-Foo'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'when responds to paranoia_field' do
|
202
|
+
before(:each) do
|
203
|
+
ModelParanoia.instance_variable_set(:@history_trackable_options, nil)
|
204
|
+
ModelParanoia.track_history(on: :emb_para_ones)
|
205
|
+
allow(emb_para_one).to receive(:deleted_at) { Time.now }
|
206
|
+
allow(emb_para_one_2).to receive(:deleted_at) { nil }
|
207
|
+
end
|
208
|
+
|
209
|
+
let(:model_paranoia) { ModelParanoia.new(emb_para_ones: [emb_para_one, emb_para_one_2]) }
|
210
|
+
let(:emb_para_one) { EmbParaOne.new(em_foo: 'Em-Foo') }
|
211
|
+
let(:emb_para_one_2) { EmbParaOne.new(em_foo: 'Em-Foo-2') }
|
212
|
+
|
213
|
+
subject { model_paranoia.send(:modified_attributes_for_create) }
|
214
|
+
|
215
|
+
it 'should not include deleted objects attributes' do
|
216
|
+
expect(subject['emb_para_ones'][0]).to be_nil
|
217
|
+
expect(subject['emb_para_ones'][1]).to eq [{ '_id' => emb_para_one_2._id, 'em_foo' => 'Em-Foo-2' }]
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
after(:all) do
|
222
|
+
Object.send(:remove_const, :ModelParanoia)
|
223
|
+
Object.send(:remove_const, :EmbParaOne)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
describe '#modified_attributes_for_update' do
|
229
|
+
before(:each) do
|
230
|
+
model_one.save!
|
231
|
+
ModelOne.clear_trackable_memoization
|
232
|
+
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
233
|
+
allow(model_one).to receive(:changes) { changes }
|
234
|
+
end
|
235
|
+
let(:changes) { {} }
|
236
|
+
subject { model_one.send(:modified_attributes_for_update) }
|
237
|
+
|
238
|
+
context 'when embeds_one attributes passed in options' do
|
239
|
+
before(:each) { ModelOne.track_history(on: { emb_one: :f_em_foo }) }
|
240
|
+
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }, { 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }] } }
|
241
|
+
it { expect(subject['emb_one'][0]).to eq('f_em_foo' => 'Foo') }
|
242
|
+
it { expect(subject['emb_one'][1]).to eq('f_em_foo' => 'Foo-new') }
|
243
|
+
end
|
244
|
+
|
245
|
+
context 'when embeds_one relation passed in options' do
|
246
|
+
before(:each) { ModelOne.track_history(on: :emb_one) }
|
247
|
+
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }, { 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }] } }
|
248
|
+
it { expect(subject['emb_one'][0]).to eq('f_em_foo' => 'Foo', 'fmb' => 'Bar') }
|
249
|
+
it { expect(subject['emb_one'][1]).to eq('f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new') }
|
250
|
+
end
|
251
|
+
|
252
|
+
context 'when embeds_one relation not tracked' do
|
253
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
254
|
+
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo' }, { 'f_em_foo' => 'Foo-new' }] } }
|
255
|
+
it { expect(subject['emb_one']).to be_nil }
|
256
|
+
end
|
257
|
+
|
258
|
+
context 'when embeds_many attributes passed in options' do
|
259
|
+
before(:each) { ModelOne.track_history(on: { emb_threes: :f_em_foo }) }
|
260
|
+
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] } }
|
261
|
+
it { expect(subject['emb_threes']).to eq [[{ 'f_em_foo' => 'Foo' }], [{ 'f_em_foo' => 'Foo-new' }]] }
|
262
|
+
end
|
263
|
+
|
264
|
+
context 'when embeds_many relation passed in options' do
|
265
|
+
before(:each) { ModelOne.track_history(on: :emb_threes) }
|
266
|
+
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] } }
|
267
|
+
it { expect(subject['emb_threes']).to eq [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] }
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'when embeds_many relation not tracked' do
|
271
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
272
|
+
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo' }], [{ 'f_em_foo' => 'Foo-new' }]] } }
|
273
|
+
it { expect(subject['emb_threes']).to be_nil }
|
274
|
+
end
|
275
|
+
|
276
|
+
context 'when field tracked' do
|
277
|
+
before(:each) { ModelOne.track_history(on: :foo) }
|
278
|
+
let(:changes) { { 'foo' => ['Foo', 'Foo-new'], 'b' => ['Bar', 'Bar-new'] } }
|
279
|
+
it { is_expected.to eq('foo' => ['Foo', 'Foo-new']) }
|
280
|
+
end
|
281
|
+
|
282
|
+
context 'when field not tracked' do
|
283
|
+
before(:each) { ModelOne.track_history(on: []) }
|
284
|
+
let(:changes) { { 'foo' => ['Foo', 'Foo-new'] } }
|
285
|
+
it { is_expected.to eq({}) }
|
286
|
+
end
|
287
|
+
|
288
|
+
describe 'embeds_one' do
|
289
|
+
before(:all) do
|
290
|
+
Email = Class.new do
|
291
|
+
include Mongoid::Document
|
292
|
+
include Mongoid::History::Trackable
|
293
|
+
store_in collection: :emails
|
294
|
+
field :provider
|
295
|
+
embeds_one :email_subject, inverse_class_name: 'EmailSubject'
|
296
|
+
end
|
297
|
+
|
298
|
+
EmailSubject = Class.new do
|
299
|
+
include Mongoid::Document
|
300
|
+
include Mongoid::History::Trackable
|
301
|
+
field :content
|
302
|
+
embedded_in :email_subject
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
before(:each) do
|
307
|
+
Email.instance_variable_set(:@history_trackable_options, nil)
|
308
|
+
Email.track_history(on: :email_subject)
|
309
|
+
allow(Email).to receive(:dynamic_enabled?) { false }
|
310
|
+
allow(email).to receive(:changes) { changes }
|
311
|
+
end
|
312
|
+
|
313
|
+
let(:email) { Email.new }
|
314
|
+
let(:changes) { {} }
|
315
|
+
subject { email.send(:modified_attributes_for_update)['email_subject'] }
|
316
|
+
|
317
|
+
context 'when paranoia_field not present' do
|
318
|
+
let(:changes) { { 'email_subject' => [{ 'content' => 'Content' }, { 'content' => 'Content-new' }] } }
|
319
|
+
it { is_expected.to eq [{ 'content' => 'Content' }, { 'content' => 'Content-new' }] }
|
320
|
+
end
|
321
|
+
|
322
|
+
context 'when older soft-deleted' do
|
323
|
+
let(:changes) { { 'email_subject' => [{ 'content' => 'Content', 'deleted_at' => Time.now }, { 'content' => 'Content-new' }] } }
|
324
|
+
it { is_expected.to eq [{}, { 'content' => 'Content-new' }] }
|
325
|
+
end
|
326
|
+
|
327
|
+
context 'when new soft-deleted' do
|
328
|
+
let(:changes) { { 'email_subject' => [{ 'content' => 'Content' }, { 'content' => 'Content-new', 'deleted_at' => Time.now }] } }
|
329
|
+
it { is_expected.to eq [{ 'content' => 'Content' }, {}] }
|
330
|
+
end
|
331
|
+
|
332
|
+
context 'when not soft-deleted' do
|
333
|
+
let(:changes) do
|
334
|
+
{ 'email_subject' => [{ 'content' => 'Content', 'deleted_at' => nil }, { 'content' => 'Content-new', 'deleted_at' => nil }] }
|
335
|
+
end
|
336
|
+
it { is_expected.to eq [{ 'content' => 'Content' }, { 'content' => 'Content-new' }] }
|
337
|
+
end
|
338
|
+
|
339
|
+
after(:all) do
|
340
|
+
Object.send(:remove_const, :EmailSubject)
|
341
|
+
Object.send(:remove_const, :Email)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
describe 'paranoia_field' do
|
346
|
+
context 'when embeds_one has alias' do
|
347
|
+
before(:all) do
|
348
|
+
# Here i need class name constant in trackable.rb. So, not using `let` to define classes
|
349
|
+
ModelTwo = Class.new do
|
350
|
+
include Mongoid::Document
|
351
|
+
include Mongoid::History::Trackable
|
352
|
+
store_in collection: :model_twos
|
353
|
+
embeds_one :emb_two_one, inverse_class_name: 'EmbTwoOne'
|
354
|
+
end
|
355
|
+
|
356
|
+
EmbTwoOne = Class.new do
|
357
|
+
include Mongoid::Document
|
358
|
+
include Mongoid::History::Trackable
|
359
|
+
field :foo
|
360
|
+
field :cncl, as: :cancelled_at
|
361
|
+
embedded_in :model_two
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
before(:each) do
|
366
|
+
ModelTwo.instance_variable_set(:@history_trackable_options, nil)
|
367
|
+
ModelTwo.instance_variable_set(:@trackable_settings, nil)
|
368
|
+
EmbTwoOne.instance_variable_set(:@trackable_settings, nil)
|
369
|
+
|
370
|
+
ModelTwo.track_history on: :emb_two_one
|
371
|
+
EmbTwoOne.history_settings paranoia_field: :cancelled_at
|
372
|
+
|
373
|
+
allow(ModelTwo).to receive(:dynamic_enabled?) { false }
|
374
|
+
allow(model_two_obj).to receive(:changes) { changes }
|
375
|
+
end
|
376
|
+
|
377
|
+
let(:model_two_obj) { ModelTwo.new }
|
378
|
+
let(:changes) { { 'emb_two_one' => [{ 'foo' => 'Foo', 'cncl' => Time.now }, { 'foo' => 'Foo-new' }] } }
|
379
|
+
subject { model_two_obj.send(:modified_attributes_for_update)['emb_two_one'] }
|
380
|
+
it { is_expected.to eq [{}, { 'foo' => 'Foo-new' }] }
|
381
|
+
|
382
|
+
after(:all) do
|
383
|
+
Object.send(:remove_const, :ModelTwo)
|
384
|
+
Object.send(:remove_const, :EmbTwoOne)
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
context 'when embeds_many has alias' do
|
389
|
+
before(:all) do
|
390
|
+
# Here i need class name constant in trackable.rb. So, not using `let` to define classes
|
391
|
+
ModelTwo = Class.new do
|
392
|
+
include Mongoid::Document
|
393
|
+
include Mongoid::History::Trackable
|
394
|
+
store_in collection: :model_twos
|
395
|
+
embeds_many :emb_two_ones, inverse_class_name: 'EmbTwoOne'
|
396
|
+
end
|
397
|
+
|
398
|
+
EmbTwoOne = Class.new do
|
399
|
+
include Mongoid::Document
|
400
|
+
include Mongoid::History::Trackable
|
401
|
+
field :foo
|
402
|
+
field :cncl, as: :cancelled_at
|
403
|
+
embedded_in :model_two
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
before(:each) do
|
408
|
+
ModelTwo.instance_variable_set(:@history_trackable_options, nil)
|
409
|
+
ModelTwo.instance_variable_set(:@trackable_settings, nil)
|
410
|
+
EmbTwoOne.instance_variable_set(:@trackable_settings, nil)
|
411
|
+
|
412
|
+
ModelTwo.track_history on: :emb_two_ones
|
413
|
+
EmbTwoOne.history_settings paranoia_field: :cancelled_at
|
414
|
+
|
415
|
+
allow(ModelTwo).to receive(:dynamic_enabled?) { false }
|
416
|
+
allow(model_two_obj).to receive(:changes) { changes }
|
417
|
+
end
|
418
|
+
|
419
|
+
let(:model_two_obj) { ModelTwo.new }
|
420
|
+
let(:changes) { { 'emb_two_ones' => [[{ 'foo' => 'Foo', 'cncl' => Time.now }], [{ 'foo' => 'Foo-new' }]] } }
|
421
|
+
subject { model_two_obj.send(:modified_attributes_for_update)['emb_two_ones'] }
|
422
|
+
it { is_expected.to eq [[], [{ 'foo' => 'Foo-new' }]] }
|
423
|
+
|
424
|
+
after(:all) do
|
425
|
+
Object.send(:remove_const, :ModelTwo)
|
426
|
+
Object.send(:remove_const, :EmbTwoOne)
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
describe '#modified_attributes_for_destroy' do
|
433
|
+
before(:each) do
|
434
|
+
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
435
|
+
model_one.save!
|
436
|
+
ModelOne.clear_trackable_memoization
|
437
|
+
end
|
438
|
+
subject { model_one.send(:modified_attributes_for_destroy) }
|
439
|
+
|
440
|
+
context 'with tracked embeds_one object' do
|
441
|
+
before(:each) { ModelOne.track_history(on: { emb_one: :f_em_foo }) }
|
442
|
+
it 'should include tracked attributes only' do
|
443
|
+
expect(subject['emb_one'][0].keys.size).to eq 2
|
444
|
+
expect(subject['emb_one'][0]['_id']).to eq emb_one._id
|
445
|
+
expect(subject['emb_one'][0]['f_em_foo']).to eq 'Foo'
|
446
|
+
|
447
|
+
expect(subject['emb_one'][1]).to be_nil
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
context 'with untracked embeds_one object' do
|
452
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
453
|
+
it 'should not include embeds_one attributes' do
|
454
|
+
expect(subject['emb_one']).to be_nil
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
context 'with tracked embeds_many objects' do
|
459
|
+
before(:each) { ModelOne.track_history(on: { emb_threes: :f_em_foo }) }
|
460
|
+
it 'should include tracked attributes only' do
|
461
|
+
expect(subject['emb_threes'][0][0].keys.count).to eq 2
|
462
|
+
expect(subject['emb_threes'][0][0]['_id']).to eq emb_threes.first._id
|
463
|
+
expect(subject['emb_threes'][0][0]['f_em_foo']).to eq 'Foo'
|
464
|
+
|
465
|
+
expect(subject['emb_threes'][1]).to be_nil
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
context 'with untracked embeds_many objects' do
|
470
|
+
before(:each) { ModelOne.track_history(on: :fields) }
|
471
|
+
it 'should include not tracked embeds_many attributes' do
|
472
|
+
expect(subject['emb_threes']).to be_nil
|
473
|
+
end
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
after :all do
|
478
|
+
Object.send(:remove_const, :ModelOne)
|
479
|
+
Object.send(:remove_const, :EmbOne)
|
480
|
+
Object.send(:remove_const, :EmbTwo)
|
481
|
+
Object.send(:remove_const, :EmbThree)
|
482
|
+
Object.send(:remove_const, :EmbFour)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|