mongoid-history 0.8.0 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/.coveralls.yml +1 -1
  3. data/.document +5 -5
  4. data/.github/workflows/test.yml +72 -0
  5. data/.gitignore +46 -46
  6. data/.rspec +2 -2
  7. data/.rubocop.yml +6 -6
  8. data/.rubocop_todo.yml +99 -101
  9. data/CHANGELOG.md +173 -144
  10. data/CONTRIBUTING.md +117 -118
  11. data/Dangerfile +1 -1
  12. data/Gemfile +49 -37
  13. data/LICENSE.txt +20 -20
  14. data/README.md +609 -595
  15. data/RELEASING.md +66 -67
  16. data/Rakefile +24 -24
  17. data/UPGRADING.md +53 -34
  18. data/lib/mongoid/history/attributes/base.rb +72 -72
  19. data/lib/mongoid/history/attributes/create.rb +45 -50
  20. data/lib/mongoid/history/attributes/destroy.rb +34 -34
  21. data/lib/mongoid/history/attributes/update.rb +104 -45
  22. data/lib/mongoid/history/options.rb +177 -179
  23. data/lib/mongoid/history/trackable.rb +588 -521
  24. data/lib/mongoid/history/tracker.rb +247 -244
  25. data/lib/mongoid/history/version.rb +5 -5
  26. data/lib/mongoid/history.rb +77 -52
  27. data/lib/mongoid-history.rb +1 -1
  28. data/mongoid-history.gemspec +25 -25
  29. data/perf/benchmark_modified_attributes_for_create.rb +65 -0
  30. data/perf/gc_suite.rb +21 -0
  31. data/spec/integration/embedded_in_polymorphic_spec.rb +112 -135
  32. data/spec/integration/integration_spec.rb +976 -942
  33. data/spec/integration/multi_relation_spec.rb +47 -53
  34. data/spec/integration/multiple_trackers_spec.rb +68 -71
  35. data/spec/integration/nested_embedded_documents_spec.rb +64 -84
  36. data/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb +124 -0
  37. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +115 -127
  38. data/spec/integration/subclasses_spec.rb +47 -29
  39. data/spec/integration/track_history_order_spec.rb +84 -52
  40. data/spec/integration/validation_failure_spec.rb +76 -63
  41. data/spec/spec_helper.rb +32 -25
  42. data/spec/support/error_helpers.rb +7 -0
  43. data/spec/support/mongoid.rb +11 -11
  44. data/spec/support/mongoid_history.rb +12 -13
  45. data/spec/unit/attributes/base_spec.rb +141 -150
  46. data/spec/unit/attributes/create_spec.rb +342 -315
  47. data/spec/unit/attributes/destroy_spec.rb +228 -218
  48. data/spec/unit/attributes/update_spec.rb +342 -321
  49. data/spec/unit/callback_options_spec.rb +165 -159
  50. data/spec/unit/embedded_methods_spec.rb +87 -69
  51. data/spec/unit/history_spec.rb +58 -35
  52. data/spec/unit/my_instance_methods_spec.rb +555 -485
  53. data/spec/unit/options_spec.rb +365 -327
  54. data/spec/unit/singleton_methods_spec.rb +406 -338
  55. data/spec/unit/store/default_store_spec.rb +11 -11
  56. data/spec/unit/store/request_store_spec.rb +13 -13
  57. data/spec/unit/trackable_spec.rb +1057 -689
  58. data/spec/unit/tracker_spec.rb +190 -163
  59. metadata +13 -8
  60. data/.travis.yml +0 -35
@@ -1,218 +1,228 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::History::Attributes::Destroy do
4
- let(:model_one) do
5
- Class.new do
6
- include Mongoid::Document
7
- include Mongoid::History::Trackable
8
- store_in collection: :model_ones
9
- field :foo
10
- field :b, as: :bar
11
- def self.name
12
- 'ModelOne'
13
- end
14
- end
15
- end
16
-
17
- let(:obj_one) { model_one.new }
18
- let(:base) { described_class.new(obj_one) }
19
- subject { base }
20
-
21
- describe '#attributes' do
22
- subject { base.attributes }
23
-
24
- describe '#fields' do
25
- before(:each) do
26
- model_one.instance_variable_set(:@history_trackable_options, nil)
27
- model_one.track_history on: :foo
28
- obj_one.save!
29
- end
30
- let(:obj_one) { model_one.new(foo: 'Foo', bar: 'Bar') }
31
- it { is_expected.to eq('_id' => [obj_one._id, nil], 'foo' => ['Foo', nil], 'version' => [1, nil]) }
32
- end
33
-
34
- describe '#insert_embeds_one_changes' do
35
- before(:all) do
36
- # Need class name constant. So, defining class like this
37
- class ModelTwo
38
- include Mongoid::Document
39
- include Mongoid::History::Trackable
40
- store_in collection: :model_twos
41
- embeds_one :emb_two
42
- track_history on: :fields
43
- end
44
-
45
- class EmbTwo
46
- include Mongoid::Document
47
- field :em_foo
48
- field :em_bar
49
- embedded_in :model_two
50
- end
51
- end
52
-
53
- before(:each) { ModelTwo.clear_trackable_memoization }
54
- let(:obj_two) { ModelTwo.new(emb_two: emb_obj_two) }
55
- let(:emb_obj_two) { EmbTwo.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
56
- let(:base) { described_class.new(obj_two) }
57
-
58
- context 'when relation tracked' do
59
- before(:each) do
60
- ModelTwo.track_history on: :emb_two
61
- obj_two.save!
62
- end
63
- it { expect(subject['emb_two']).to eq [{ '_id' => emb_obj_two._id, 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, nil] }
64
- end
65
-
66
- context 'when relation not tracked' do
67
- before(:each) do
68
- ModelTwo.track_history on: :fields
69
- allow(ModelTwo).to receive(:dynamic_enabled?) { false }
70
- obj_two.save!
71
- end
72
- it { expect(subject['emb_two']).to be_nil }
73
- end
74
-
75
- context 'when relation with alias' do
76
- before(:all) do
77
- # Need class name constant. So, defining class like this
78
- class ModelThree
79
- include Mongoid::Document
80
- include Mongoid::History::Trackable
81
- store_in collection: :model_threes
82
- embeds_one :emb_three, store_as: :emtr
83
- end
84
-
85
- class EmbThree
86
- include Mongoid::Document
87
- field :em_foo
88
- embedded_in :model_three
89
- end
90
- end
91
-
92
- before(:each) do
93
- ModelThree.instance_variable_set(:@history_trackable_options, nil)
94
- ModelThree.track_history on: :emb_three
95
- obj_three.save!
96
- end
97
-
98
- let(:obj_three) { ModelThree.new(emb_three: emb_obj_three) }
99
- let(:emb_obj_three) { EmbThree.new(em_foo: 'Em-Foo') }
100
- let(:base) { described_class.new(obj_three) }
101
- it { expect(subject['emb_three']).to eq [{ '_id' => emb_obj_three._id, 'em_foo' => 'Em-Foo' }, nil] }
102
-
103
- after(:all) do
104
- Object.send(:remove_const, :ModelThree)
105
- Object.send(:remove_const, :EmbThree)
106
- end
107
- end
108
-
109
- context 'relation with permitted attributes' do
110
- before(:each) do
111
- ModelTwo.track_history on: [{ emb_two: :em_foo }]
112
- obj_two.save!
113
- end
114
- it { expect(subject['emb_two']).to eq [{ '_id' => emb_obj_two._id, 'em_foo' => 'Em-Foo' }, nil] }
115
- end
116
-
117
- context 'when relation object not built' do
118
- before(:each) do
119
- ModelTwo.track_history on: :emb_two
120
- obj_two.save!
121
- end
122
- let(:obj_two) { ModelTwo.new }
123
- it { expect(subject['emb_two']).to be_nil }
124
- end
125
-
126
- after(:all) do
127
- Object.send(:remove_const, :ModelTwo)
128
- Object.send(:remove_const, :EmbTwo)
129
- end
130
- end
131
-
132
- describe '#insert_embeds_many_changes' do
133
- context 'Case 1:' do
134
- before(:all) do
135
- class ModelTwo
136
- include Mongoid::Document
137
- include Mongoid::History::Trackable
138
- embeds_many :em_twos
139
- track_history on: :fields
140
- end
141
-
142
- class EmTwo
143
- include Mongoid::Document
144
- field :em_foo
145
- field :em_bar
146
- embedded_in :model_two
147
- end
148
- end
149
-
150
- let(:obj_two) { ModelTwo.new(em_twos: [em_obj_two]) }
151
- let(:em_obj_two) { EmTwo.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
152
- let(:base) { described_class.new(obj_two) }
153
-
154
- context 'when relation tracked' do
155
- before(:each) do
156
- ModelTwo.clear_trackable_memoization
157
- ModelTwo.track_history on: :em_twos
158
- end
159
- it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], nil] }
160
- end
161
-
162
- context 'when relation not tracked' do
163
- before(:each) do
164
- ModelTwo.clear_trackable_memoization
165
- ModelTwo.track_history on: :fields
166
- end
167
- it { expect(subject['em_twos']).to be_nil }
168
- end
169
-
170
- context 'when relation with permitted attributes for tracking' do
171
- before(:each) do
172
- ModelTwo.clear_trackable_memoization
173
- ModelTwo.track_history on: { em_twos: :em_foo }
174
- end
175
- it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo' }], nil] }
176
- end
177
-
178
- after(:all) do
179
- Object.send(:remove_const, :ModelTwo)
180
- Object.send(:remove_const, :EmTwo)
181
- end
182
- end
183
-
184
- context 'when relation with alias' do
185
- before(:all) do
186
- class ModelTwo
187
- include Mongoid::Document
188
- include Mongoid::History::Trackable
189
- embeds_many :em_twos, store_as: :emws
190
- track_history on: :fields
191
- end
192
-
193
- class EmTwo
194
- include Mongoid::Document
195
- field :em_foo
196
- embedded_in :model_two
197
- end
198
- end
199
-
200
- before(:each) do
201
- ModelTwo.clear_trackable_memoization
202
- ModelTwo.track_history on: :em_twos
203
- end
204
-
205
- let(:obj_two) { ModelTwo.new(em_twos: [em_obj_two]) }
206
- let(:em_obj_two) { EmTwo.new(em_foo: 'Em-Foo') }
207
- let(:base) { described_class.new(obj_two) }
208
-
209
- it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo' }], nil] }
210
-
211
- after(:all) do
212
- Object.send(:remove_const, :ModelTwo)
213
- Object.send(:remove_const, :EmTwo)
214
- end
215
- end
216
- end
217
- end
218
- end
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::History::Attributes::Destroy do
4
+ before :each do
5
+ class ModelOne
6
+ include Mongoid::Document
7
+ include Mongoid::History::Trackable
8
+
9
+ store_in collection: :model_ones
10
+
11
+ field :foo
12
+ field :b, as: :bar
13
+
14
+ track_history on: :foo, modifier_field_optional: true
15
+ end
16
+ end
17
+
18
+ after :each do
19
+ Object.send(:remove_const, :ModelOne)
20
+ end
21
+
22
+ let(:obj_one) { ModelOne.new }
23
+ let(:base) { described_class.new(obj_one) }
24
+ subject { base }
25
+
26
+ describe '#attributes' do
27
+ subject { base.attributes }
28
+
29
+ describe '#fields' do
30
+ before :each do
31
+ obj_one.save!
32
+ end
33
+
34
+ let(:obj_one) { ModelOne.new(foo: 'Foo', bar: 'Bar') }
35
+ it { is_expected.to eq('_id' => [obj_one._id, nil], 'foo' => ['Foo', nil], 'version' => [1, nil]) }
36
+ end
37
+
38
+ describe '#insert_embeds_one_changes' do
39
+ before :each do
40
+ class ModelTwo
41
+ include Mongoid::Document
42
+ include Mongoid::History::Trackable
43
+
44
+ store_in collection: :model_twos
45
+
46
+ embeds_one :emb_two
47
+
48
+ track_history on: :fields, modifier_field_optional: true
49
+ end
50
+
51
+ class EmbTwo
52
+ include Mongoid::Document
53
+
54
+ field :em_foo
55
+ field :em_bar
56
+
57
+ embedded_in :model_two
58
+ end
59
+ end
60
+
61
+ after :each do
62
+ Object.send(:remove_const, :ModelTwo)
63
+ Object.send(:remove_const, :EmbTwo)
64
+ end
65
+
66
+ let(:obj_two) { ModelTwo.new(emb_two: emb_obj_two) }
67
+ let(:emb_obj_two) { EmbTwo.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
68
+ let(:base) { described_class.new(obj_two) }
69
+
70
+ context 'when relation tracked' do
71
+ before :each do
72
+ ModelTwo.track_history on: :emb_two, modifier_field_optional: true
73
+ obj_two.save!
74
+ end
75
+ it { expect(subject['emb_two']).to eq [{ '_id' => emb_obj_two._id, 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, nil] }
76
+ end
77
+
78
+ context 'when relation not tracked' do
79
+ before :each do
80
+ ModelTwo.track_history on: :fields, modifier_field_optional: true
81
+ allow(ModelTwo).to receive(:dynamic_enabled?) { false }
82
+ obj_two.save!
83
+ end
84
+ it { expect(subject['emb_two']).to be_nil }
85
+ end
86
+
87
+ context 'when relation with alias' do
88
+ before :each do
89
+ class ModelThree
90
+ include Mongoid::Document
91
+ include Mongoid::History::Trackable
92
+
93
+ store_in collection: :model_threes
94
+ embeds_one :emb_three, store_as: :emtr
95
+
96
+ track_history on: :emb_three, modifier_field_optional: true
97
+ end
98
+
99
+ class EmbThree
100
+ include Mongoid::Document
101
+
102
+ field :em_foo
103
+ embedded_in :model_three
104
+ end
105
+ end
106
+
107
+ after :each do
108
+ Object.send(:remove_const, :ModelThree)
109
+ Object.send(:remove_const, :EmbThree)
110
+ end
111
+
112
+ before :each do
113
+ obj_three.save!
114
+ end
115
+
116
+ let(:obj_three) { ModelThree.new(emb_three: emb_obj_three) }
117
+ let(:emb_obj_three) { EmbThree.new(em_foo: 'Em-Foo') }
118
+ let(:base) { described_class.new(obj_three) }
119
+
120
+ it { expect(subject['emb_three']).to eq [{ '_id' => emb_obj_three._id, 'em_foo' => 'Em-Foo' }, nil] }
121
+ end
122
+
123
+ context 'relation with permitted attributes' do
124
+ before :each do
125
+ ModelTwo.track_history on: [{ emb_two: :em_foo }], modifier_field_optional: true
126
+ obj_two.save!
127
+ end
128
+
129
+ it { expect(subject['emb_two']).to eq [{ '_id' => emb_obj_two._id, 'em_foo' => 'Em-Foo' }, nil] }
130
+ end
131
+
132
+ context 'when relation object not built' do
133
+ before :each do
134
+ ModelTwo.track_history on: :emb_two, modifier_field_optional: true
135
+ obj_two.save!
136
+ end
137
+
138
+ let(:obj_two) { ModelTwo.new }
139
+ it { expect(subject['emb_two']).to be_nil }
140
+ end
141
+ end
142
+
143
+ describe '#insert_embeds_many_changes' do
144
+ context 'Case 1:' do
145
+ before :each do
146
+ class ModelTwo
147
+ include Mongoid::Document
148
+ include Mongoid::History::Trackable
149
+
150
+ embeds_many :em_twos
151
+ track_history on: :fields
152
+ end
153
+
154
+ class EmTwo
155
+ include Mongoid::Document
156
+
157
+ field :em_foo
158
+ field :em_bar
159
+
160
+ embedded_in :model_two
161
+ end
162
+ end
163
+
164
+ after :each do
165
+ Object.send(:remove_const, :ModelTwo)
166
+ Object.send(:remove_const, :EmTwo)
167
+ end
168
+
169
+ let(:obj_two) { ModelTwo.new(em_twos: [em_obj_two]) }
170
+ let(:em_obj_two) { EmTwo.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
171
+ let(:base) { described_class.new(obj_two) }
172
+
173
+ context 'when relation tracked' do
174
+ before :each do
175
+ ModelTwo.track_history on: :em_twos
176
+ end
177
+ it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], nil] }
178
+ end
179
+
180
+ context 'when relation not tracked' do
181
+ before :each do
182
+ ModelTwo.track_history on: :fields
183
+ end
184
+ it { expect(subject['em_twos']).to be_nil }
185
+ end
186
+
187
+ context 'when relation with permitted attributes for tracking' do
188
+ before :each do
189
+ ModelTwo.track_history on: { em_twos: :em_foo }
190
+ end
191
+ it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo' }], nil] }
192
+ end
193
+ end
194
+
195
+ context 'when relation with alias' do
196
+ before :each do
197
+ class ModelTwo
198
+ include Mongoid::Document
199
+ include Mongoid::History::Trackable
200
+
201
+ embeds_many :em_twos, store_as: :emws
202
+ track_history on: :fields
203
+
204
+ track_history on: :em_twos
205
+ end
206
+
207
+ class EmTwo
208
+ include Mongoid::Document
209
+
210
+ field :em_foo
211
+ embedded_in :model_two
212
+ end
213
+ end
214
+
215
+ after :each do
216
+ Object.send(:remove_const, :ModelTwo)
217
+ Object.send(:remove_const, :EmTwo)
218
+ end
219
+
220
+ let(:obj_two) { ModelTwo.new(em_twos: [em_obj_two]) }
221
+ let(:em_obj_two) { EmTwo.new(em_foo: 'Em-Foo') }
222
+ let(:base) { described_class.new(obj_two) }
223
+
224
+ it { expect(subject['em_twos']).to eq [[{ '_id' => em_obj_two._id, 'em_foo' => 'Em-Foo' }], nil] }
225
+ end
226
+ end
227
+ end
228
+ end