-
Notifications
You must be signed in to change notification settings - Fork 129
/
integration_spec.rb
976 lines (844 loc) · 33.3 KB
/
integration_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
require 'spec_helper'
describe Mongoid::History do
before :each do
class Post
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
field :title
field :body
field :rating
field :views, type: Integer
embeds_many :comments, store_as: :coms
embeds_one :section, store_as: :sec
embeds_many :tags, cascade_callbacks: true
accepts_nested_attributes_for :tags, allow_destroy: true
track_history on: %i[title body], track_destroy: true
end
class Comment
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
field :t, as: :title
field :body
embedded_in :commentable, polymorphic: true
# BUG: see https://github.com/mongoid/mongoid-history/issues/223, modifier_field_optional should not be necessary
track_history on: %i[title body], scope: :post, track_create: true, track_destroy: true, modifier_field_optional: true
end
class Section
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
field :t, as: :title
embedded_in :post
track_history on: [:title], scope: :post, track_create: true, track_destroy: true
end
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
field :n, as: :name
field :em, as: :email
field :phone
field :address
field :city
field :country
field :aliases, type: Array
track_history except: %i[email updated_at], modifier_field_optional: true
end
class Tag
include Mongoid::Document
# include Mongoid::Timestamps (see: https://github.com/mongoid/mongoid/issues/3078)
include Mongoid::History::Trackable
belongs_to :updated_by, class_name: 'User'
field :title
track_history on: [:title], scope: :post, track_create: true, track_destroy: true, modifier_field: :updated_by
end
class Foo < Comment
end
end
after :each do
Object.send(:remove_const, :Post)
Object.send(:remove_const, :Comment)
Object.send(:remove_const, :Section)
Object.send(:remove_const, :User)
Object.send(:remove_const, :Tag)
Object.send(:remove_const, :Foo)
end
let(:user) { User.create!(name: 'Aaron', email: '[email protected]', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') }
let(:another_user) { User.create!(name: 'Another Guy', email: '[email protected]') }
let(:post) { Post.create!(title: 'Test', body: 'Post', modifier: user, views: 100) }
let(:comment) { post.comments.create!(title: 'test', body: 'comment', modifier: user) }
let(:tag) { Tag.create!(title: 'test', updated_by: user) }
describe 'track' do
describe 'on creation' do
it 'should have one history track in comment' do
expect(comment.history_tracks.count).to eq(1)
end
it 'should assign title and body on modified' do
expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment')
end
it 'should not assign title and body on original' do
expect(comment.history_tracks.first.original).to eq({})
end
it 'should assign modifier' do
expect(comment.history_tracks.first.modifier.id).to eq(user.id)
end
it 'should assign version' do
expect(comment.history_tracks.first.version).to eq(1)
end
it 'should assign scope' do
expect(comment.history_tracks.first.scope).to eq('post')
end
it 'should assign method' do
expect(comment.history_tracks.first.action).to eq('create')
end
it 'should assign association_chain' do
expected = [
{ 'id' => post.id, 'name' => 'Post' },
{ 'id' => comment.id, 'name' => 'coms' }
]
expect(comment.history_tracks.first.association_chain).to eq(expected)
end
end
describe 'on destruction' do
it 'should have two history track records in post' do
post # This will create history track records for creation
expect do
post.destroy
end.to change(Tracker, :count).by(1)
end
it 'should assign destroy on track record' do
post.destroy
expect(post.history_tracks.last.action).to eq('destroy')
end
it 'should return affected attributes from track record' do
post.destroy
expect(post.history_tracks.last.affected['title']).to eq('Test')
end
it 'should no-op on repeated calls to destroy' do
post.destroy
expect do
post.destroy
end.not_to change(Tracker, :count)
end
end
describe 'on update non-embedded' do
it 'should create a history track if changed attributes match tracked attributes' do
post # This will create history track records for creation
expect do
post.update_attributes!(title: 'Another Test')
end.to change(Tracker, :count).by(1)
end
it 'should not create a history track if changed attributes do not match tracked attributes' do
post # This will create history track records for creation
expect do
post.update_attributes!(rating: 'untracked')
end.to change(Tracker, :count).by(0)
end
it 'should assign modified fields' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.last.modified).to eq(
'title' => 'Another Test'
)
end
it 'should assign method field' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.last.action).to eq('update')
end
it 'should assign original fields' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.last.original).to eq(
'title' => 'Test'
)
end
it 'should assign modifier' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.first.modifier.id).to eq(user.id)
end
it 'should assign version on history tracks' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.first.version).to eq(1)
end
it 'should assign version on post' do
expect(post.version).to eq(1) # Created
post.update_attributes!(title: 'Another Test')
expect(post.version).to eq(2) # Updated
end
it 'should assign scope' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.first.scope).to eq('post')
end
it 'should assign association_chain' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.last.association_chain).to eq([{ 'id' => post.id, 'name' => 'Post' }])
end
it 'should exclude defined options' do
name = user.name
user.update_attributes!(name: 'Aaron2', email: '[email protected]')
expect(user.history_tracks.last.original.keys).to eq(['n'])
expect(user.history_tracks.last.original['n']).to eq(name)
expect(user.history_tracks.last.modified.keys).to eq(['n'])
expect(user.history_tracks.last.modified['n']).to eq(user.name)
end
it 'should undo field changes' do
name = user.name
user.update_attributes!(name: 'Aaron2', email: '[email protected]')
user.history_tracks.last.undo! nil
expect(user.reload.name).to eq(name)
end
it 'should undo non-existing field changes' do
post = Post.create!(modifier: user, views: 100)
expect(post.reload.title).to be_nil
post.update_attributes!(title: 'Aaron2')
expect(post.reload.title).to eq('Aaron2')
post.history_tracks.last.undo! user
expect(post.reload.title).to be_nil
end
it 'should track array changes' do
aliases = user.aliases
user.update_attributes!(aliases: %w[bob joe])
expect(user.history_tracks.last.original['aliases']).to eq(aliases)
expect(user.history_tracks.last.modified['aliases']).to eq(user.aliases)
end
it 'should undo array changes' do
aliases = user.aliases
user.update_attributes!(aliases: %w[bob joe])
user.history_tracks.last.undo! nil
expect(user.reload.aliases).to eq(aliases)
end
end
describe '#tracked_changes' do
context 'create action' do
subject { tag.history_tracks.first.tracked_changes }
it 'consider all fields values as :to' do
expect(subject[:title]).to eq({ to: 'test' }.with_indifferent_access)
end
end
context 'destroy action' do
subject do
tag.destroy
tag.history_tracks.last.tracked_changes
end
it 'consider all fields values as :from' do
expect(subject[:title]).to eq({ from: 'test' }.with_indifferent_access)
end
end
context 'update action' do
subject { user.history_tracks.last.tracked_changes }
before do
user.update_attributes!(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
end
it { is_expected.to be_a HashWithIndifferentAccess }
it 'should track changed field' do
expect(subject[:n]).to eq({ from: 'Aaron', to: 'Aaron2' }.with_indifferent_access)
end
it 'should track added field' do
expect(subject[:phone]).to eq({ to: '867-5309' }.with_indifferent_access)
end
it 'should track removed field' do
expect(subject[:city]).to eq({ from: 'Toronto' }.with_indifferent_access)
end
it 'should not consider blank as removed' do
expect(subject[:country]).to eq({ from: 'Canada', to: '' }.with_indifferent_access)
end
it 'should track changed array field' do
expect(subject[:aliases]).to eq({ from: ['bob'], to: ['', 'bill', 'james'] }.with_indifferent_access)
end
it 'should not track unmodified field' do
expect(subject[:address]).to be_nil
end
it 'should not track untracked fields' do
expect(subject[:email]).to be_nil
end
end
end
describe '#tracked_edits' do
context 'create action' do
subject { tag.history_tracks.first.tracked_edits }
it 'consider all edits as ;add' do
expect(subject[:add]).to eq({ title: 'test' }.with_indifferent_access)
end
end
context 'destroy action' do
subject do
tag.destroy
tag.history_tracks.last.tracked_edits
end
it 'consider all edits as ;remove' do
expect(subject[:remove]).to eq({ title: 'test' }.with_indifferent_access)
end
end
context 'update action' do
subject { user.history_tracks.last.tracked_edits }
before do
user.update_attributes!(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
end
it { is_expected.to be_a HashWithIndifferentAccess }
it 'should track changed field' do
expect(subject[:modify]).to eq({ n: { from: 'Aaron', to: 'Aaron2' } }.with_indifferent_access)
end
it 'should track added field' do
expect(subject[:add]).to eq({ phone: '867-5309' }.with_indifferent_access)
end
it 'should track removed field and consider blank as removed' do
expect(subject[:remove]).to eq({ city: 'Toronto', country: 'Canada' }.with_indifferent_access)
end
it 'should track changed array field' do
expect(subject[:array]).to eq({ aliases: { remove: ['bob'], add: ['', 'bill', 'james'] } }.with_indifferent_access)
end
it 'should not track unmodified field' do
%w[add modify remove array].each do |edit|
expect(subject[edit][:address]).to be_nil
end
end
it 'should not track untracked fields' do
%w[add modify remove array].each do |edit|
expect(subject[edit][:email]).to be_nil
end
end
end
context 'with empty values' do
before do
allow(subject).to receive(:trackable_parent_class) { Tracker }
allow(Tracker).to receive(:tracked_embeds_many?) { false }
end
subject { Tracker.new }
it 'should skip empty values' do
allow(subject).to receive(:tracked_changes) { { name: { to: '', from: [] }, city: { to: 'Toronto', from: '' } } }
expect(subject.tracked_edits).to eq({ add: { city: 'Toronto' } }.with_indifferent_access)
end
end
end
describe 'on update non-embedded twice' do
it 'should assign version on post' do
expect(post.version).to eq(1)
post.update_attributes!(title: 'Test2')
post.update_attributes!(title: 'Test3')
expect(post.version).to eq(3)
end
it 'should create a history track if changed attributes match tracked attributes' do
post # Created
expect do
post.update_attributes!(title: 'Test2')
post.update_attributes!(title: 'Test3')
end.to change(Tracker, :count).by(2)
end
it 'should create a history track of version 2' do
post.update_attributes!(title: 'Test2')
post.update_attributes!(title: 'Test3')
expect(post.history_tracks.where(version: 2).first).not_to be_nil
end
it 'should assign modified fields' do
post.update_attributes!(title: 'Test2')
post.update_attributes!(title: 'Test3')
expect(post.history_tracks.where(version: 3).first.modified).to eq(
'title' => 'Test3'
)
end
it 'should assign original fields' do
post.update_attributes!(title: 'Test2')
post.update_attributes!(title: 'Test3')
expect(post.history_tracks.where(version: 3).first.original).to eq(
'title' => 'Test2'
)
end
it 'should assign modifier' do
post.update_attributes!(title: 'Another Test', modifier: another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end
describe 'on update embedded 1..N (embeds_many)' do
it 'should assign version on comment' do
comment.update_attributes!(title: 'Test2')
expect(comment.version).to eq(2) # first track generated on creation
end
it 'should create a history track of version 2' do
comment.update_attributes!(title: 'Test2')
expect(comment.history_tracks.where(version: 2).first).not_to be_nil
end
it 'should assign modified fields' do
comment.update_attributes!(t: 'Test2')
expect(comment.history_tracks.where(version: 2).first.modified).to eq(
't' => 'Test2'
)
end
it 'should assign original fields' do
comment.update_attributes!(title: 'Test2')
expect(comment.history_tracks.where(version: 2).first.original).to eq(
't' => 'test'
)
end
it 'should be possible to undo from parent' do
comment.update_attributes!(title: 'Test 2')
user
post.history_tracks.last.undo!(user)
comment.reload
expect(comment.title).to eq('test')
end
it 'should assign modifier' do
post.update_attributes!(title: 'Another Test', modifier: another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end
describe 'on update embedded 1..1 (embeds_one)' do
let(:section) { Section.new(title: 'Technology', modifier: user) }
before(:each) do
post.section = section
post.modifier = user
post.save!
post.reload
post.section
end
it 'should assign version on create section' do
expect(section.version).to eq(1)
end
it 'should assign version on section' do
section.update_attributes!(title: 'Technology 2')
expect(section.version).to eq(2) # first track generated on creation
end
it 'should create a history track of version 2' do
section.update_attributes!(title: 'Technology 2')
expect(section.history_tracks.where(version: 2).first).not_to be_nil
end
it 'should assign modified fields' do
section.update_attributes!(title: 'Technology 2')
expect(section.history_tracks.where(version: 2).first.modified).to eq(
't' => 'Technology 2'
)
end
it 'should assign original fields' do
section.update_attributes!(title: 'Technology 2')
expect(section.history_tracks.where(version: 2).first.original).to eq(
't' => 'Technology'
)
end
it 'should be possible to undo from parent' do
section.update_attributes!(title: 'Technology 2')
post.history_tracks.last.undo!(user)
section.reload
expect(section.title).to eq('Technology')
end
it 'should assign modifier' do
section.update_attributes!(title: 'Business', modifier: another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end
describe 'on destroy embedded' do
it 'should be possible to re-create destroyed embedded' do
comment.destroy
comment.history_tracks.last.undo!(user)
post.reload
expect(post.comments.first.title).to eq('test')
end
it 'should be possible to re-create destroyed embedded from parent' do
comment.destroy
post.history_tracks.last.undo!(user)
post.reload
expect(post.comments.first.title).to eq('test')
end
it 'should be possible to destroy after re-create embedded from parent' do
comment.destroy
post.history_tracks[-1].undo!(user)
post.history_tracks[-1].undo!(user)
post.reload
expect(post.comments.count).to eq(0)
end
it 'should be possible to create with redo after undo create embedded from parent' do
comment # initialize
post.comments.create!(title: 'The second one', modifier: user)
track = post.history_tracks[2]
expect(post.reload.comments.count).to eq 2
track.undo!(user)
expect(post.reload.comments.count).to eq 1
track.redo!(user)
expect(post.reload.comments.count).to eq 2
end
end
describe 'embedded with cascading callbacks' do
let(:tag_foo) { post.tags.create!(title: 'foo', updated_by: user) }
let(:tag_bar) { post.tags.create!(title: 'bar', updated_by: user) }
it 'should allow an update through the parent model' do
update_hash = { 'post' => { 'tags_attributes' => { '1234' => { 'id' => tag_bar.id, 'title' => 'baz' } } } }
post.update_attributes!(update_hash['post'])
expect(post.tags.last.title).to eq('baz')
end
it 'should be possible to destroy through parent model using canoncial _destroy macro' do
tag_foo
tag_bar # initialize
expect(post.tags.count).to eq(2)
update_hash = { 'post' => { 'tags_attributes' => { '1234' => { 'id' => tag_bar.id, 'title' => 'baz', '_destroy' => 'true' } } } }
post.update_attributes!(update_hash['post'])
expect(post.tags.count).to eq(1)
expect(post.history_tracks.to_a.last.action).to eq('destroy')
end
it 'should write relationship name for association_chain hiearchy instead of class name when using _destroy macro' do
update_hash = { 'tags_attributes' => { '1234' => { 'id' => tag_foo.id, '_destroy' => '1' } } }
post.update_attributes!(update_hash)
# historically this would have evaluated to 'Tags' and an error would be thrown
# on any call that walked up the association_chain, e.g. 'trackable'
expect(tag_foo.history_tracks.last.association_chain.last['name']).to eq('tags')
expect { tag_foo.history_tracks.last.trackable }.not_to raise_error
end
end
describe 'non-embedded' do
it 'should undo changes' do
post.update_attributes!(title: 'Test2')
post.history_tracks.where(version: 2).last.undo!(user)
post.reload
expect(post.title).to eq('Test')
end
it 'should undo destruction' do
post.destroy
post.history_tracks.where(version: 2).last.undo!(user)
expect(Post.find(post.id).title).to eq('Test')
end
it 'should create a new history track after undo' do
comment # initialize
post.update_attributes!(title: 'Test2')
post.history_tracks.last.undo!(user)
post.reload
expect(post.history_tracks.count).to eq(4)
end
it 'should assign user as the modifier of the newly created history track' do
post.update_attributes!(title: 'Test2')
post.history_tracks.where(version: 2).last.undo!(user)
post.reload
expect(post.history_tracks.where(version: 2).last.modifier.id).to eq(user.id)
end
it 'should stay the same after undo and redo' do
post.update_attributes!(title: 'Test2')
track = post.history_tracks.last
track.undo!(user)
track.redo!(user)
post2 = Post.where(_id: post.id).first
expect(post.title).to eq(post2.title)
end
it 'should be destroyed after undo and redo' do
post.destroy
track = post.history_tracks.where(version: 2).last
track.undo!(user)
track.redo!(user)
expect(Post.where(_id: post.id).first).to be_nil
end
end
describe 'embedded' do
it 'should undo changes' do
comment.update_attributes!(title: 'Test2')
comment.history_tracks.where(version: 2).first.undo!(user)
comment.reload
expect(comment.title).to eq('test')
end
it 'should create a new history track after undo' do
comment.update_attributes!(title: 'Test2')
comment.history_tracks.where(version: 2).first.undo!(user)
comment.reload
expect(comment.history_tracks.count).to eq(3)
end
it 'should assign user as the modifier of the newly created history track' do
comment.update_attributes!(title: 'Test2')
comment.history_tracks.where(version: 2).first.undo!(user)
comment.reload
expect(comment.history_tracks.where(version: 3).first.modifier.id).to eq(user.id)
end
it 'should stay the same after undo and redo' do
comment.update_attributes!(title: 'Test2')
track = comment.history_tracks.where(version: 2).first
track.undo!(user)
track.redo!(user)
comment.reload
expect(comment.title).to eq('Test2')
end
end
describe 'trackables' do
before :each do
comment.update_attributes!(title: 'Test2') # version == 2
comment.update_attributes!(title: 'Test3') # version == 3
comment.update_attributes!(title: 'Test4') # version == 4
end
describe 'undo' do
{ 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods|
methods.each do |method|
context (method || 'instance').to_s do
it 'recognizes :from, :to options' do
comment.send test_method, user, from: 4, to: 2
comment.send(method) if method
expect(comment.title).to eq('test')
end
it 'recognizes parameter as version number' do
comment.send test_method, user, 3
comment.send(method) if method
expect(comment.title).to eq('Test2')
end
it 'should undo last version when no parameter is specified' do
comment.send test_method, user
comment.send(method) if method
expect(comment.title).to eq('Test3')
end
it 'recognizes :last options' do
comment.send test_method, user, last: 2
comment.send(method) if method
expect(comment.title).to eq('Test2')
end
if Mongoid::Compatibility::Version.mongoid3?
context 'protected attributes' do
before :each do
Comment.attr_accessible(nil)
end
after :each do
Comment.attr_protected(nil)
end
it 'should undo last version when no parameter is specified on protected attributes' do
comment.send test_method, user
comment.send(method) if method
expect(comment.title).to eq('Test3')
end
it 'recognizes :last options on model with protected attributes' do
comment.send test_method, user, last: 2
comment.send(method) if method
expect(comment.title).to eq('Test2')
end
end
end
end
end
end
end
describe 'redo' do
[nil, :reload].each do |method|
context (method || 'instance').to_s do
before :each do
comment.update_attributes!(title: 'Test5')
end
it 'should recognize :from, :to options' do
comment.redo! user, from: 2, to: 4
comment.send(method) if method
expect(comment.title).to eq('Test4')
end
it 'should recognize parameter as version number' do
comment.redo! user, 2
comment.send(method) if method
expect(comment.title).to eq('Test2')
end
it 'should redo last version when no parameter is specified' do
comment.redo! user
comment.send(method) if method
expect(comment.title).to eq('Test5')
end
it 'should recognize :last options' do
comment.redo! user, last: 1
comment.send(method) if method
expect(comment.title).to eq('Test5')
end
if Mongoid::Compatibility::Version.mongoid3?
context 'protected attributes' do
before :each do
Comment.attr_accessible(nil)
end
after :each do
Comment.attr_protected(nil)
end
it 'should recognize parameter as version number' do
comment.redo! user, 2
comment.send(method) if method
expect(comment.title).to eq('Test2')
end
it 'should recognize :from, :to options' do
comment.redo! user, from: 2, to: 4
comment.send(method) if method
expect(comment.title).to eq('Test4')
end
end
end
end
end
end
end
describe 'embedded with a polymorphic trackable' do
let(:foo) { Foo.new(title: 'a title', body: 'a body', modifier: user) }
before :each do
post.comments << foo
post.save!
end
it 'should assign interface name in association chain' do
foo.update_attribute(:body, 'a changed body')
expected_root = { 'name' => 'Post', 'id' => post.id }
expected_node = { 'name' => 'coms', 'id' => foo.id }
expect(foo.history_tracks.first.association_chain).to eq([expected_root, expected_node])
end
end
describe '#trackable_parent_class' do
context 'a non-embedded model' do
it 'should return the trackable parent class' do
expect(tag.history_tracks.first.trackable_parent_class).to eq(Tag)
end
it 'should return the parent class even if the trackable is deleted' do
tracker = tag.history_tracks.first
tag.destroy
expect(tracker.trackable_parent_class).to eq(Tag)
end
end
context 'an embedded model' do
it 'should return the trackable parent class' do
comment.update_attributes!(title: 'Foo')
expect(comment.history_tracks.first.trackable_parent_class).to eq(Post)
end
it 'should return the parent class even if the trackable is deleted' do
tracker = comment.history_tracks.first
comment.destroy
expect(tracker.trackable_parent_class).to eq(Post)
end
end
end
describe 'when default scope is present' do
before :each do
class Post
default_scope -> { where(title: nil) }
end
class Comment
default_scope -> { where(title: nil) }
end
class User
default_scope -> { where(name: nil) }
end
class Tag
default_scope -> { where(title: nil) }
end
end
describe 'post' do
it 'should correctly undo and redo' do
post.update_attributes!(title: 'a new title')
track = post.history_tracks.last
track.undo! user
expect(post.reload.title).to eq('Test')
track.redo! user
expect(post.reload.title).to eq('a new title')
end
it 'should stay the same after undo and redo' do
post.update_attributes!(title: 'testing')
track = post.history_tracks.last
track.undo! user
track.redo! user
expect(post.reload.title).to eq('testing')
end
end
describe 'comment' do
it 'should correctly undo and redo' do
comment.update_attributes!(title: 'a new title')
track = comment.history_tracks.last
track.undo! user
expect(comment.reload.title).to eq('test')
track.redo! user
expect(comment.reload.title).to eq('a new title')
end
it 'should stay the same after undo and redo' do
comment.update_attributes!(title: 'testing')
track = comment.history_tracks.last
track.undo! user
track.redo! user
expect(comment.reload.title).to eq('testing')
end
end
describe 'user' do
it 'should correctly undo and redo' do
user.update_attributes!(name: 'a new name')
track = user.history_tracks.last
track.undo! user
expect(user.reload.name).to eq('Aaron')
track.redo! user
expect(user.reload.name).to eq('a new name')
end
it 'should stay the same after undo and redo' do
user.update_attributes!(name: 'testing')
track = user.history_tracks.last
track.undo! user
track.redo! user
expect(user.reload.name).to eq('testing')
end
end
describe 'tag' do
it 'should correctly undo and redo' do
tag.update_attributes!(title: 'a new title')
track = tag.history_tracks.last
track.undo! user
expect(tag.reload.title).to eq('test')
track.redo! user
expect(tag.reload.title).to eq('a new title')
end
it 'should stay the same after undo and redo' do
tag.update_attributes!(title: 'testing')
track = tag.history_tracks.last
track.undo! user
track.redo! user
expect(tag.reload.title).to eq('testing')
end
end
end
describe 'overriden changes_method with additional fields' do
before :each do
class OverriddenChangesMethod
include Mongoid::Document
include Mongoid::History::Trackable
track_history on: [:foo], changes_method: :my_changes
def my_changes
{ foo: %w[bar baz] }
end
end
end
after :each do
Object.send(:remove_const, :OverriddenChangesMethod)
end
it 'should add foo to the changes history' do
o = OverriddenChangesMethod.create(modifier: user)
o.save!
track = o.history_tracks.last
expect(track.modified).to eq('foo' => 'baz')
expect(track.original).to eq('foo' => 'bar')
end
end
describe 'localized fields' do
before :each do
class Sausage
include Mongoid::Document
include Mongoid::History::Trackable
field :flavour, localize: true
track_history on: [:flavour], track_destroy: true, modifier_field_optional: true
end
end
after :each do
Object.send(:remove_const, :Sausage)
end
it 'should correctly undo and redo' do
pending unless Sausage.respond_to?(:localized_fields)
sausage = Sausage.create!(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' }, modifier: user)
sausage.update_attributes!(flavour: 'Guinness')
track = sausage.history_tracks.last
track.undo! user
expect(sausage.reload.flavour).to eq('Apple')
track.redo! user
expect(sausage.reload.flavour).to eq('Guinness')
sausage.destroy
expect(sausage.history_tracks.last.action).to eq('destroy')
sausage.history_tracks.last.undo! user
expect(sausage.reload.flavour).to eq('Guinness')
end
end
describe 'changing collection' do
before :each do
class Fish
include Mongoid::Document
include Mongoid::History::Trackable
track_history on: [:species], modifier_field_optional: true
store_in collection: :animals
field :species
end
end
after :each do
Object.send(:remove_const, :Fish)
end
it 'should track history' do
Fish.new.save!
end
end
end
end