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.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +22 -18
- data/.travis.yml +1 -0
- data/CHANGELOG.md +4 -0
- data/CONTRIBUTING.md +4 -4
- data/Gemfile +8 -6
- data/README.md +2 -12
- data/UPGRADING.md +20 -1
- data/lib/mongoid/history.rb +8 -4
- data/lib/mongoid/history/attributes/base.rb +2 -2
- data/lib/mongoid/history/attributes/create.rb +2 -2
- data/lib/mongoid/history/attributes/update.rb +2 -2
- data/lib/mongoid/history/options.rb +11 -20
- data/lib/mongoid/history/trackable.rb +71 -56
- data/lib/mongoid/history/tracker.rb +8 -5
- data/lib/mongoid/history/version.rb +1 -1
- data/spec/integration/embedded_in_polymorphic_spec.rb +26 -49
- data/spec/integration/integration_spec.rb +132 -120
- data/spec/integration/multi_relation_spec.rb +14 -20
- data/spec/integration/multiple_trackers_spec.rb +35 -38
- data/spec/integration/nested_embedded_documents_spec.rb +31 -51
- data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +64 -76
- data/spec/integration/subclasses_spec.rb +17 -5
- data/spec/integration/track_history_order_spec.rb +59 -27
- data/spec/integration/validation_failure_spec.rb +21 -8
- data/spec/spec_helper.rb +6 -1
- data/spec/unit/attributes/base_spec.rb +17 -26
- data/spec/unit/attributes/create_spec.rb +152 -125
- data/spec/unit/attributes/destroy_spec.rb +68 -58
- data/spec/unit/attributes/update_spec.rb +71 -50
- data/spec/unit/callback_options_spec.rb +36 -30
- data/spec/unit/embedded_methods_spec.rb +42 -24
- data/spec/unit/history_spec.rb +12 -10
- data/spec/unit/my_instance_methods_spec.rb +191 -121
- data/spec/unit/options_spec.rb +49 -26
- data/spec/unit/singleton_methods_spec.rb +156 -88
- data/spec/unit/trackable_spec.rb +254 -156
- data/spec/unit/tracker_spec.rb +81 -54
- metadata +2 -2
@@ -2,23 +2,25 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Mongoid::History::Options do
|
4
4
|
describe ':if' do
|
5
|
-
|
6
|
-
|
5
|
+
before :each do
|
6
|
+
class DummyModel
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::History::Trackable
|
9
|
+
|
9
10
|
store_in collection: :model_ones
|
10
11
|
field :foo
|
12
|
+
|
11
13
|
attr_accessor :bar
|
12
|
-
|
13
|
-
|
14
|
-
track_destroy: true,
|
15
|
-
if: :bar
|
16
|
-
def self.name
|
17
|
-
'DummyModel'
|
18
|
-
end
|
14
|
+
|
15
|
+
track_history modifier_field_optional: true, if: :bar
|
19
16
|
end
|
20
17
|
end
|
21
|
-
|
18
|
+
|
19
|
+
after :each do
|
20
|
+
Object.send(:remove_const, :DummyModel)
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:obj) { DummyModel.new(foo: 'Foo') }
|
22
24
|
|
23
25
|
context 'when condition evaluates to true' do
|
24
26
|
before { obj.bar = true }
|
@@ -44,23 +46,25 @@ describe Mongoid::History::Options do
|
|
44
46
|
end
|
45
47
|
|
46
48
|
describe ':unless' do
|
47
|
-
|
48
|
-
|
49
|
+
before :each do
|
50
|
+
class DummyModel
|
49
51
|
include Mongoid::Document
|
50
52
|
include Mongoid::History::Trackable
|
53
|
+
|
51
54
|
store_in collection: :model_ones
|
52
55
|
field :foo
|
56
|
+
|
53
57
|
attr_accessor :bar
|
54
|
-
|
55
|
-
|
56
|
-
track_destroy: true,
|
57
|
-
unless: ->(obj) { obj.bar }
|
58
|
-
def self.name
|
59
|
-
'DummyModel'
|
60
|
-
end
|
58
|
+
|
59
|
+
track_history modifier_field_optional: true, unless: ->(obj) { obj.bar }
|
61
60
|
end
|
62
61
|
end
|
63
|
-
|
62
|
+
|
63
|
+
after :each do
|
64
|
+
Object.send(:remove_const, :DummyModel)
|
65
|
+
end
|
66
|
+
|
67
|
+
let(:obj) { DummyModel.new(foo: 'Foo') }
|
64
68
|
|
65
69
|
context 'when condition evaluates to true' do
|
66
70
|
before { obj.bar = true }
|
@@ -86,23 +90,25 @@ describe Mongoid::History::Options do
|
|
86
90
|
end
|
87
91
|
|
88
92
|
describe ':if and :unless' do
|
89
|
-
|
90
|
-
|
93
|
+
before :each do
|
94
|
+
class DummyModel
|
91
95
|
include Mongoid::Document
|
92
96
|
include Mongoid::History::Trackable
|
97
|
+
|
93
98
|
store_in collection: :model_ones
|
94
99
|
field :foo
|
100
|
+
|
95
101
|
attr_accessor :bar, :baz
|
96
|
-
|
97
|
-
|
98
|
-
track_destroy: true,
|
99
|
-
if: :bar, unless: ->(obj) { obj.baz }
|
100
|
-
def self.name
|
101
|
-
'DummyModel'
|
102
|
-
end
|
102
|
+
|
103
|
+
track_history modifier_field_optional: true, if: :bar, unless: ->(obj) { obj.baz }
|
103
104
|
end
|
104
105
|
end
|
105
|
-
|
106
|
+
|
107
|
+
after :each do
|
108
|
+
Object.send(:remove_const, :DummyModel)
|
109
|
+
end
|
110
|
+
|
111
|
+
let(:obj) { DummyModel.new(foo: 'Foo') }
|
106
112
|
|
107
113
|
context 'when :if condition evaluates to true' do
|
108
114
|
before { obj.bar = true }
|
@@ -2,68 +2,86 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Mongoid::History::Trackable do
|
4
4
|
describe 'EmbeddedMethods' do
|
5
|
-
describe '
|
6
|
-
before :
|
7
|
-
ModelOne
|
5
|
+
describe 'relation_class_of' do
|
6
|
+
before :each do
|
7
|
+
class ModelOne
|
8
8
|
include Mongoid::Document
|
9
9
|
include Mongoid::History::Trackable
|
10
|
-
|
11
|
-
|
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
|
+
|
12
19
|
track_history
|
13
20
|
end
|
14
21
|
|
15
|
-
EmbOne
|
22
|
+
class EmbOne
|
16
23
|
include Mongoid::Document
|
24
|
+
|
17
25
|
embedded_in :model_one
|
18
26
|
end
|
19
27
|
|
20
|
-
EmbTwo
|
28
|
+
class EmbTwo
|
21
29
|
include Mongoid::Document
|
30
|
+
|
22
31
|
embedded_in :model_one
|
23
32
|
end
|
24
33
|
end
|
25
34
|
|
26
|
-
|
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
|
35
|
+
after :each do
|
31
36
|
Object.send(:remove_const, :ModelOne)
|
32
37
|
Object.send(:remove_const, :EmbOne)
|
33
38
|
Object.send(:remove_const, :EmbTwo)
|
34
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 }
|
35
44
|
end
|
36
45
|
|
37
|
-
describe '
|
38
|
-
before :
|
39
|
-
ModelOne
|
46
|
+
describe 'relation_class_of' do
|
47
|
+
before :each do
|
48
|
+
class ModelOne
|
40
49
|
include Mongoid::Document
|
41
50
|
include Mongoid::History::Trackable
|
42
|
-
|
43
|
-
|
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
|
+
|
44
60
|
track_history
|
45
61
|
end
|
46
62
|
|
47
|
-
EmbOne
|
63
|
+
class EmbOne
|
48
64
|
include Mongoid::Document
|
65
|
+
|
49
66
|
embedded_in :model_one
|
50
67
|
end
|
51
68
|
|
52
|
-
EmbTwo
|
69
|
+
class EmbTwo
|
53
70
|
include Mongoid::Document
|
71
|
+
|
54
72
|
embedded_in :model_one
|
55
73
|
end
|
56
74
|
end
|
57
75
|
|
58
|
-
|
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
|
76
|
+
after :each do
|
63
77
|
Object.send(:remove_const, :ModelOne)
|
64
78
|
Object.send(:remove_const, :EmbOne)
|
65
79
|
Object.send(:remove_const, :EmbTwo)
|
66
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 }
|
67
85
|
end
|
68
86
|
end
|
69
87
|
end
|
data/spec/unit/history_spec.rb
CHANGED
@@ -10,26 +10,28 @@ describe Mongoid::History do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe '#trackable_class_settings' do
|
13
|
-
before
|
14
|
-
|
15
|
-
Class.new do
|
13
|
+
before :each do
|
14
|
+
class ModelOne
|
16
15
|
include Mongoid::Document
|
17
16
|
include Mongoid::History::Trackable
|
18
|
-
store_in collection: :model_ones
|
19
17
|
|
20
|
-
|
21
|
-
'ModelOne'
|
22
|
-
end
|
18
|
+
store_in collection: :model_ones
|
23
19
|
end
|
24
20
|
end
|
25
21
|
|
22
|
+
after :each do
|
23
|
+
Object.send(:remove_const, :ModelOne)
|
24
|
+
end
|
25
|
+
|
26
26
|
context 'when present' do
|
27
|
-
before
|
28
|
-
|
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') }
|
29
31
|
end
|
30
32
|
|
31
33
|
context 'when not present' do
|
32
|
-
it { expect(described_class.trackable_class_settings(
|
34
|
+
it { expect(described_class.trackable_class_settings(ModelOne)).to eq(paranoia_field: 'deleted_at') }
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -2,71 +2,91 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Mongoid::History::Trackable do
|
4
4
|
describe 'MyInstanceMethods' do
|
5
|
-
before :
|
6
|
-
ModelOne
|
5
|
+
before :each do
|
6
|
+
class ModelOne
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::History::Trackable
|
9
|
+
|
9
10
|
store_in collection: :model_ones
|
11
|
+
|
10
12
|
field :foo
|
11
13
|
field :b, as: :bar
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
16
|
+
embeds_one :emb_one
|
17
|
+
embeds_one :emb_two, store_as: :emt
|
18
|
+
embeds_many :emb_threes
|
19
|
+
embeds_many :emb_fours, store_as: :emfs
|
20
|
+
else
|
21
|
+
embeds_one :emb_one, inverse_class_name: 'EmbOne'
|
22
|
+
embeds_one :emb_two, store_as: :emt, inverse_class_name: 'EmbTwo'
|
23
|
+
embeds_many :emb_threes, inverse_class_name: 'EmbThree'
|
24
|
+
embeds_many :emb_fours, store_as: :emfs, inverse_class_name: 'EmbFour'
|
25
|
+
end
|
16
26
|
end
|
17
27
|
|
18
|
-
EmbOne
|
28
|
+
class EmbOne
|
19
29
|
include Mongoid::Document
|
20
30
|
include Mongoid::History::Trackable
|
31
|
+
|
21
32
|
field :f_em_foo
|
22
33
|
field :fmb, as: :f_em_bar
|
34
|
+
|
23
35
|
embedded_in :model_one
|
24
36
|
end
|
25
37
|
|
26
|
-
EmbTwo
|
38
|
+
class EmbTwo
|
27
39
|
include Mongoid::Document
|
28
40
|
include Mongoid::History::Trackable
|
41
|
+
|
29
42
|
field :baz
|
30
43
|
embedded_in :model_one
|
31
44
|
end
|
32
45
|
|
33
|
-
EmbThree
|
46
|
+
class EmbThree
|
34
47
|
include Mongoid::Document
|
35
48
|
include Mongoid::History::Trackable
|
49
|
+
|
36
50
|
field :f_em_foo
|
37
51
|
field :fmb, as: :f_em_bar
|
52
|
+
|
38
53
|
embedded_in :model_one
|
39
54
|
end
|
40
55
|
|
41
|
-
EmbFour
|
56
|
+
class EmbFour
|
42
57
|
include Mongoid::Document
|
43
58
|
include Mongoid::History::Trackable
|
59
|
+
|
44
60
|
field :baz
|
45
61
|
embedded_in :model_one
|
46
62
|
end
|
63
|
+
end
|
47
64
|
|
48
|
-
|
49
|
-
|
65
|
+
after :each do
|
66
|
+
Object.send(:remove_const, :ModelOne)
|
67
|
+
Object.send(:remove_const, :EmbOne)
|
68
|
+
Object.send(:remove_const, :EmbTwo)
|
69
|
+
Object.send(:remove_const, :EmbThree)
|
70
|
+
Object.send(:remove_const, :EmbFour)
|
50
71
|
end
|
51
|
-
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
52
72
|
|
53
73
|
let(:bson_class) { defined?(BSON::ObjectId) ? BSON::ObjectId : Moped::BSON::ObjectId }
|
54
74
|
|
55
75
|
let(:emb_one) { EmbOne.new(f_em_foo: 'Foo', f_em_bar: 'Bar') }
|
56
76
|
let(:emb_threes) { [EmbThree.new(f_em_foo: 'Foo', f_em_bar: 'Bar')] }
|
57
|
-
let(:model_one)
|
58
|
-
ModelOne.new(foo: 'Foo',
|
59
|
-
bar: 'Bar',
|
60
|
-
emb_one: emb_one,
|
61
|
-
emb_threes: emb_threes)
|
62
|
-
end
|
77
|
+
let(:model_one) { ModelOne.new(foo: 'Foo', bar: 'Bar', emb_one: emb_one, emb_threes: emb_threes) }
|
63
78
|
|
64
79
|
describe '#modified_attributes_for_create' do
|
65
|
-
before
|
80
|
+
before :each do
|
81
|
+
ModelOne.track_history modifier_field_optional: true, on: %i[foo emb_one emb_threes]
|
82
|
+
end
|
83
|
+
|
66
84
|
subject { model_one.send(:modified_attributes_for_create) }
|
67
85
|
|
68
86
|
context 'with tracked embeds_one object' do
|
69
|
-
before
|
87
|
+
before :each do
|
88
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_one: :f_em_foo })
|
89
|
+
end
|
70
90
|
it 'should include tracked attributes only' do
|
71
91
|
expect(subject['emb_one'][0]).to be_nil
|
72
92
|
|
@@ -77,14 +97,18 @@ describe Mongoid::History::Trackable do
|
|
77
97
|
end
|
78
98
|
|
79
99
|
context 'with untracked embeds_one object' do
|
80
|
-
before
|
100
|
+
before :each do
|
101
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
102
|
+
end
|
81
103
|
it 'should not include embeds_one attributes' do
|
82
104
|
expect(subject['emb_one']).to be_nil
|
83
105
|
end
|
84
106
|
end
|
85
107
|
|
86
108
|
context 'with tracked embeds_many objects' do
|
87
|
-
before
|
109
|
+
before :each do
|
110
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_threes: :f_em_foo })
|
111
|
+
end
|
88
112
|
it 'should include tracked attributes only' do
|
89
113
|
expect(subject['emb_threes'][0]).to be_nil
|
90
114
|
|
@@ -95,7 +119,9 @@ describe Mongoid::History::Trackable do
|
|
95
119
|
end
|
96
120
|
|
97
121
|
context 'with untracked embeds_many objects' do
|
98
|
-
before
|
122
|
+
before :each do
|
123
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
124
|
+
end
|
99
125
|
it 'should include not tracked embeds_many attributes' do
|
100
126
|
expect(subject['emb_threes']).to be_nil
|
101
127
|
end
|
@@ -110,25 +136,34 @@ describe Mongoid::History::Trackable do
|
|
110
136
|
end
|
111
137
|
|
112
138
|
describe 'embeds_one' do
|
113
|
-
before
|
114
|
-
Mail
|
139
|
+
before :each do
|
140
|
+
class Mail
|
115
141
|
include Mongoid::Document
|
116
142
|
include Mongoid::History::Trackable
|
143
|
+
|
117
144
|
store_in collection: :mails
|
118
145
|
field :provider
|
119
|
-
|
146
|
+
|
147
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
148
|
+
embeds_one :mail_subject
|
149
|
+
else
|
150
|
+
embeds_one :mail_subject, inverse_class_name: 'MailSubject'
|
151
|
+
end
|
152
|
+
|
153
|
+
track_history on: :mail_subject
|
120
154
|
end
|
121
155
|
|
122
|
-
MailSubject
|
156
|
+
class MailSubject
|
123
157
|
include Mongoid::Document
|
158
|
+
|
124
159
|
field :content
|
125
160
|
embedded_in :mail
|
126
161
|
end
|
127
162
|
end
|
128
163
|
|
129
|
-
|
130
|
-
|
131
|
-
|
164
|
+
after :each do
|
165
|
+
Object.send(:remove_const, :MailSubject)
|
166
|
+
Object.send(:remove_const, :Mail)
|
132
167
|
end
|
133
168
|
|
134
169
|
let(:mail) { Mail.new(mail_subject: mail_subject) }
|
@@ -145,46 +180,57 @@ describe Mongoid::History::Trackable do
|
|
145
180
|
end
|
146
181
|
|
147
182
|
context 'when obj not soft-deleted' do
|
148
|
-
before
|
183
|
+
before :each do
|
184
|
+
allow(mail_subject).to receive(:deleted_at) { nil }
|
185
|
+
end
|
149
186
|
let(:mail_subject) { MailSubject.new(content: 'Content') }
|
150
187
|
it { is_expected.to eq [nil, { '_id' => mail_subject._id, 'content' => 'Content' }] }
|
151
188
|
end
|
152
189
|
|
153
190
|
context 'when obj soft-deleted' do
|
154
|
-
before
|
191
|
+
before :each do
|
192
|
+
allow(mail_subject).to receive(:deleted_at) { Time.now }
|
193
|
+
end
|
155
194
|
let(:mail_subject) { MailSubject.new(content: 'Content') }
|
156
195
|
it { is_expected.to be_nil }
|
157
196
|
end
|
158
|
-
|
159
|
-
after(:all) do
|
160
|
-
Object.send(:remove_const, :MailSubject)
|
161
|
-
Object.send(:remove_const, :Mail)
|
162
|
-
end
|
163
197
|
end
|
164
198
|
|
165
199
|
describe 'paranoia' do
|
166
|
-
before
|
167
|
-
ModelParanoia
|
200
|
+
before :each do
|
201
|
+
class ModelParanoia
|
168
202
|
include Mongoid::Document
|
169
203
|
include Mongoid::History::Trackable
|
204
|
+
|
170
205
|
store_in collection: :model_paranoias
|
171
|
-
|
206
|
+
|
207
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
208
|
+
embeds_many :emb_para_ones
|
209
|
+
else
|
210
|
+
embeds_many :emb_para_ones, inverse_class_name: 'EmbParaOne'
|
211
|
+
end
|
172
212
|
end
|
173
213
|
|
174
|
-
EmbParaOne
|
214
|
+
class EmbParaOne
|
175
215
|
include Mongoid::Document
|
216
|
+
|
176
217
|
field :em_foo
|
177
218
|
field :deleted_at
|
219
|
+
|
178
220
|
embedded_in :model_paranoia
|
179
221
|
end
|
180
222
|
end
|
181
223
|
|
224
|
+
after :each do
|
225
|
+
Object.send(:remove_const, :ModelParanoia)
|
226
|
+
Object.send(:remove_const, :EmbParaOne)
|
227
|
+
end
|
228
|
+
|
182
229
|
let(:emb_para_one) { EmbParaOne.new(em_foo: 'Em-Foo') }
|
183
230
|
let(:model_paranoia) { ModelParanoia.new(emb_para_ones: [emb_para_one]) }
|
184
231
|
|
185
232
|
context 'when does not respond to paranoia_field' do
|
186
|
-
before
|
187
|
-
ModelParanoia.instance_variable_set(:@history_trackable_options, nil)
|
233
|
+
before :each do
|
188
234
|
ModelParanoia.track_history(on: :emb_para_ones)
|
189
235
|
end
|
190
236
|
|
@@ -199,8 +245,7 @@ describe Mongoid::History::Trackable do
|
|
199
245
|
end
|
200
246
|
|
201
247
|
context 'when responds to paranoia_field' do
|
202
|
-
before
|
203
|
-
ModelParanoia.instance_variable_set(:@history_trackable_options, nil)
|
248
|
+
before :each do
|
204
249
|
ModelParanoia.track_history(on: :emb_para_ones)
|
205
250
|
allow(emb_para_one).to receive(:deleted_at) { Time.now }
|
206
251
|
allow(emb_para_one_2).to receive(:deleted_at) { nil }
|
@@ -217,18 +262,12 @@ describe Mongoid::History::Trackable do
|
|
217
262
|
expect(subject['emb_para_ones'][1]).to eq [{ '_id' => emb_para_one_2._id, 'em_foo' => 'Em-Foo-2' }]
|
218
263
|
end
|
219
264
|
end
|
220
|
-
|
221
|
-
after(:all) do
|
222
|
-
Object.send(:remove_const, :ModelParanoia)
|
223
|
-
Object.send(:remove_const, :EmbParaOne)
|
224
|
-
end
|
225
265
|
end
|
226
266
|
end
|
227
267
|
|
228
268
|
describe '#modified_attributes_for_update' do
|
229
|
-
before
|
269
|
+
before :each do
|
230
270
|
model_one.save!
|
231
|
-
ModelOne.clear_trackable_memoization
|
232
271
|
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
233
272
|
allow(model_one).to receive(:changes) { changes }
|
234
273
|
end
|
@@ -236,76 +275,104 @@ describe Mongoid::History::Trackable do
|
|
236
275
|
subject { model_one.send(:modified_attributes_for_update) }
|
237
276
|
|
238
277
|
context 'when embeds_one attributes passed in options' do
|
239
|
-
before
|
278
|
+
before :each do
|
279
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_one: :f_em_foo })
|
280
|
+
end
|
240
281
|
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }, { 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }] } }
|
241
282
|
it { expect(subject['emb_one'][0]).to eq('f_em_foo' => 'Foo') }
|
242
283
|
it { expect(subject['emb_one'][1]).to eq('f_em_foo' => 'Foo-new') }
|
243
284
|
end
|
244
285
|
|
245
286
|
context 'when embeds_one relation passed in options' do
|
246
|
-
before
|
287
|
+
before :each do
|
288
|
+
ModelOne.track_history(modifier_field_optional: true, on: :emb_one)
|
289
|
+
end
|
247
290
|
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }, { 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }] } }
|
248
291
|
it { expect(subject['emb_one'][0]).to eq('f_em_foo' => 'Foo', 'fmb' => 'Bar') }
|
249
292
|
it { expect(subject['emb_one'][1]).to eq('f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new') }
|
250
293
|
end
|
251
294
|
|
252
295
|
context 'when embeds_one relation not tracked' do
|
253
|
-
before
|
296
|
+
before :each do
|
297
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
298
|
+
end
|
254
299
|
let(:changes) { { 'emb_one' => [{ 'f_em_foo' => 'Foo' }, { 'f_em_foo' => 'Foo-new' }] } }
|
255
300
|
it { expect(subject['emb_one']).to be_nil }
|
256
301
|
end
|
257
302
|
|
258
303
|
context 'when embeds_many attributes passed in options' do
|
259
|
-
before
|
304
|
+
before :each do
|
305
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_threes: :f_em_foo })
|
306
|
+
end
|
260
307
|
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] } }
|
261
308
|
it { expect(subject['emb_threes']).to eq [[{ 'f_em_foo' => 'Foo' }], [{ 'f_em_foo' => 'Foo-new' }]] }
|
262
309
|
end
|
263
310
|
|
264
311
|
context 'when embeds_many relation passed in options' do
|
265
|
-
before
|
312
|
+
before :each do
|
313
|
+
ModelOne.track_history(modifier_field_optional: true, on: :emb_threes)
|
314
|
+
end
|
266
315
|
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] } }
|
267
316
|
it { expect(subject['emb_threes']).to eq [[{ 'f_em_foo' => 'Foo', 'fmb' => 'Bar' }], [{ 'f_em_foo' => 'Foo-new', 'fmb' => 'Bar-new' }]] }
|
268
317
|
end
|
269
318
|
|
270
319
|
context 'when embeds_many relation not tracked' do
|
271
|
-
before
|
320
|
+
before :each do
|
321
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
322
|
+
end
|
272
323
|
let(:changes) { { 'emb_threes' => [[{ 'f_em_foo' => 'Foo' }], [{ 'f_em_foo' => 'Foo-new' }]] } }
|
273
324
|
it { expect(subject['emb_threes']).to be_nil }
|
274
325
|
end
|
275
326
|
|
276
327
|
context 'when field tracked' do
|
277
|
-
before
|
328
|
+
before :each do
|
329
|
+
ModelOne.track_history(modifier_field_optional: true, on: :foo)
|
330
|
+
end
|
278
331
|
let(:changes) { { 'foo' => ['Foo', 'Foo-new'], 'b' => ['Bar', 'Bar-new'] } }
|
279
332
|
it { is_expected.to eq('foo' => ['Foo', 'Foo-new']) }
|
280
333
|
end
|
281
334
|
|
282
335
|
context 'when field not tracked' do
|
283
|
-
before
|
336
|
+
before :each do
|
337
|
+
ModelOne.track_history(modifier_field_optional: true, on: [])
|
338
|
+
end
|
284
339
|
let(:changes) { { 'foo' => ['Foo', 'Foo-new'] } }
|
285
340
|
it { is_expected.to eq({}) }
|
286
341
|
end
|
287
342
|
|
288
343
|
describe 'embeds_one' do
|
289
|
-
before
|
290
|
-
Email
|
344
|
+
before :each do
|
345
|
+
class Email
|
291
346
|
include Mongoid::Document
|
292
347
|
include Mongoid::History::Trackable
|
348
|
+
|
293
349
|
store_in collection: :emails
|
294
350
|
field :provider
|
295
|
-
|
351
|
+
|
352
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
353
|
+
embeds_one :email_subject
|
354
|
+
else
|
355
|
+
embeds_one :email_subject, inverse_class_name: 'EmailSubject'
|
356
|
+
end
|
357
|
+
|
358
|
+
track_history on: :email_subject
|
296
359
|
end
|
297
360
|
|
298
|
-
EmailSubject
|
361
|
+
class EmailSubject
|
299
362
|
include Mongoid::Document
|
300
363
|
include Mongoid::History::Trackable
|
364
|
+
|
301
365
|
field :content
|
302
366
|
embedded_in :email_subject
|
303
367
|
end
|
304
368
|
end
|
305
369
|
|
306
|
-
|
307
|
-
|
308
|
-
|
370
|
+
after :each do
|
371
|
+
Object.send(:remove_const, :EmailSubject)
|
372
|
+
Object.send(:remove_const, :Email)
|
373
|
+
end
|
374
|
+
|
375
|
+
before :each do
|
309
376
|
allow(Email).to receive(:dynamic_enabled?) { false }
|
310
377
|
allow(email).to receive(:changes) { changes }
|
311
378
|
end
|
@@ -335,83 +402,92 @@ describe Mongoid::History::Trackable do
|
|
335
402
|
end
|
336
403
|
it { is_expected.to eq [{ 'content' => 'Content' }, { 'content' => 'Content-new' }] }
|
337
404
|
end
|
338
|
-
|
339
|
-
after(:all) do
|
340
|
-
Object.send(:remove_const, :EmailSubject)
|
341
|
-
Object.send(:remove_const, :Email)
|
342
|
-
end
|
343
405
|
end
|
344
406
|
|
345
407
|
describe 'paranoia_field' do
|
346
408
|
context 'when embeds_one has alias' do
|
347
|
-
before
|
348
|
-
|
349
|
-
ModelTwo = Class.new do
|
409
|
+
before :each do
|
410
|
+
class ModelTwo
|
350
411
|
include Mongoid::Document
|
351
412
|
include Mongoid::History::Trackable
|
413
|
+
|
352
414
|
store_in collection: :model_twos
|
353
|
-
|
415
|
+
|
416
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
417
|
+
embeds_one :emb_two_one
|
418
|
+
else
|
419
|
+
embeds_one :emb_two_one, inverse_class_name: 'EmbTwoOne'
|
420
|
+
end
|
421
|
+
|
422
|
+
track_history on: :emb_two_one
|
354
423
|
end
|
355
424
|
|
356
|
-
EmbTwoOne
|
425
|
+
class EmbTwoOne
|
357
426
|
include Mongoid::Document
|
358
427
|
include Mongoid::History::Trackable
|
428
|
+
|
359
429
|
field :foo
|
360
430
|
field :cncl, as: :cancelled_at
|
431
|
+
|
361
432
|
embedded_in :model_two
|
433
|
+
|
434
|
+
history_settings paranoia_field: :cancelled_at
|
362
435
|
end
|
363
436
|
end
|
364
437
|
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
ModelTwo.track_history on: :emb_two_one
|
371
|
-
EmbTwoOne.history_settings paranoia_field: :cancelled_at
|
438
|
+
after :each do
|
439
|
+
Object.send(:remove_const, :ModelTwo)
|
440
|
+
Object.send(:remove_const, :EmbTwoOne)
|
441
|
+
end
|
372
442
|
|
443
|
+
before :each do
|
373
444
|
allow(ModelTwo).to receive(:dynamic_enabled?) { false }
|
374
445
|
allow(model_two_obj).to receive(:changes) { changes }
|
375
446
|
end
|
376
447
|
|
377
448
|
let(:model_two_obj) { ModelTwo.new }
|
378
449
|
let(:changes) { { 'emb_two_one' => [{ 'foo' => 'Foo', 'cncl' => Time.now }, { 'foo' => 'Foo-new' }] } }
|
450
|
+
|
379
451
|
subject { model_two_obj.send(:modified_attributes_for_update)['emb_two_one'] }
|
380
452
|
it { is_expected.to eq [{}, { 'foo' => 'Foo-new' }] }
|
381
|
-
|
382
|
-
after(:all) do
|
383
|
-
Object.send(:remove_const, :ModelTwo)
|
384
|
-
Object.send(:remove_const, :EmbTwoOne)
|
385
|
-
end
|
386
453
|
end
|
387
454
|
|
388
455
|
context 'when embeds_many has alias' do
|
389
|
-
before
|
390
|
-
|
391
|
-
ModelTwo = Class.new do
|
456
|
+
before :each do
|
457
|
+
class ModelTwo
|
392
458
|
include Mongoid::Document
|
393
459
|
include Mongoid::History::Trackable
|
460
|
+
|
394
461
|
store_in collection: :model_twos
|
395
|
-
|
462
|
+
|
463
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
464
|
+
embeds_many :emb_two_ones
|
465
|
+
else
|
466
|
+
embeds_many :emb_two_ones, inverse_class_name: 'EmbTwoOne'
|
467
|
+
end
|
468
|
+
|
469
|
+
track_history on: :emb_two_ones
|
396
470
|
end
|
397
471
|
|
398
|
-
EmbTwoOne
|
472
|
+
class EmbTwoOne
|
399
473
|
include Mongoid::Document
|
400
474
|
include Mongoid::History::Trackable
|
475
|
+
|
401
476
|
field :foo
|
402
477
|
field :cncl, as: :cancelled_at
|
478
|
+
|
403
479
|
embedded_in :model_two
|
480
|
+
|
481
|
+
history_settings paranoia_field: :cancelled_at
|
404
482
|
end
|
405
483
|
end
|
406
484
|
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
ModelTwo.track_history on: :emb_two_ones
|
413
|
-
EmbTwoOne.history_settings paranoia_field: :cancelled_at
|
485
|
+
after :each do
|
486
|
+
Object.send(:remove_const, :ModelTwo)
|
487
|
+
Object.send(:remove_const, :EmbTwoOne)
|
488
|
+
end
|
414
489
|
|
490
|
+
before :each do
|
415
491
|
allow(ModelTwo).to receive(:dynamic_enabled?) { false }
|
416
492
|
allow(model_two_obj).to receive(:changes) { changes }
|
417
493
|
end
|
@@ -420,25 +496,21 @@ describe Mongoid::History::Trackable do
|
|
420
496
|
let(:changes) { { 'emb_two_ones' => [[{ 'foo' => 'Foo', 'cncl' => Time.now }], [{ 'foo' => 'Foo-new' }]] } }
|
421
497
|
subject { model_two_obj.send(:modified_attributes_for_update)['emb_two_ones'] }
|
422
498
|
it { is_expected.to eq [[], [{ 'foo' => 'Foo-new' }]] }
|
423
|
-
|
424
|
-
after(:all) do
|
425
|
-
Object.send(:remove_const, :ModelTwo)
|
426
|
-
Object.send(:remove_const, :EmbTwoOne)
|
427
|
-
end
|
428
499
|
end
|
429
500
|
end
|
430
501
|
end
|
431
502
|
|
432
503
|
describe '#modified_attributes_for_destroy' do
|
433
|
-
before
|
504
|
+
before :each do
|
434
505
|
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
435
506
|
model_one.save!
|
436
|
-
ModelOne.clear_trackable_memoization
|
437
507
|
end
|
438
508
|
subject { model_one.send(:modified_attributes_for_destroy) }
|
439
509
|
|
440
510
|
context 'with tracked embeds_one object' do
|
441
|
-
before
|
511
|
+
before :each do
|
512
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_one: :f_em_foo })
|
513
|
+
end
|
442
514
|
it 'should include tracked attributes only' do
|
443
515
|
expect(subject['emb_one'][0].keys.size).to eq 2
|
444
516
|
expect(subject['emb_one'][0]['_id']).to eq emb_one._id
|
@@ -449,14 +521,18 @@ describe Mongoid::History::Trackable do
|
|
449
521
|
end
|
450
522
|
|
451
523
|
context 'with untracked embeds_one object' do
|
452
|
-
before
|
524
|
+
before :each do
|
525
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
526
|
+
end
|
453
527
|
it 'should not include embeds_one attributes' do
|
454
528
|
expect(subject['emb_one']).to be_nil
|
455
529
|
end
|
456
530
|
end
|
457
531
|
|
458
532
|
context 'with tracked embeds_many objects' do
|
459
|
-
before
|
533
|
+
before :each do
|
534
|
+
ModelOne.track_history(modifier_field_optional: true, on: { emb_threes: :f_em_foo })
|
535
|
+
end
|
460
536
|
it 'should include tracked attributes only' do
|
461
537
|
expect(subject['emb_threes'][0][0].keys.count).to eq 2
|
462
538
|
expect(subject['emb_threes'][0][0]['_id']).to eq emb_threes.first._id
|
@@ -467,19 +543,13 @@ describe Mongoid::History::Trackable do
|
|
467
543
|
end
|
468
544
|
|
469
545
|
context 'with untracked embeds_many objects' do
|
470
|
-
before
|
546
|
+
before :each do
|
547
|
+
ModelOne.track_history(modifier_field_optional: true, on: :fields)
|
548
|
+
end
|
471
549
|
it 'should include not tracked embeds_many attributes' do
|
472
550
|
expect(subject['emb_threes']).to be_nil
|
473
551
|
end
|
474
552
|
end
|
475
553
|
end
|
476
|
-
|
477
|
-
after :all do
|
478
|
-
Object.send(:remove_const, :ModelOne)
|
479
|
-
Object.send(:remove_const, :EmbOne)
|
480
|
-
Object.send(:remove_const, :EmbTwo)
|
481
|
-
Object.send(:remove_const, :EmbThree)
|
482
|
-
Object.send(:remove_const, :EmbFour)
|
483
|
-
end
|
484
554
|
end
|
485
555
|
end
|