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,159 +1,165 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::History::Options do
4
- describe ':if' do
5
- let(:dummy_model) do
6
- Class.new do
7
- include Mongoid::Document
8
- include Mongoid::History::Trackable
9
- store_in collection: :model_ones
10
- field :foo
11
- attr_accessor :bar
12
- track_history track_create: true,
13
- track_update: true,
14
- track_destroy: true,
15
- if: :bar
16
- def self.name
17
- 'DummyModel'
18
- end
19
- end
20
- end
21
- let(:obj) { dummy_model.new(foo: 'Foo') }
22
-
23
- context 'when condition evaluates to true' do
24
- before { obj.bar = true }
25
- it 'should create history tracks' do
26
- expect { obj.save }.to change(Tracker, :count).by(1)
27
- expect do
28
- obj.update_attributes(foo: 'Foo-2')
29
- end.to change(Tracker, :count).by(1)
30
- expect { obj.destroy }.to change(Tracker, :count).by(1)
31
- end
32
- end
33
-
34
- context 'when condition evaluates to false' do
35
- before { obj.bar = false }
36
- it 'should not create history tracks' do
37
- expect { obj.save }.to_not change(Tracker, :count)
38
- expect do
39
- obj.update_attributes(foo: 'Foo-2')
40
- end.to_not change(Tracker, :count)
41
- expect { obj.destroy }.to_not change(Tracker, :count)
42
- end
43
- end
44
- end
45
-
46
- describe ':unless' do
47
- let(:dummy_model) do
48
- Class.new do
49
- include Mongoid::Document
50
- include Mongoid::History::Trackable
51
- store_in collection: :model_ones
52
- field :foo
53
- attr_accessor :bar
54
- track_history track_create: true,
55
- track_update: true,
56
- track_destroy: true,
57
- unless: ->(obj) { obj.bar }
58
- def self.name
59
- 'DummyModel'
60
- end
61
- end
62
- end
63
- let(:obj) { dummy_model.new(foo: 'Foo') }
64
-
65
- context 'when condition evaluates to true' do
66
- before { obj.bar = true }
67
- it 'should not create history tracks' do
68
- expect { obj.save }.to_not change(Tracker, :count)
69
- expect do
70
- obj.update_attributes(foo: 'Foo-2')
71
- end.to_not change(Tracker, :count)
72
- expect { obj.destroy }.to_not change(Tracker, :count)
73
- end
74
- end
75
-
76
- context 'when condition evaluates to false' do
77
- before { obj.bar = false }
78
- it 'should create history tracks' do
79
- expect { obj.save }.to change(Tracker, :count).by(1)
80
- expect do
81
- obj.update_attributes(foo: 'Foo-2')
82
- end.to change(Tracker, :count).by(1)
83
- expect { obj.destroy }.to change(Tracker, :count).by(1)
84
- end
85
- end
86
- end
87
-
88
- describe ':if and :unless' do
89
- let(:dummy_model) do
90
- Class.new do
91
- include Mongoid::Document
92
- include Mongoid::History::Trackable
93
- store_in collection: :model_ones
94
- field :foo
95
- attr_accessor :bar, :baz
96
- track_history track_create: true,
97
- track_update: true,
98
- track_destroy: true,
99
- if: :bar, unless: ->(obj) { obj.baz }
100
- def self.name
101
- 'DummyModel'
102
- end
103
- end
104
- end
105
- let(:obj) { dummy_model.new(foo: 'Foo') }
106
-
107
- context 'when :if condition evaluates to true' do
108
- before { obj.bar = true }
109
-
110
- context 'and :unless condition evaluates to true' do
111
- before { obj.baz = true }
112
- it 'should not create history tracks' do
113
- expect { obj.save }.to_not change(Tracker, :count)
114
- expect do
115
- obj.update_attributes(foo: 'Foo-2')
116
- end.to_not change(Tracker, :count)
117
- expect { obj.destroy }.to_not change(Tracker, :count)
118
- end
119
- end
120
-
121
- context 'and :unless condition evaluates to false' do
122
- before { obj.baz = false }
123
- it 'should create history tracks' do
124
- expect { obj.save }.to change(Tracker, :count).by(1)
125
- expect do
126
- obj.update_attributes(foo: 'Foo-2')
127
- end.to change(Tracker, :count).by(1)
128
- expect { obj.destroy }.to change(Tracker, :count).by(1)
129
- end
130
- end
131
- end
132
-
133
- context 'when :if condition evaluates to false' do
134
- before { obj.bar = false }
135
-
136
- context 'and :unless condition evaluates to true' do
137
- before { obj.baz = true }
138
- it 'should not create history tracks' do
139
- expect { obj.save }.to_not change(Tracker, :count)
140
- expect do
141
- obj.update_attributes(foo: 'Foo-2')
142
- end.to_not change(Tracker, :count)
143
- expect { obj.destroy }.to_not change(Tracker, :count)
144
- end
145
- end
146
-
147
- context 'and :unless condition evaluates to false' do
148
- before { obj.baz = false }
149
- it 'should not create history tracks' do
150
- expect { obj.save }.to_not change(Tracker, :count)
151
- expect do
152
- obj.update_attributes(foo: 'Foo-2')
153
- end.to_not change(Tracker, :count)
154
- expect { obj.destroy }.to_not change(Tracker, :count)
155
- end
156
- end
157
- end
158
- end
159
- end
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::History::Options do
4
+ describe ':if' do
5
+ before :each do
6
+ class DummyModel
7
+ include Mongoid::Document
8
+ include Mongoid::History::Trackable
9
+
10
+ store_in collection: :model_ones
11
+ field :foo
12
+
13
+ attr_accessor :bar
14
+
15
+ track_history modifier_field_optional: true, if: :bar
16
+ end
17
+ end
18
+
19
+ after :each do
20
+ Object.send(:remove_const, :DummyModel)
21
+ end
22
+
23
+ let(:obj) { DummyModel.new(foo: 'Foo') }
24
+
25
+ context 'when condition evaluates to true' do
26
+ before { obj.bar = true }
27
+ it 'should create history tracks' do
28
+ expect { obj.save }.to change(Tracker, :count).by(1)
29
+ expect do
30
+ obj.update_attributes(foo: 'Foo-2')
31
+ end.to change(Tracker, :count).by(1)
32
+ expect { obj.destroy }.to change(Tracker, :count).by(1)
33
+ end
34
+ end
35
+
36
+ context 'when condition evaluates to false' do
37
+ before { obj.bar = false }
38
+ it 'should not create history tracks' do
39
+ expect { obj.save }.to_not change(Tracker, :count)
40
+ expect do
41
+ obj.update_attributes(foo: 'Foo-2')
42
+ end.to_not change(Tracker, :count)
43
+ expect { obj.destroy }.to_not change(Tracker, :count)
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ':unless' do
49
+ before :each do
50
+ class DummyModel
51
+ include Mongoid::Document
52
+ include Mongoid::History::Trackable
53
+
54
+ store_in collection: :model_ones
55
+ field :foo
56
+
57
+ attr_accessor :bar
58
+
59
+ track_history modifier_field_optional: true, unless: ->(obj) { obj.bar }
60
+ end
61
+ end
62
+
63
+ after :each do
64
+ Object.send(:remove_const, :DummyModel)
65
+ end
66
+
67
+ let(:obj) { DummyModel.new(foo: 'Foo') }
68
+
69
+ context 'when condition evaluates to true' do
70
+ before { obj.bar = true }
71
+ it 'should not create history tracks' do
72
+ expect { obj.save }.to_not change(Tracker, :count)
73
+ expect do
74
+ obj.update_attributes(foo: 'Foo-2')
75
+ end.to_not change(Tracker, :count)
76
+ expect { obj.destroy }.to_not change(Tracker, :count)
77
+ end
78
+ end
79
+
80
+ context 'when condition evaluates to false' do
81
+ before { obj.bar = false }
82
+ it 'should create history tracks' do
83
+ expect { obj.save }.to change(Tracker, :count).by(1)
84
+ expect do
85
+ obj.update_attributes(foo: 'Foo-2')
86
+ end.to change(Tracker, :count).by(1)
87
+ expect { obj.destroy }.to change(Tracker, :count).by(1)
88
+ end
89
+ end
90
+ end
91
+
92
+ describe ':if and :unless' do
93
+ before :each do
94
+ class DummyModel
95
+ include Mongoid::Document
96
+ include Mongoid::History::Trackable
97
+
98
+ store_in collection: :model_ones
99
+ field :foo
100
+
101
+ attr_accessor :bar, :baz
102
+
103
+ track_history modifier_field_optional: true, if: :bar, unless: ->(obj) { obj.baz }
104
+ end
105
+ end
106
+
107
+ after :each do
108
+ Object.send(:remove_const, :DummyModel)
109
+ end
110
+
111
+ let(:obj) { DummyModel.new(foo: 'Foo') }
112
+
113
+ context 'when :if condition evaluates to true' do
114
+ before { obj.bar = true }
115
+
116
+ context 'and :unless condition evaluates to true' do
117
+ before { obj.baz = true }
118
+ it 'should not create history tracks' do
119
+ expect { obj.save }.to_not change(Tracker, :count)
120
+ expect do
121
+ obj.update_attributes(foo: 'Foo-2')
122
+ end.to_not change(Tracker, :count)
123
+ expect { obj.destroy }.to_not change(Tracker, :count)
124
+ end
125
+ end
126
+
127
+ context 'and :unless condition evaluates to false' do
128
+ before { obj.baz = false }
129
+ it 'should create history tracks' do
130
+ expect { obj.save }.to change(Tracker, :count).by(1)
131
+ expect do
132
+ obj.update_attributes(foo: 'Foo-2')
133
+ end.to change(Tracker, :count).by(1)
134
+ expect { obj.destroy }.to change(Tracker, :count).by(1)
135
+ end
136
+ end
137
+ end
138
+
139
+ context 'when :if condition evaluates to false' do
140
+ before { obj.bar = false }
141
+
142
+ context 'and :unless condition evaluates to true' do
143
+ before { obj.baz = true }
144
+ it 'should not create history tracks' do
145
+ expect { obj.save }.to_not change(Tracker, :count)
146
+ expect do
147
+ obj.update_attributes(foo: 'Foo-2')
148
+ end.to_not change(Tracker, :count)
149
+ expect { obj.destroy }.to_not change(Tracker, :count)
150
+ end
151
+ end
152
+
153
+ context 'and :unless condition evaluates to false' do
154
+ before { obj.baz = false }
155
+ it 'should not create history tracks' do
156
+ expect { obj.save }.to_not change(Tracker, :count)
157
+ expect do
158
+ obj.update_attributes(foo: 'Foo-2')
159
+ end.to_not change(Tracker, :count)
160
+ expect { obj.destroy }.to_not change(Tracker, :count)
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -1,69 +1,87 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::History::Trackable do
4
- describe 'EmbeddedMethods' do
5
- describe 'embeds_one_class' do
6
- before :all do
7
- ModelOne = Class.new do
8
- include Mongoid::Document
9
- include Mongoid::History::Trackable
10
- embeds_one :emb_one, inverse_class_name: 'EmbOne'
11
- embeds_one :emb_two, store_as: 'emt', inverse_class_name: 'EmbTwo'
12
- track_history
13
- end
14
-
15
- EmbOne = Class.new do
16
- include Mongoid::Document
17
- embedded_in :model_one
18
- end
19
-
20
- EmbTwo = Class.new do
21
- include Mongoid::Document
22
- embedded_in :model_one
23
- end
24
- end
25
-
26
- it { expect(ModelOne.embeds_one_class('emb_one')).to eq EmbOne }
27
- it { expect(ModelOne.embeds_one_class('emt')).to eq EmbTwo }
28
- it { expect(ModelOne.embeds_one_class('invalid')).to be_nil }
29
-
30
- after :all do
31
- Object.send(:remove_const, :ModelOne)
32
- Object.send(:remove_const, :EmbOne)
33
- Object.send(:remove_const, :EmbTwo)
34
- end
35
- end
36
-
37
- describe 'embeds_many_class' do
38
- before :all do
39
- ModelOne = Class.new do
40
- include Mongoid::Document
41
- include Mongoid::History::Trackable
42
- embeds_many :emb_ones, inverse_class_name: 'EmbOne'
43
- embeds_many :emb_twos, store_as: 'emts', inverse_class_name: 'EmbTwo'
44
- track_history
45
- end
46
-
47
- EmbOne = Class.new do
48
- include Mongoid::Document
49
- embedded_in :model_one
50
- end
51
-
52
- EmbTwo = Class.new do
53
- include Mongoid::Document
54
- embedded_in :model_one
55
- end
56
- end
57
-
58
- it { expect(ModelOne.embeds_many_class('emb_ones')).to eq EmbOne }
59
- it { expect(ModelOne.embeds_many_class('emts')).to eq EmbTwo }
60
- it { expect(ModelOne.embeds_many_class('invalid')).to be_nil }
61
-
62
- after :all do
63
- Object.send(:remove_const, :ModelOne)
64
- Object.send(:remove_const, :EmbOne)
65
- Object.send(:remove_const, :EmbTwo)
66
- end
67
- end
68
- end
69
- end
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::History::Trackable do
4
+ describe 'EmbeddedMethods' do
5
+ describe 'relation_class_of' do
6
+ before :each do
7
+ class ModelOne
8
+ include Mongoid::Document
9
+ include Mongoid::History::Trackable
10
+
11
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
12
+ embeds_one :emb_one
13
+ embeds_one :emb_two, store_as: 'emt'
14
+ else
15
+ embeds_one :emb_one, inverse_class_name: 'EmbOne'
16
+ embeds_one :emb_two, store_as: 'emt', inverse_class_name: 'EmbTwo'
17
+ end
18
+
19
+ track_history
20
+ end
21
+
22
+ class EmbOne
23
+ include Mongoid::Document
24
+
25
+ embedded_in :model_one
26
+ end
27
+
28
+ class EmbTwo
29
+ include Mongoid::Document
30
+
31
+ embedded_in :model_one
32
+ end
33
+ end
34
+
35
+ after :each do
36
+ Object.send(:remove_const, :ModelOne)
37
+ Object.send(:remove_const, :EmbOne)
38
+ Object.send(:remove_const, :EmbTwo)
39
+ end
40
+
41
+ it { expect(ModelOne.relation_class_of('emb_one')).to eq EmbOne }
42
+ it { expect(ModelOne.relation_class_of('emt')).to eq EmbTwo }
43
+ it { expect(ModelOne.relation_class_of('invalid')).to be_nil }
44
+ end
45
+
46
+ describe 'relation_class_of' do
47
+ before :each do
48
+ class ModelOne
49
+ include Mongoid::Document
50
+ include Mongoid::History::Trackable
51
+
52
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
53
+ embeds_many :emb_ones
54
+ embeds_many :emb_twos, store_as: 'emts'
55
+ else
56
+ embeds_many :emb_ones, inverse_class_name: 'EmbOne'
57
+ embeds_many :emb_twos, store_as: 'emts', inverse_class_name: 'EmbTwo'
58
+ end
59
+
60
+ track_history
61
+ end
62
+
63
+ class EmbOne
64
+ include Mongoid::Document
65
+
66
+ embedded_in :model_one
67
+ end
68
+
69
+ class EmbTwo
70
+ include Mongoid::Document
71
+
72
+ embedded_in :model_one
73
+ end
74
+ end
75
+
76
+ after :each do
77
+ Object.send(:remove_const, :ModelOne)
78
+ Object.send(:remove_const, :EmbOne)
79
+ Object.send(:remove_const, :EmbTwo)
80
+ end
81
+
82
+ it { expect(ModelOne.relation_class_of('emb_ones')).to eq EmbOne }
83
+ it { expect(ModelOne.relation_class_of('emts')).to eq EmbTwo }
84
+ it { expect(ModelOne.relation_class_of('invalid')).to be_nil }
85
+ end
86
+ end
87
+ end
@@ -1,35 +1,58 @@
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
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 do
14
+ class ModelOne
15
+ include Mongoid::Document
16
+ include Mongoid::History::Trackable
17
+
18
+ store_in collection: :model_ones
19
+ end
20
+ end
21
+
22
+ after :each do
23
+ Object.send(:remove_const, :ModelOne)
24
+ end
25
+
26
+ context 'when present' do
27
+ before :each do
28
+ ModelOne.history_settings paranoia_field: :annuled_at
29
+ end
30
+ it { expect(described_class.trackable_class_settings(ModelOne)).to eq(paranoia_field: 'annuled_at') }
31
+ end
32
+
33
+ context 'when not present' do
34
+ it { expect(described_class.trackable_class_settings(ModelOne)).to eq(paranoia_field: 'deleted_at') }
35
+ end
36
+ end
37
+
38
+ describe '#reset!' do
39
+ before :each do
40
+ class ModelTwo
41
+ include Mongoid::Document
42
+ include Mongoid::History::Trackable
43
+
44
+ track_history
45
+ end
46
+ end
47
+
48
+ after :each do
49
+ Object.send(:remove_const, :ModelTwo)
50
+ end
51
+
52
+ it 'should remove all configurations' do
53
+ expect(ModelTwo).to have_attributes mongoid_history_options: be_a(Mongoid::History::Options)
54
+ Mongoid::History.reset!
55
+ expect(ModelTwo).to_not respond_to :mongoid_history_options
56
+ end
57
+ end
58
+ end