mongoid-history 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +16 -3
  3. data/.travis.yml +23 -8
  4. data/CHANGELOG.md +20 -6
  5. data/Dangerfile +1 -0
  6. data/Gemfile +22 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +130 -18
  9. data/lib/mongoid/history.rb +32 -15
  10. data/lib/mongoid/history/attributes/base.rb +31 -0
  11. data/lib/mongoid/history/attributes/create.rb +52 -0
  12. data/lib/mongoid/history/attributes/destroy.rb +36 -0
  13. data/lib/mongoid/history/attributes/update.rb +43 -0
  14. data/lib/mongoid/history/options.rb +153 -0
  15. data/lib/mongoid/history/trackable.rb +170 -72
  16. data/lib/mongoid/history/tracker.rb +39 -23
  17. data/lib/mongoid/history/version.rb +1 -1
  18. data/mongoid-history.gemspec +0 -8
  19. data/spec/integration/embedded_in_polymorphic_spec.rb +8 -0
  20. data/spec/integration/integration_spec.rb +4 -0
  21. data/spec/integration/nested_embedded_documents_spec.rb +13 -6
  22. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +24 -24
  23. data/spec/spec_helper.rb +6 -0
  24. data/spec/unit/attributes/base_spec.rb +54 -0
  25. data/spec/unit/attributes/create_spec.rb +315 -0
  26. data/spec/unit/attributes/destroy_spec.rb +218 -0
  27. data/spec/unit/attributes/update_spec.rb +210 -0
  28. data/spec/unit/embedded_methods_spec.rb +69 -0
  29. data/spec/unit/history_spec.rb +35 -0
  30. data/spec/unit/my_instance_methods_spec.rb +485 -0
  31. data/spec/unit/options_spec.rb +294 -0
  32. data/spec/unit/singleton_methods_spec.rb +338 -0
  33. data/spec/unit/store/default_store_spec.rb +11 -0
  34. data/spec/unit/store/request_store_spec.rb +13 -0
  35. data/spec/unit/trackable_spec.rb +335 -68
  36. data/spec/unit/tracker_spec.rb +153 -0
  37. metadata +31 -102
@@ -1,10 +1,163 @@
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
8
+
4
9
  it 'should set tracker_class_name when included' do
5
10
  class MyTracker
6
11
  include Mongoid::History::Tracker
7
12
  end
8
13
  expect(Mongoid::History.tracker_class_name).to eq(:my_tracker)
9
14
  end
15
+
16
+ it 'should set fields defaults' do
17
+ class MyTrackerTwo
18
+ include Mongoid::History::Tracker
19
+ end
20
+ expect(MyTrackerTwo.new.association_chain).to eq([])
21
+ expect(MyTrackerTwo.new.original).to eq({})
22
+ expect(MyTrackerTwo.new.modified).to eq({})
23
+ end
24
+
25
+ describe '#tracked_edits' do
26
+ before(:all) do
27
+ TrackerOne = Class.new do
28
+ def self.name
29
+ 'TrackerOne'
30
+ end
31
+
32
+ include Mongoid::History::Tracker
33
+ end
34
+
35
+ ModelOne = Class.new do
36
+ include Mongoid::Document
37
+ include Mongoid::History::Trackable
38
+ store_in collection: :model_ones
39
+ embeds_many :emb_ones, inverse_class_name: 'EmbOne'
40
+ end
41
+
42
+ EmbOne = Class.new do
43
+ include Mongoid::Document
44
+ field :em_foo
45
+ embedded_in :model_one
46
+ end
47
+ end
48
+
49
+ context 'when embeds_many' do
50
+ before(:each) do
51
+ ModelOne.instance_variable_set(:@history_trackable_options, nil)
52
+ ModelOne.track_history(on: :emb_ones)
53
+ allow(tracker).to receive(:trackable_parent_class) { ModelOne }
54
+ end
55
+
56
+ let(:tracker) { TrackerOne.new }
57
+
58
+ describe '#prepare_tracked_edits_for_embeds_many' do
59
+ before(:each) do
60
+ tracker.instance_variable_set(:@tracked_edits, nil)
61
+ allow(tracker).to receive(:tracked_changes) { changes }
62
+ end
63
+ let(:emb_one) { EmbOne.new }
64
+ let(:emb_one_2) { EmbOne.new }
65
+ let(:emb_one_3) { EmbOne.new }
66
+ let(:changes) { {} }
67
+ subject { tracker.tracked_edits['embeds_many']['emb_ones'] }
68
+
69
+ context 'when all values present' do
70
+ 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' }] } }
75
+ end
76
+ it 'should include :add, :remove, and :modify' do
77
+ expect(subject['add']).to eq [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
78
+ expect(subject['remove']).to eq [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' }]
79
+ expect(subject['modify'].size).to eq 1
80
+ expect(subject['modify'][0]['from']).to eq('_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2')
81
+ expect(subject['modify'][0]['to']).to eq('_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new')
82
+ end
83
+ end
84
+
85
+ context 'when value :from blank' do
86
+ 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' }] } }
89
+ end
90
+ it 'should include :add' do
91
+ expect(subject['add'].size).to eq 2
92
+ expect(subject['add'][0]).to eq('_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2-new')
93
+ expect(subject['add'][1]).to eq('_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3')
94
+ expect(subject['remove']).to be_nil
95
+ expect(subject['modify']).to be_nil
96
+ end
97
+ end
98
+
99
+ context 'when value :to blank' do
100
+ 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' }] } }
103
+ end
104
+ it 'should include :remove' do
105
+ expect(subject['add']).to be_nil
106
+ expect(subject['modify']).to be_nil
107
+ expect(subject['remove'].size).to eq 2
108
+ expect(subject['remove'][0]).to eq('_id' => emb_one._id, 'em_foo' => 'Em-Foo')
109
+ expect(subject['remove'][1]).to eq('_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2')
110
+ end
111
+ end
112
+
113
+ context 'when no id common in :from and :to' do
114
+ 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' }] } }
117
+ end
118
+ it 'should include :add, and :remove' do
119
+ expect(subject['add']).to eq [{ '_id' => emb_one_3._id, 'em_foo' => 'Em-Foo-3' }]
120
+ expect(subject['modify']).to be_nil
121
+ expect(subject['remove']).to eq [{ '_id' => emb_one._id, 'em_foo' => 'Em-Foo' }]
122
+ end
123
+ end
124
+
125
+ context 'when _id attribute not set' do
126
+ 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' }] } }
131
+ end
132
+ it 'should include :add, and :remove' do
133
+ expect(subject['add']).to eq([{ 'em_foo' => 'Em-Foo-2-new' }, { 'em_foo' => 'Em-Foo-3' }])
134
+ expect(subject['modify']).to be_nil
135
+ expect(subject['remove']).to eq [{ 'em_foo' => 'Em-Foo' }, { '_id' => emb_one_2._id, 'em_foo' => 'Em-Foo-2' }]
136
+ end
137
+ end
138
+
139
+ context 'when no change in an object' do
140
+ 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' }] } }
143
+ end
144
+ it 'should include not :add, :remove, and :modify' do
145
+ expect(subject['add']).to be_nil
146
+ expect(subject['modify']).to be_nil
147
+ expect(subject['remove']).to be_nil
148
+ end
149
+ end
150
+ end
151
+ 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
+ end
10
163
  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.5.0
4
+ version: 0.6.0
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: 2015-09-18 00:00:00.000000000 Z
13
+ date: 2016-09-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: easy_diff
@@ -68,104 +68,6 @@ dependencies:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
- - !ruby/object:Gem::Dependency
72
- name: rake
73
- requirement: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- type: :development
79
- prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
85
- - !ruby/object:Gem::Dependency
86
- name: rspec
87
- requirement: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - "~>"
90
- - !ruby/object:Gem::Version
91
- version: '3.1'
92
- type: :development
93
- prerelease: false
94
- version_requirements: !ruby/object:Gem::Requirement
95
- requirements:
96
- - - "~>"
97
- - !ruby/object:Gem::Version
98
- version: '3.1'
99
- - !ruby/object:Gem::Dependency
100
- name: bundler
101
- requirement: !ruby/object:Gem::Requirement
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- version: '0'
106
- type: :development
107
- prerelease: false
108
- version_requirements: !ruby/object:Gem::Requirement
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: '0'
113
- - !ruby/object:Gem::Dependency
114
- name: rubocop
115
- requirement: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - '='
118
- - !ruby/object:Gem::Version
119
- version: 0.34.1
120
- type: :development
121
- prerelease: false
122
- version_requirements: !ruby/object:Gem::Requirement
123
- requirements:
124
- - - '='
125
- - !ruby/object:Gem::Version
126
- version: 0.34.1
127
- - !ruby/object:Gem::Dependency
128
- name: yard
129
- requirement: !ruby/object:Gem::Requirement
130
- requirements:
131
- - - ">="
132
- - !ruby/object:Gem::Version
133
- version: '0'
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- requirements:
138
- - - ">="
139
- - !ruby/object:Gem::Version
140
- version: '0'
141
- - !ruby/object:Gem::Dependency
142
- name: gem-release
143
- requirement: !ruby/object:Gem::Requirement
144
- requirements:
145
- - - ">="
146
- - !ruby/object:Gem::Version
147
- version: '0'
148
- type: :development
149
- prerelease: false
150
- version_requirements: !ruby/object:Gem::Requirement
151
- requirements:
152
- - - ">="
153
- - !ruby/object:Gem::Version
154
- version: '0'
155
- - !ruby/object:Gem::Dependency
156
- name: coveralls
157
- requirement: !ruby/object:Gem::Requirement
158
- requirements:
159
- - - ">="
160
- - !ruby/object:Gem::Version
161
- version: '0'
162
- type: :development
163
- prerelease: false
164
- version_requirements: !ruby/object:Gem::Requirement
165
- requirements:
166
- - - ">="
167
- - !ruby/object:Gem::Version
168
- version: '0'
169
71
  description: This library tracks historical changes for any document, including embedded
170
72
  ones. It achieves this by storing all history tracks in a single collection that
171
73
  you define. Embedded documents are referenced by storing an association path, which
@@ -192,12 +94,18 @@ files:
192
94
  - ".travis.yml"
193
95
  - CHANGELOG.md
194
96
  - CONTRIBUTING.md
97
+ - Dangerfile
195
98
  - Gemfile
196
99
  - LICENSE.txt
197
100
  - README.md
198
101
  - Rakefile
199
102
  - lib/mongoid-history.rb
200
103
  - lib/mongoid/history.rb
104
+ - lib/mongoid/history/attributes/base.rb
105
+ - lib/mongoid/history/attributes/create.rb
106
+ - lib/mongoid/history/attributes/destroy.rb
107
+ - lib/mongoid/history/attributes/update.rb
108
+ - lib/mongoid/history/options.rb
201
109
  - lib/mongoid/history/trackable.rb
202
110
  - lib/mongoid/history/tracker.rb
203
111
  - lib/mongoid/history/version.rb
@@ -211,6 +119,17 @@ files:
211
119
  - spec/spec_helper.rb
212
120
  - spec/support/mongoid.rb
213
121
  - spec/support/mongoid_history.rb
122
+ - spec/unit/attributes/base_spec.rb
123
+ - spec/unit/attributes/create_spec.rb
124
+ - spec/unit/attributes/destroy_spec.rb
125
+ - spec/unit/attributes/update_spec.rb
126
+ - spec/unit/embedded_methods_spec.rb
127
+ - spec/unit/history_spec.rb
128
+ - spec/unit/my_instance_methods_spec.rb
129
+ - spec/unit/options_spec.rb
130
+ - spec/unit/singleton_methods_spec.rb
131
+ - spec/unit/store/default_store_spec.rb
132
+ - spec/unit/store/request_store_spec.rb
214
133
  - spec/unit/trackable_spec.rb
215
134
  - spec/unit/tracker_spec.rb
216
135
  homepage: http://github.com/aq1018/mongoid-history
@@ -233,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
233
152
  version: '0'
234
153
  requirements: []
235
154
  rubyforge_project:
236
- rubygems_version: 2.2.2
155
+ rubygems_version: 2.5.1
237
156
  signing_key:
238
157
  specification_version: 4
239
158
  summary: Track and audit, undo and redo changes on Mongoid documents.
@@ -247,6 +166,16 @@ test_files:
247
166
  - spec/spec_helper.rb
248
167
  - spec/support/mongoid.rb
249
168
  - spec/support/mongoid_history.rb
169
+ - spec/unit/attributes/base_spec.rb
170
+ - spec/unit/attributes/create_spec.rb
171
+ - spec/unit/attributes/destroy_spec.rb
172
+ - spec/unit/attributes/update_spec.rb
173
+ - spec/unit/embedded_methods_spec.rb
174
+ - spec/unit/history_spec.rb
175
+ - spec/unit/my_instance_methods_spec.rb
176
+ - spec/unit/options_spec.rb
177
+ - spec/unit/singleton_methods_spec.rb
178
+ - spec/unit/store/default_store_spec.rb
179
+ - spec/unit/store/request_store_spec.rb
250
180
  - spec/unit/trackable_spec.rb
251
181
  - spec/unit/tracker_spec.rb
252
- has_rdoc: