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,54 +1,65 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Tracker do
4
- before do
5
- @tracker_class_name = Mongoid::History.tracker_class_name
6
- Mongoid::History.tracker_class_name = nil
7
- end
4
+ context 'when included' do
5
+ before :each do
6
+ Mongoid::History.tracker_class_name = nil
8
7
 
9
- it 'should set tracker_class_name when included' do
10
- class MyTracker
11
- include Mongoid::History::Tracker
8
+ class MyTracker
9
+ include Mongoid::History::Tracker
10
+ end
11
+ end
12
+
13
+ after :each do
14
+ Object.send(:remove_const, :MyTracker)
15
+ end
16
+
17
+ it 'should set tracker_class_name when included' do
18
+ expect(Mongoid::History.tracker_class_name).to eq(:my_tracker)
12
19
  end
13
- expect(Mongoid::History.tracker_class_name).to eq(:my_tracker)
14
- end
15
20
 
16
- it 'should set fields defaults' do
17
- class MyTrackerTwo
18
- include Mongoid::History::Tracker
21
+ it 'should set fields defaults' do
22
+ expect(MyTracker.new.association_chain).to eq([])
23
+ expect(MyTracker.new.original).to eq({})
24
+ expect(MyTracker.new.modified).to eq({})
19
25
  end
20
- expect(MyTrackerTwo.new.association_chain).to eq([])
21
- expect(MyTrackerTwo.new.original).to eq({})
22
- expect(MyTrackerTwo.new.modified).to eq({})
23
26
  end
24
27
 
25
28
  describe '#tracked_edits' do
26
- before(:all) do
27
- TrackerOne = Class.new do
28
- def self.name
29
- 'TrackerOne'
30
- end
31
-
29
+ before :each do
30
+ class TrackerOne
32
31
  include Mongoid::History::Tracker
33
32
  end
34
33
 
35
- ModelOne = Class.new do
34
+ class ModelOne
36
35
  include Mongoid::Document
37
36
  include Mongoid::History::Trackable
37
+
38
38
  store_in collection: :model_ones
39
- embeds_many :emb_ones, inverse_class_name: 'EmbOne'
39
+
40
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
41
+ embeds_many :emb_ones
42
+ else
43
+ embeds_many :emb_ones, inverse_class_name: 'EmbOne'
44
+ end
40
45
  end
41
46
 
42
- EmbOne = Class.new do
47
+ class EmbOne
43
48
  include Mongoid::Document
49
+
44
50
  field :em_foo
45
51
  embedded_in :model_one
46
52
  end
47
53
  end
48
54
 
55
+ after :each do
56
+ Object.send(:remove_const, :TrackerOne)
57
+ Object.send(:remove_const, :ModelOne)
58
+ Object.send(:remove_const, :EmbOne)
59
+ end
60
+
49
61
  context 'when embeds_many' do
50
- before(:each) do
51
- ModelOne.instance_variable_set(:@history_trackable_options, nil)
62
+ before :each do
52
63
  ModelOne.track_history(on: :emb_ones)
53
64
  allow(tracker).to receive(:trackable_parent_class) { ModelOne }
54
65
  end
@@ -56,23 +67,29 @@ describe Mongoid::History::Tracker do
56
67
  let(:tracker) { TrackerOne.new }
57
68
 
58
69
  describe '#prepare_tracked_edits_for_embeds_many' do
59
- before(:each) do
60
- tracker.instance_variable_set(:@tracked_edits, nil)
70
+ before :each do
61
71
  allow(tracker).to receive(:tracked_changes) { changes }
62
72
  end
73
+
63
74
  let(:emb_one) { EmbOne.new }
64
75
  let(:emb_one_2) { EmbOne.new }
65
76
  let(:emb_one_3) { EmbOne.new }
66
77
  let(:changes) { {} }
78
+
67
79
  subject { tracker.tracked_edits['embeds_many']['emb_ones'] }
68
80
 
69
81
  context 'when all values present' do
70
82
  let(:changes) do
71
- { 'emb_ones' => { from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' },
72
- { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
73
- to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new' },
74
- { '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }] } }
83
+ {
84
+ 'emb_ones' => {
85
+ from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' },
86
+ { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
87
+ to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new' },
88
+ { '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
89
+ }
90
+ }
75
91
  end
92
+
76
93
  it 'should include :add, :remove, and :modify' do
77
94
  expect(subject['add']).to eq [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
78
95
  expect(subject['remove']).to eq [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' }]
@@ -84,8 +101,12 @@ describe Mongoid::History::Tracker do
84
101
 
85
102
  context 'when value :from blank' do
86
103
  let(:changes) do
87
- { 'emb_ones' => { to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new' },
88
- { '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }] } }
104
+ {
105
+ 'emb_ones' => {
106
+ to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new' },
107
+ { '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
108
+ }
109
+ }
89
110
  end
90
111
  it 'should include :add' do
91
112
  expect(subject['add'].size).to eq 2
@@ -98,8 +119,12 @@ describe Mongoid::History::Tracker do
98
119
 
99
120
  context 'when value :to blank' do
100
121
  let(:changes) do
101
- { 'emb_ones' => { from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' },
102
- { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }] } }
122
+ {
123
+ 'emb_ones' => {
124
+ from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' },
125
+ { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }]
126
+ }
127
+ }
103
128
  end
104
129
  it 'should include :remove' do
105
130
  expect(subject['add']).to be_nil
@@ -112,8 +137,12 @@ describe Mongoid::History::Tracker do
112
137
 
113
138
  context 'when no id common in :from and :to' do
114
139
  let(:changes) do
115
- { 'emb_ones' => { from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' }],
116
- to: [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }] } }
140
+ {
141
+ 'emb_ones' => {
142
+ from: [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' }],
143
+ to: [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
144
+ }
145
+ }
117
146
  end
118
147
  it 'should include :add, and :remove' do
119
148
  expect(subject['add']).to eq [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
@@ -124,10 +153,14 @@ describe Mongoid::History::Tracker do
124
153
 
125
154
  context 'when _id attribute not set' do
126
155
  let(:changes) do
127
- { 'emb_ones' => { from: [{ 'em_foo' => 'Em-Foo' },
128
- { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
129
- to: [{ 'em_foo' => 'Em-Foo-2-new' },
130
- { 'em_foo' => 'Em-Foo-3' }] } }
156
+ {
157
+ 'emb_ones' => {
158
+ from: [{ 'em_foo' => 'Em-Foo' },
159
+ { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
160
+ to: [{ 'em_foo' => 'Em-Foo-2-new' },
161
+ { 'em_foo' => 'Em-Foo-3' }]
162
+ }
163
+ }
131
164
  end
132
165
  it 'should include :add, and :remove' do
133
166
  expect(subject['add']).to eq([{ 'em_foo' => 'Em-Foo-2-new' }, { 'em_foo' => 'Em-Foo-3' }])
@@ -138,8 +171,12 @@ describe Mongoid::History::Tracker do
138
171
 
139
172
  context 'when no change in an object' do
140
173
  let(:changes) do
141
- { 'emb_ones' => { from: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
142
- to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }] } }
174
+ {
175
+ 'emb_ones' => {
176
+ from: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }],
177
+ to: [{ '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }]
178
+ }
179
+ }
143
180
  end
144
181
  it 'should include not :add, :remove, and :modify' do
145
182
  expect(subject['add']).to be_nil
@@ -149,15 +186,5 @@ describe Mongoid::History::Tracker do
149
186
  end
150
187
  end
151
188
  end
152
-
153
- after(:all) do
154
- Object.send(:remove_const, :TrackerOne)
155
- Object.send(:remove_const, :ModelOne)
156
- Object.send(:remove_const, :EmbOne)
157
- end
158
- end
159
-
160
- after do
161
- Mongoid::History.tracker_class_name = @tracker_class_name
162
189
  end
163
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Qian
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-01-16 00:00:00.000000000 Z
13
+ date: 2018-06-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: easy_diff