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
data/spec/unit/tracker_spec.rb
CHANGED
@@ -1,54 +1,65 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::History::Tracker do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
context 'when included' do
|
5
|
+
before :each do
|
6
|
+
Mongoid::History.tracker_class_name = nil
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
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
|
27
|
-
TrackerOne
|
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
|
34
|
+
class ModelOne
|
36
35
|
include Mongoid::Document
|
37
36
|
include Mongoid::History::Trackable
|
37
|
+
|
38
38
|
store_in collection: :model_ones
|
39
|
-
|
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
|
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
|
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
|
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
|
-
{
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
{
|
88
|
-
|
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
|
-
{
|
102
|
-
|
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
|
-
{
|
116
|
-
|
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
|
-
{
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
-
{
|
142
|
-
|
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.
|
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-
|
13
|
+
date: 2018-06-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: easy_diff
|