mongoid-history 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +22 -18
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +4 -0
  5. data/CONTRIBUTING.md +4 -4
  6. data/Gemfile +8 -6
  7. data/README.md +2 -12
  8. data/UPGRADING.md +20 -1
  9. data/lib/mongoid/history.rb +8 -4
  10. data/lib/mongoid/history/attributes/base.rb +2 -2
  11. data/lib/mongoid/history/attributes/create.rb +2 -2
  12. data/lib/mongoid/history/attributes/update.rb +2 -2
  13. data/lib/mongoid/history/options.rb +11 -20
  14. data/lib/mongoid/history/trackable.rb +71 -56
  15. data/lib/mongoid/history/tracker.rb +8 -5
  16. data/lib/mongoid/history/version.rb +1 -1
  17. data/spec/integration/embedded_in_polymorphic_spec.rb +26 -49
  18. data/spec/integration/integration_spec.rb +132 -120
  19. data/spec/integration/multi_relation_spec.rb +14 -20
  20. data/spec/integration/multiple_trackers_spec.rb +35 -38
  21. data/spec/integration/nested_embedded_documents_spec.rb +31 -51
  22. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +64 -76
  23. data/spec/integration/subclasses_spec.rb +17 -5
  24. data/spec/integration/track_history_order_spec.rb +59 -27
  25. data/spec/integration/validation_failure_spec.rb +21 -8
  26. data/spec/spec_helper.rb +6 -1
  27. data/spec/unit/attributes/base_spec.rb +17 -26
  28. data/spec/unit/attributes/create_spec.rb +152 -125
  29. data/spec/unit/attributes/destroy_spec.rb +68 -58
  30. data/spec/unit/attributes/update_spec.rb +71 -50
  31. data/spec/unit/callback_options_spec.rb +36 -30
  32. data/spec/unit/embedded_methods_spec.rb +42 -24
  33. data/spec/unit/history_spec.rb +12 -10
  34. data/spec/unit/my_instance_methods_spec.rb +191 -121
  35. data/spec/unit/options_spec.rb +49 -26
  36. data/spec/unit/singleton_methods_spec.rb +156 -88
  37. data/spec/unit/trackable_spec.rb +254 -156
  38. data/spec/unit/tracker_spec.rb +81 -54
  39. metadata +2 -2
@@ -1,22 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
- class MyModel
4
- include Mongoid::Document
5
- include Mongoid::History::Trackable
6
- field :foo
7
- end
3
+ describe Mongoid::History::Trackable do
4
+ before :each do
5
+ class MyModel
6
+ include Mongoid::Document
7
+ include Mongoid::History::Trackable
8
8
 
9
- class MyDynamicModel
10
- include Mongoid::Document
11
- include Mongoid::History::Trackable
12
- include Mongoid::Attributes::Dynamic unless Mongoid::Compatibility::Version.mongoid3?
13
- end
9
+ field :foo
10
+ end
14
11
 
15
- class HistoryTracker
16
- include Mongoid::History::Tracker
17
- end
12
+ class MyDynamicModel
13
+ include Mongoid::Document
14
+ include Mongoid::History::Trackable
15
+ include Mongoid::Attributes::Dynamic unless Mongoid::Compatibility::Version.mongoid3?
16
+ end
17
+
18
+ class HistoryTracker
19
+ include Mongoid::History::Tracker
20
+ end
21
+
22
+ class User
23
+ include Mongoid::Document
24
+ end
25
+ end
26
+
27
+ after :each do
28
+ Object.send(:remove_const, :MyModel)
29
+ Object.send(:remove_const, :MyDynamicModel)
30
+ Object.send(:remove_const, :HistoryTracker)
31
+ Object.send(:remove_const, :User)
32
+ end
33
+
34
+ let(:user) { User.create! }
18
35
 
19
- describe Mongoid::History::Trackable do
20
36
  it 'should have #track_history' do
21
37
  expect(MyModel).to respond_to :track_history
22
38
  end
@@ -28,20 +44,27 @@ describe Mongoid::History::Trackable do
28
44
  end
29
45
 
30
46
  describe '#track_history' do
31
- class MyModelWithNoModifier
32
- include Mongoid::Document
33
- include Mongoid::History::Trackable
34
- field :foo
47
+ before :each do
48
+ class MyModelWithNoModifier
49
+ include Mongoid::Document
50
+ include Mongoid::History::Trackable
51
+
52
+ field :foo
53
+ end
54
+ end
55
+
56
+ after :each do
57
+ Object.send(:remove_const, :MyModelWithNoModifier)
35
58
  end
36
59
 
37
- before :all do
60
+ before :each do
38
61
  MyModel.track_history
39
- @persisted_history_options = Mongoid::History.trackable_class_options
40
62
  MyModelWithNoModifier.track_history modifier_field: nil
41
63
  end
42
- before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
64
+
43
65
  let(:expected_option) do
44
- { on: %i[foo],
66
+ {
67
+ on: %i[foo],
45
68
  except: %w[created_at updated_at],
46
69
  tracker_class_name: nil,
47
70
  modifier_field: :modifier,
@@ -54,8 +77,10 @@ describe Mongoid::History::Trackable do
54
77
  fields: %w[foo],
55
78
  relations: { embeds_one: {}, embeds_many: {} },
56
79
  dynamic: [],
57
- format: {} }
80
+ format: {}
81
+ }
58
82
  end
83
+
59
84
  let(:regular_fields) { ['foo'] }
60
85
  let(:reserved_fields) { %w[_id version modifier_id] }
61
86
 
@@ -87,15 +112,24 @@ describe Mongoid::History::Trackable do
87
112
  end
88
113
 
89
114
  context 'modifier_field_optional true' do
90
- class MyModelWithOptionalModifier
91
- include Mongoid::Document
92
- include Mongoid::History::Trackable
93
- field :foo
115
+ before :each do
116
+ class MyModelWithOptionalModifier
117
+ include Mongoid::Document
118
+ include Mongoid::History::Trackable
119
+
120
+ field :foo
121
+ end
122
+ end
123
+
124
+ after :each do
125
+ Object.send(:remove_const, :MyModelWithOptionalModifier)
94
126
  end
95
127
 
96
128
  it 'marks modifier relationship optional' do
97
129
  MyModelWithOptionalModifier.track_history modifier_field_optional: true
98
- if Mongoid::Compatibility::Version.mongoid6_or_newer?
130
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
131
+ expect(MyModelWithOptionalModifier.reflect_on_association(:modifier).options[:optional]).to be true
132
+ elsif Mongoid::Compatibility::Version.mongoid6_or_newer?
99
133
  expect(MyModelWithOptionalModifier.reflect_on_association(:modifier)[:optional]).to be true
100
134
  else
101
135
  expect(MyModelWithOptionalModifier.reflect_on_association(:modifier)).not_to be_nil
@@ -189,14 +223,19 @@ describe Mongoid::History::Trackable do
189
223
  end
190
224
 
191
225
  describe '#field_format' do
192
- before :all do
193
- ModelOne = Class.new do
226
+ before :each do
227
+ class ModelOne
194
228
  include Mongoid::Document
195
229
  include Mongoid::History::Trackable
230
+
196
231
  field :foo
197
232
  end
198
233
  end
199
234
 
235
+ after :each do
236
+ Object.send(:remove_const, :ModelOne)
237
+ end
238
+
200
239
  let(:format) { '***' }
201
240
 
202
241
  before do
@@ -214,10 +253,6 @@ describe Mongoid::History::Trackable do
214
253
  expect(ModelOne.field_format(:bar)).to be_nil
215
254
  end
216
255
  end
217
-
218
- after :all do
219
- Object.send(:remove_const, :ModelOne)
220
- end
221
256
  end
222
257
 
223
258
  context 'sub-model' do
@@ -226,6 +261,10 @@ describe Mongoid::History::Trackable do
226
261
  end
227
262
  end
228
263
 
264
+ after :each do
265
+ Object.send(:remove_const, :MySubModel)
266
+ end
267
+
229
268
  it 'should have default options' do
230
269
  expect(Mongoid::History.trackable_class_options[:my_model].prepared).to eq(expected_option)
231
270
  end
@@ -261,16 +300,25 @@ describe Mongoid::History::Trackable do
261
300
  expect(MyModel.new.track_history?).to eq(true)
262
301
  end
263
302
 
264
- it 'should be disabled only for the class that calls disable_tracking' do
265
- class MyModel2
266
- include Mongoid::Document
267
- include Mongoid::History::Trackable
268
- track_history
303
+ context 'with multiple classes' do
304
+ before :each do
305
+ class MyModel2
306
+ include Mongoid::Document
307
+ include Mongoid::History::Trackable
308
+
309
+ track_history
310
+ end
269
311
  end
270
312
 
271
- MyModel.disable_tracking do
272
- expect(Mongoid::History.enabled?).to eq(true)
273
- expect(MyModel2.new.track_history?).to eq(true)
313
+ after :each do
314
+ Object.send(:remove_const, :MyModel2)
315
+ end
316
+
317
+ it 'should be disabled only for the class that calls disable_tracking' do
318
+ MyModel.disable_tracking do
319
+ expect(Mongoid::History.enabled?).to eq(true)
320
+ expect(MyModel2.new.track_history?).to eq(true)
321
+ end
274
322
  end
275
323
  end
276
324
  end
@@ -305,17 +353,26 @@ describe Mongoid::History::Trackable do
305
353
  end
306
354
  end
307
355
 
308
- it 'should be disabled only for the class that calls disable_tracking' do
309
- class MyModel2
310
- include Mongoid::Document
311
- include Mongoid::History::Trackable
312
- track_history
356
+ context 'with multiple classes' do
357
+ before :each do
358
+ class MyModel2
359
+ include Mongoid::Document
360
+ include Mongoid::History::Trackable
361
+
362
+ track_history
363
+ end
313
364
  end
314
365
 
315
- Mongoid::History.disable do
316
- MyModel.disable_tracking do
317
- expect(Mongoid::History.enabled?).to eq(false)
318
- expect(MyModel2.new.track_history?).to eq(false)
366
+ after :each do
367
+ Object.send(:remove_const, :MyModel2)
368
+ end
369
+
370
+ it 'should be disabled only for the class that calls disable_tracking' do
371
+ Mongoid::History.disable do
372
+ MyModel.disable_tracking do
373
+ expect(Mongoid::History.enabled?).to eq(false)
374
+ expect(MyModel2.new.track_history?).to eq(false)
375
+ end
319
376
  end
320
377
  end
321
378
  end
@@ -346,44 +403,74 @@ describe Mongoid::History::Trackable do
346
403
  end
347
404
 
348
405
  describe ':changes_method' do
349
- let(:custom_tracker) do
350
- CustomTracker = Class.new(MyModel) do
351
- field :key
406
+ it 'should be set in parent class' do
407
+ expect(MyModel.history_trackable_options[:changes_method]).to eq :changes
408
+ end
409
+
410
+ context 'subclass' do
411
+ before :each do
412
+ # BUGBUG: if this is not prepared, it inherits the subclass settings
413
+ MyModel.history_trackable_options
352
414
 
353
- track_history on: :key, changes_method: :my_changes, track_create: true
415
+ class CustomTracker < MyModel
416
+ field :key
354
417
 
355
- def my_changes
356
- changes.merge('key' => "Save history-#{key}")
418
+ track_history on: :key, changes_method: :my_changes, track_create: true
419
+
420
+ def my_changes
421
+ changes.merge('key' => "Save history-#{key}")
422
+ end
357
423
  end
358
424
  end
359
- end
360
425
 
361
- it 'should default to :changes' do
362
- m = MyModel.create
363
- expect(m).to receive(:changes).exactly(3).times.and_call_original
364
- expect(m).not_to receive(:my_changes)
365
- m.save
366
- end
426
+ after :each do
427
+ Object.send(:remove_const, :CustomTracker)
428
+ end
367
429
 
368
- it 'should allow an alternate method to be specified' do
369
- class MyModel3 < MyModel
370
- track_history changes_method: :my_changes
430
+ it 'should not override in parent class' do
431
+ expect(MyModel.history_trackable_options[:changes_method]).to eq :changes
432
+ expect(CustomTracker.history_trackable_options[:changes_method]).to eq :my_changes
433
+ end
371
434
 
372
- def my_changes
373
- {}
374
- end
435
+ it 'should default to :changes' do
436
+ m = MyModel.create!(modifier: user)
437
+ expect(m).to receive(:changes).exactly(3).times.and_call_original
438
+ expect(m).not_to receive(:my_changes)
439
+ m.save!
375
440
  end
376
441
 
377
- m = MyModel3.create
378
- expect(m).to receive(:changes).twice.and_call_original
379
- expect(m).to receive(:my_changes).once.and_call_original
380
- m.save
381
- end
442
+ context 'with another model' do
443
+ before :each do
444
+ class MyModel3 < MyModel
445
+ track_history changes_method: :my_changes
446
+
447
+ def my_changes
448
+ {}
449
+ end
450
+ end
451
+ end
452
+
453
+ after :each do
454
+ Object.send(:remove_const, :MyModel3)
455
+ end
382
456
 
383
- it 'should allow an alternate method to be specified on object creation' do
384
- m = custom_tracker.create(key: 'on object creation')
385
- history_track = m.history_tracks.last
386
- expect(history_track.modified['key']).to eq('Save history-on object creation')
457
+ it 'should allow an alternate method to be specified' do
458
+ m = MyModel3.create!(modifier: user)
459
+ expect(m).to receive(:changes).twice.and_call_original
460
+ expect(m).to receive(:my_changes).once.and_call_original
461
+ m.save
462
+ end
463
+ end
464
+
465
+ it 'should allow an alternate method to be specified on object creation' do
466
+ m = if Mongoid::Compatibility::Version.mongoid7_or_newer? # BUGBUG
467
+ CustomTracker.create!(key: 'on object creation', modifier: user)
468
+ else
469
+ CustomTracker.create!(key: 'on object creation')
470
+ end
471
+ history_track = m.history_tracks.last
472
+ expect(history_track.modified['key']).to eq('Save history-on object creation')
473
+ end
387
474
  end
388
475
  end
389
476
  end
@@ -391,51 +478,50 @@ describe Mongoid::History::Trackable do
391
478
  describe '#history_settings' do
392
479
  before(:each) { Mongoid::History.trackable_settings = nil }
393
480
 
394
- let(:model_one) do
395
- Class.new do
481
+ before :each do
482
+ class ModelOne
396
483
  include Mongoid::Document
397
484
  include Mongoid::History::Trackable
485
+
398
486
  store_in collection: :model_ones
399
- embeds_one :emb_one, inverse_class_name: 'EmbOne'
400
- embeds_many :emb_twos, inverse_class_name: 'EmbTwo'
401
487
 
402
- def self.name
403
- 'ModelOne'
488
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
489
+ embeds_one :emb_one
490
+ embeds_many :emb_twos
491
+ else
492
+ embeds_one :emb_one, inverse_class_name: 'EmbOne'
493
+ embeds_many :emb_twos, inverse_class_name: 'EmbTwo'
404
494
  end
405
495
  end
406
- end
407
496
 
408
- let(:emb_one) do
409
- Class.new do
497
+ class EmbOne
410
498
  include Mongoid::Document
411
499
  include Mongoid::History::Trackable
412
- embedded_in :model_one
413
500
 
414
- def self.name
415
- 'EmbOne'
416
- end
501
+ embedded_in :model_one
417
502
  end
418
- end
419
503
 
420
- let(:emb_two) do
421
- Class.new do
504
+ class EmbTwo
422
505
  include Mongoid::Document
423
506
  include Mongoid::History::Trackable
424
- embedded_in :model_one
425
507
 
426
- def self.name
427
- 'EmbTwo'
428
- end
508
+ embedded_in :model_one
429
509
  end
430
510
  end
431
511
 
512
+ after :each do
513
+ Object.send(:remove_const, :ModelOne)
514
+ Object.send(:remove_const, :EmbOne)
515
+ Object.send(:remove_const, :EmbTwo)
516
+ end
517
+
432
518
  let(:default_options) { { paranoia_field: 'deleted_at' } }
433
519
 
434
520
  context 'when options not passed' do
435
521
  before(:each) do
436
- model_one.history_settings
437
- emb_one.history_settings
438
- emb_two.history_settings
522
+ ModelOne.history_settings
523
+ EmbOne.history_settings
524
+ EmbTwo.history_settings
439
525
  end
440
526
 
441
527
  it 'should use default options' do
@@ -447,9 +533,9 @@ describe Mongoid::History::Trackable do
447
533
 
448
534
  context 'when extra invalid options passed' do
449
535
  before(:each) do
450
- model_one.history_settings foo: :bar
451
- emb_one.history_settings em_foo: :em_bar
452
- emb_two.history_settings em_foo: :em_baz
536
+ ModelOne.history_settings foo: :bar
537
+ EmbOne.history_settings em_foo: :em_bar
538
+ EmbTwo.history_settings em_foo: :em_baz
453
539
  end
454
540
 
455
541
  it 'should ignore invalid options' do
@@ -461,9 +547,9 @@ describe Mongoid::History::Trackable do
461
547
 
462
548
  context 'when valid options passed' do
463
549
  before(:each) do
464
- model_one.history_settings paranoia_field: :disabled_at
465
- emb_one.history_settings paranoia_field: :deactivated_at
466
- emb_two.history_settings paranoia_field: :omitted_at
550
+ ModelOne.history_settings paranoia_field: :disabled_at
551
+ EmbOne.history_settings paranoia_field: :deactivated_at
552
+ EmbTwo.history_settings paranoia_field: :omitted_at
467
553
  end
468
554
 
469
555
  it 'should override default options' do
@@ -474,7 +560,7 @@ describe Mongoid::History::Trackable do
474
560
  end
475
561
 
476
562
  context 'when string keys' do
477
- before(:each) { model_one.history_settings 'paranoia_field' => 'erased_at' }
563
+ before(:each) { ModelOne.history_settings 'paranoia_field' => 'erased_at' }
478
564
 
479
565
  it 'should convert option keys to symbols' do
480
566
  expect(Mongoid::History.trackable_settings[:ModelOne]).to eq(paranoia_field: 'erased_at')
@@ -482,33 +568,36 @@ describe Mongoid::History::Trackable do
482
568
  end
483
569
 
484
570
  context 'when paranoia field has alias' do
485
- before(:each) do
486
- Mongoid::History.trackable_settings = nil
487
- model_two.history_settings paranoia_field: :neglected_at
488
- end
489
-
490
- let(:model_two) do
491
- Class.new do
571
+ before :each do
572
+ class ModelTwo
492
573
  include Mongoid::Document
493
574
  include Mongoid::History::Trackable
494
- field :nglt, as: :neglected_at
495
575
 
496
- def self.name
497
- 'ModelTwo'
498
- end
576
+ field :nglt, as: :neglected_at
499
577
  end
500
578
  end
501
579
 
580
+ after :each do
581
+ Object.send(:remove_const, :ModelTwo)
582
+ end
583
+
584
+ before :each do
585
+ ModelTwo.history_settings paranoia_field: :neglected_at
586
+ end
587
+
502
588
  it { expect(Mongoid::History.trackable_settings[:ModelTwo]).to eq(paranoia_field: 'nglt') }
503
589
  end
504
590
  end
505
591
 
506
592
  describe '#tracker_class' do
507
- before :all do
508
- MyTrackerClass = Class.new
593
+ before :each do
594
+ class MyTrackerClass
595
+ end
509
596
  end
510
597
 
511
- before { MyModel.instance_variable_set(:@history_trackable_options, nil) }
598
+ after :each do
599
+ Object.send(:remove_const, :MyTrackerClass)
600
+ end
512
601
 
513
602
  context 'when options contain tracker_class_name' do
514
603
  context 'when underscored' do
@@ -528,28 +617,37 @@ describe Mongoid::History::Trackable do
528
617
  end
529
618
 
530
619
  describe '#modified_attributes_for_update' do
531
- before(:all) do
532
- ModelOne = Class.new do
620
+ before :each do
621
+ class ModelOne
533
622
  include Mongoid::Document
534
623
  include Mongoid::History::Trackable
624
+
535
625
  store_in collection: :model_ones
536
626
  field :foo
537
- embeds_many :emb_ones, inverse_class_name: 'EmbOne'
627
+
628
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
629
+ embeds_many :emb_ones
630
+ else
631
+ embeds_many :emb_ones, inverse_class_name: 'EmbOne'
632
+ end
538
633
  end
539
634
 
540
- EmbOne = Class.new do
635
+ class EmbOne
541
636
  include Mongoid::Document
542
637
  include Mongoid::History::Trackable
638
+
543
639
  field :em_foo
544
640
  embedded_in :model_one
545
641
  end
546
642
  end
547
643
 
548
- before(:each) do
644
+ after :each do
645
+ Object.send(:remove_const, :ModelOne)
646
+ Object.send(:remove_const, :EmbOne)
647
+ end
648
+
649
+ before :each do
549
650
  model_one.save!
550
- ModelOne.instance_variable_set(:@history_trackable_options, nil)
551
- ModelOne.instance_variable_set(:@trackable_settings, nil)
552
- EmbOne.instance_variable_set(:@trackable_settings, nil)
553
651
  end
554
652
 
555
653
  let(:model_one) { ModelOne.new(foo: 'Foo') }
@@ -560,14 +658,14 @@ describe Mongoid::History::Trackable do
560
658
  before(:each) { allow(model_one).to receive(:changes) { changes } }
561
659
 
562
660
  context 'when not paranoia' do
563
- before(:each) { ModelOne.track_history(on: :emb_ones) }
661
+ before(:each) { ModelOne.track_history(on: :emb_ones, modifier_field_optional: true) }
564
662
  let(:changes) { { 'emb_ones' => [[{ 'em_foo' => 'Foo' }], [{ 'em_foo' => 'Foo-new' }]] } }
565
663
  it { expect(subject['emb_ones'][0]).to eq [{ 'em_foo' => 'Foo' }] }
566
664
  it { expect(subject['emb_ones'][1]).to eq [{ 'em_foo' => 'Foo-new' }] }
567
665
  end
568
666
 
569
667
  context 'when default field for paranoia' do
570
- before(:each) { ModelOne.track_history(on: :emb_ones) }
668
+ before(:each) { ModelOne.track_history(on: :emb_ones, modifier_field_optional: true) }
571
669
  let(:changes) do
572
670
  { 'emb_ones' => [[{ 'em_foo' => 'Foo' }, { 'em_foo' => 'Foo-2', 'deleted_at' => Time.now }],
573
671
  [{ 'em_foo' => 'Foo-new' }, { 'em_foo' => 'Foo-2-new', 'deleted_at' => Time.now }]] }
@@ -578,7 +676,7 @@ describe Mongoid::History::Trackable do
578
676
 
579
677
  context 'when custom field for paranoia' do
580
678
  before(:each) do
581
- ModelOne.track_history on: :emb_ones
679
+ ModelOne.track_history on: :emb_ones, modifier_field_optional: true
582
680
  EmbOne.history_settings paranoia_field: :my_paranoia_field
583
681
  end
584
682
  let(:changes) do
@@ -595,39 +693,29 @@ describe Mongoid::History::Trackable do
595
693
  describe 'fields' do
596
694
  context 'when custom method for changes' do
597
695
  before(:each) do
598
- ModelOne.clear_trackable_memoization
599
696
  ModelOne.track_history(on: :foo, changes_method: :my_changes_method)
600
697
  allow(ModelOne).to receive(:dynamic_enabled?) { false }
601
698
  allow(model_one).to receive(:my_changes_method) { changes }
602
699
  end
700
+
603
701
  let(:changes) { { 'foo' => ['Foo', 'Foo-new'], 'bar' => ['Bar', 'Bar-new'] } }
604
702
  it { is_expected.to eq('foo' => ['Foo', 'Foo-new']) }
605
703
  end
606
704
  end
607
-
608
- after(:all) do
609
- Object.send(:remove_const, :ModelOne)
610
- Object.send(:remove_const, :EmbOne)
611
- end
612
705
  end
613
706
 
614
707
  context 'when options not contain tracker_class_name' do
615
708
  before { MyModel.track_history }
616
709
  it { expect(MyModel.tracker_class).to eq Tracker }
617
710
  end
618
-
619
- after :all do
620
- Object.send(:remove_const, :MyTrackerClass)
621
- end
622
711
  end
623
712
 
624
713
  describe '#track_update' do
625
- before :all do
714
+ before :each do
626
715
  MyModel.track_history(on: :foo, track_update: true)
627
- @persisted_history_options = Mongoid::History.trackable_class_options
628
716
  end
629
- before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
630
- let!(:m) { MyModel.create!(foo: 'bar') }
717
+
718
+ let!(:m) { MyModel.create!(foo: 'bar', modifier: user) }
631
719
 
632
720
  it 'should create history' do
633
721
  expect { m.update_attributes!(foo: 'bar2') }.to change(Tracker, :count).by(1)
@@ -642,12 +730,11 @@ describe Mongoid::History::Trackable do
642
730
  end
643
731
 
644
732
  describe '#track_destroy' do
645
- before :all do
733
+ before :each do
646
734
  MyModel.track_history(on: :foo, track_destroy: true)
647
- @persisted_history_options = Mongoid::History.trackable_class_options
648
735
  end
649
- before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
650
- let!(:m) { MyModel.create!(foo: 'bar') }
736
+
737
+ let!(:m) { MyModel.create!(foo: 'bar', modifier: user) }
651
738
 
652
739
  it 'should create history' do
653
740
  expect { m.destroy }.to change(Tracker, :count).by(1)
@@ -662,15 +749,26 @@ describe Mongoid::History::Trackable do
662
749
  end
663
750
 
664
751
  describe '#track_create' do
665
- before :all do
752
+ before :each do
753
+ class MyModelWithNoModifier
754
+ include Mongoid::Document
755
+ include Mongoid::History::Trackable
756
+
757
+ field :foo
758
+ end
759
+ end
760
+
761
+ after :each do
762
+ Object.send(:remove_const, :MyModelWithNoModifier)
763
+ end
764
+
765
+ before :each do
666
766
  MyModel.track_history(on: :foo, track_create: true)
667
- @persisted_history_options = Mongoid::History.trackable_class_options
668
767
  MyModelWithNoModifier.track_history modifier_field: nil
669
768
  end
670
- before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
671
769
 
672
770
  it 'should create history' do
673
- expect { MyModel.create!(foo: 'bar') }.to change(Tracker, :count).by(1)
771
+ expect { MyModel.create!(foo: 'bar', modifier: user) }.to change(Tracker, :count).by(1)
674
772
  end
675
773
 
676
774
  context 'no modifier_field' do