mongoid-history 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -71
- data/.rubocop_todo.yml +61 -0
- data/.travis.yml +3 -1
- data/CHANGELOG.md +8 -0
- data/README.md +24 -0
- data/Rakefile +3 -3
- data/lib/mongoid/history.rb +12 -1
- data/lib/mongoid/history/mongoid.rb +1 -1
- data/lib/mongoid/history/trackable.rb +24 -10
- data/lib/mongoid/history/tracker.rb +8 -8
- data/lib/mongoid/history/version.rb +1 -1
- data/mongoid-history.gemspec +2 -2
- data/spec/integration/embedded_in_polymorphic_spec.rb +4 -4
- data/spec/integration/integration_spec.rb +391 -389
- data/spec/integration/multi_relation_spec.rb +11 -11
- data/spec/integration/nested_embedded_documents_spec.rb +7 -7
- data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +127 -0
- data/spec/integration/subclasses_spec.rb +4 -4
- data/spec/spec_helper.rb +2 -1
- data/spec/support/mongoid_history.rb +2 -2
- data/spec/unit/trackable_spec.rb +84 -84
- data/spec/unit/tracker_spec.rb +2 -2
- metadata +36 -34
- data/lib/mongoid-history.rb +0 -11
@@ -62,7 +62,7 @@ describe Mongoid::History do
|
|
62
62
|
# include Mongoid::Timestamps (see: https://github.com/mongoid/mongoid/issues/3078)
|
63
63
|
include Mongoid::History::Trackable
|
64
64
|
|
65
|
-
belongs_to :updated_by, class_name:
|
65
|
+
belongs_to :updated_by, class_name: 'User'
|
66
66
|
|
67
67
|
field :title
|
68
68
|
track_history on: [:title], scope: :post, track_create: true, track_destroy: true, modifier_field: :updated_by
|
@@ -75,348 +75,348 @@ describe Mongoid::History do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
78
|
-
let(:user) { User.create(name:
|
79
|
-
let(:another_user) { User.create(name:
|
80
|
-
let(:post) { Post.create(title:
|
81
|
-
let(:comment) { post.comments.create(title:
|
82
|
-
let(:tag) { Tag.create(title:
|
78
|
+
let(:user) { User.create(name: 'Aaron', email: 'aaron@randomemail.com', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') }
|
79
|
+
let(:another_user) { User.create(name: 'Another Guy', email: 'anotherguy@randomemail.com') }
|
80
|
+
let(:post) { Post.create(title: 'Test', body: 'Post', modifier: user, views: 100) }
|
81
|
+
let(:comment) { post.comments.create(title: 'test', body: 'comment', modifier: user) }
|
82
|
+
let(:tag) { Tag.create(title: 'test') }
|
83
83
|
|
84
|
-
describe
|
85
|
-
describe
|
86
|
-
it
|
87
|
-
comment.history_tracks.count.
|
84
|
+
describe 'track' do
|
85
|
+
describe 'on creation' do
|
86
|
+
it 'should have one history track in comment' do
|
87
|
+
expect(comment.history_tracks.count).to eq(1)
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
91
|
-
comment.history_tracks.first.modified.
|
90
|
+
it 'should assign title and body on modified' do
|
91
|
+
expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment')
|
92
92
|
end
|
93
93
|
|
94
|
-
it
|
95
|
-
comment.history_tracks.first.original.
|
94
|
+
it 'should not assign title and body on original' do
|
95
|
+
expect(comment.history_tracks.first.original).to eq({})
|
96
96
|
end
|
97
97
|
|
98
|
-
it
|
99
|
-
comment.history_tracks.first.modifier.
|
98
|
+
it 'should assign modifier' do
|
99
|
+
expect(comment.history_tracks.first.modifier).to eq(user)
|
100
100
|
end
|
101
101
|
|
102
|
-
it
|
103
|
-
comment.history_tracks.first.version.
|
102
|
+
it 'should assign version' do
|
103
|
+
expect(comment.history_tracks.first.version).to eq(1)
|
104
104
|
end
|
105
105
|
|
106
|
-
it
|
107
|
-
comment.history_tracks.first.scope.
|
106
|
+
it 'should assign scope' do
|
107
|
+
expect(comment.history_tracks.first.scope).to eq('post')
|
108
108
|
end
|
109
109
|
|
110
|
-
it
|
111
|
-
comment.history_tracks.first.action.
|
110
|
+
it 'should assign method' do
|
111
|
+
expect(comment.history_tracks.first.action).to eq('create')
|
112
112
|
end
|
113
113
|
|
114
|
-
it
|
114
|
+
it 'should assign association_chain' do
|
115
115
|
expected = [
|
116
|
-
{ 'id' => post.id, 'name' =>
|
117
|
-
{ 'id' => comment.id, 'name' =>
|
116
|
+
{ 'id' => post.id, 'name' => 'Post' },
|
117
|
+
{ 'id' => comment.id, 'name' => 'coms' }
|
118
118
|
]
|
119
|
-
comment.history_tracks.first.association_chain.
|
119
|
+
expect(comment.history_tracks.first.association_chain).to eq(expected)
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
-
describe
|
124
|
-
it
|
125
|
-
|
123
|
+
describe 'on destruction' do
|
124
|
+
it 'should have two history track records in post' do
|
125
|
+
expect do
|
126
126
|
post.destroy
|
127
|
-
|
127
|
+
end.to change(Tracker, :count).by(1)
|
128
128
|
end
|
129
129
|
|
130
|
-
it
|
130
|
+
it 'should assign destroy on track record' do
|
131
131
|
post.destroy
|
132
|
-
post.history_tracks.last.action.
|
132
|
+
expect(post.history_tracks.last.action).to eq('destroy')
|
133
133
|
end
|
134
134
|
|
135
|
-
it
|
135
|
+
it 'should return affected attributes from track record' do
|
136
136
|
post.destroy
|
137
|
-
post.history_tracks.last.affected[
|
137
|
+
expect(post.history_tracks.last.affected['title']).to eq('Test')
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
|
-
describe
|
142
|
-
it
|
143
|
-
|
144
|
-
post.update_attributes(title:
|
145
|
-
|
141
|
+
describe 'on update non-embedded' do
|
142
|
+
it 'should create a history track if changed attributes match tracked attributes' do
|
143
|
+
expect do
|
144
|
+
post.update_attributes(title: 'Another Test')
|
145
|
+
end.to change(Tracker, :count).by(1)
|
146
146
|
end
|
147
147
|
|
148
|
-
it
|
149
|
-
|
150
|
-
post.update_attributes(rating:
|
151
|
-
|
148
|
+
it 'should not create a history track if changed attributes do not match tracked attributes' do
|
149
|
+
expect do
|
150
|
+
post.update_attributes(rating: 'untracked')
|
151
|
+
end.to change(Tracker, :count).by(0)
|
152
152
|
end
|
153
153
|
|
154
|
-
it
|
155
|
-
post.update_attributes(title:
|
156
|
-
post.history_tracks.last.modified.
|
157
|
-
|
158
|
-
|
154
|
+
it 'should assign modified fields' do
|
155
|
+
post.update_attributes(title: 'Another Test')
|
156
|
+
expect(post.history_tracks.last.modified).to eq(
|
157
|
+
'title' => 'Another Test'
|
158
|
+
)
|
159
159
|
end
|
160
160
|
|
161
|
-
it
|
162
|
-
post.update_attributes(title:
|
163
|
-
post.history_tracks.last.action.
|
161
|
+
it 'should assign method field' do
|
162
|
+
post.update_attributes(title: 'Another Test')
|
163
|
+
expect(post.history_tracks.last.action).to eq('update')
|
164
164
|
end
|
165
165
|
|
166
|
-
it
|
167
|
-
post.update_attributes(title:
|
168
|
-
post.history_tracks.last.original.
|
169
|
-
|
170
|
-
|
166
|
+
it 'should assign original fields' do
|
167
|
+
post.update_attributes(title: 'Another Test')
|
168
|
+
expect(post.history_tracks.last.original).to eq(
|
169
|
+
'title' => 'Test'
|
170
|
+
)
|
171
171
|
end
|
172
172
|
|
173
|
-
it
|
174
|
-
post.update_attributes(title:
|
175
|
-
post.history_tracks.first.modifier.
|
173
|
+
it 'should assign modifier' do
|
174
|
+
post.update_attributes(title: 'Another Test')
|
175
|
+
expect(post.history_tracks.first.modifier).to eq(user)
|
176
176
|
end
|
177
177
|
|
178
|
-
it
|
179
|
-
post.update_attributes(title:
|
180
|
-
post.history_tracks.first.version.
|
178
|
+
it 'should assign version on history tracks' do
|
179
|
+
post.update_attributes(title: 'Another Test')
|
180
|
+
expect(post.history_tracks.first.version).to eq(1)
|
181
181
|
end
|
182
182
|
|
183
|
-
it
|
184
|
-
post.update_attributes(title:
|
185
|
-
post.version.
|
183
|
+
it 'should assign version on post' do
|
184
|
+
post.update_attributes(title: 'Another Test')
|
185
|
+
expect(post.version).to eq(1)
|
186
186
|
end
|
187
187
|
|
188
|
-
it
|
189
|
-
post.update_attributes(title:
|
190
|
-
post.history_tracks.first.scope.
|
188
|
+
it 'should assign scope' do
|
189
|
+
post.update_attributes(title: 'Another Test')
|
190
|
+
expect(post.history_tracks.first.scope).to eq('post')
|
191
191
|
end
|
192
192
|
|
193
|
-
it
|
194
|
-
post.update_attributes(title:
|
195
|
-
post.history_tracks.last.association_chain.
|
193
|
+
it 'should assign association_chain' do
|
194
|
+
post.update_attributes(title: 'Another Test')
|
195
|
+
expect(post.history_tracks.last.association_chain).to eq([{ 'id' => post.id, 'name' => 'Post' }])
|
196
196
|
end
|
197
197
|
|
198
|
-
it
|
198
|
+
it 'should exclude defined options' do
|
199
199
|
name = user.name
|
200
|
-
user.update_attributes(name:
|
201
|
-
user.history_tracks.first.original.keys.
|
202
|
-
user.history_tracks.first.original[
|
203
|
-
user.history_tracks.first.modified.keys.
|
204
|
-
user.history_tracks.first.modified[
|
200
|
+
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
|
201
|
+
expect(user.history_tracks.first.original.keys).to eq(['n'])
|
202
|
+
expect(user.history_tracks.first.original['n']).to eq(name)
|
203
|
+
expect(user.history_tracks.first.modified.keys).to eq(['n'])
|
204
|
+
expect(user.history_tracks.first.modified['n']).to eq(user.name)
|
205
205
|
end
|
206
206
|
|
207
|
-
it
|
207
|
+
it 'should undo field changes' do
|
208
208
|
name = user.name
|
209
|
-
user.update_attributes(name:
|
209
|
+
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
|
210
210
|
user.history_tracks.first.undo! nil
|
211
|
-
user.reload.name.
|
211
|
+
expect(user.reload.name).to eq(name)
|
212
212
|
end
|
213
213
|
|
214
|
-
it
|
214
|
+
it 'should undo non-existing field changes' do
|
215
215
|
post = Post.create(modifier: user, views: 100)
|
216
|
-
post.reload.title.
|
217
|
-
post.update_attributes(title:
|
218
|
-
post.reload.title.
|
216
|
+
expect(post.reload.title).to be_nil
|
217
|
+
post.update_attributes(title: 'Aaron2')
|
218
|
+
expect(post.reload.title).to eq('Aaron2')
|
219
219
|
post.history_tracks.first.undo! nil
|
220
|
-
post.reload.title.
|
220
|
+
expect(post.reload.title).to be_nil
|
221
221
|
end
|
222
222
|
|
223
|
-
it
|
223
|
+
it 'should track array changes' do
|
224
224
|
aliases = user.aliases
|
225
|
-
user.update_attributes(aliases:
|
226
|
-
user.history_tracks.first.original[
|
227
|
-
user.history_tracks.first.modified[
|
225
|
+
user.update_attributes(aliases: %w(bob joe))
|
226
|
+
expect(user.history_tracks.first.original['aliases']).to eq(aliases)
|
227
|
+
expect(user.history_tracks.first.modified['aliases']).to eq(user.aliases)
|
228
228
|
end
|
229
229
|
|
230
|
-
it
|
230
|
+
it 'should undo array changes' do
|
231
231
|
aliases = user.aliases
|
232
|
-
user.update_attributes(aliases:
|
232
|
+
user.update_attributes(aliases: %w(bob joe))
|
233
233
|
user.history_tracks.first.undo! nil
|
234
|
-
user.reload.aliases.
|
234
|
+
expect(user.reload.aliases).to eq(aliases)
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
238
|
-
describe
|
239
|
-
context
|
238
|
+
describe '#tracked_changes' do
|
239
|
+
context 'create action' do
|
240
240
|
subject { tag.history_tracks.first.tracked_changes }
|
241
|
-
it
|
242
|
-
subject[:title].
|
241
|
+
it 'consider all fields values as :to' do
|
242
|
+
expect(subject[:title]).to eq({ to: 'test' }.with_indifferent_access)
|
243
243
|
end
|
244
244
|
end
|
245
|
-
context
|
246
|
-
subject
|
245
|
+
context 'destroy action' do
|
246
|
+
subject do
|
247
247
|
tag.destroy
|
248
248
|
tag.history_tracks.last.tracked_changes
|
249
|
-
|
250
|
-
it
|
251
|
-
subject[:title].
|
249
|
+
end
|
250
|
+
it 'consider all fields values as :from' do
|
251
|
+
expect(subject[:title]).to eq({ from: 'test' }.with_indifferent_access)
|
252
252
|
end
|
253
253
|
end
|
254
|
-
context
|
254
|
+
context 'update action' do
|
255
255
|
subject { user.history_tracks.first.tracked_changes }
|
256
256
|
before do
|
257
|
-
user.update_attributes(name:
|
257
|
+
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
|
258
258
|
end
|
259
|
-
it {
|
260
|
-
it
|
261
|
-
subject[:n].
|
259
|
+
it { is_expected.to be_a HashWithIndifferentAccess }
|
260
|
+
it 'should track changed field' do
|
261
|
+
expect(subject[:n]).to eq({ from: 'Aaron', to: 'Aaron2' }.with_indifferent_access)
|
262
262
|
end
|
263
|
-
it
|
264
|
-
subject[:phone].
|
263
|
+
it 'should track added field' do
|
264
|
+
expect(subject[:phone]).to eq({ to: '867-5309' }.with_indifferent_access)
|
265
265
|
end
|
266
|
-
it
|
267
|
-
subject[:city].
|
266
|
+
it 'should track removed field' do
|
267
|
+
expect(subject[:city]).to eq({ from: 'Toronto' }.with_indifferent_access)
|
268
268
|
end
|
269
|
-
it
|
270
|
-
subject[:country].
|
269
|
+
it 'should not consider blank as removed' do
|
270
|
+
expect(subject[:country]).to eq({ from: 'Canada', to: '' }.with_indifferent_access)
|
271
271
|
end
|
272
|
-
it
|
273
|
-
subject[:aliases].
|
272
|
+
it 'should track changed array field' do
|
273
|
+
expect(subject[:aliases]).to eq({ from: ['bob'], to: ['', 'bill', 'james'] }.with_indifferent_access)
|
274
274
|
end
|
275
|
-
it
|
276
|
-
subject[:address].
|
275
|
+
it 'should not track unmodified field' do
|
276
|
+
expect(subject[:address]).to be_nil
|
277
277
|
end
|
278
|
-
it
|
279
|
-
subject[:email].
|
278
|
+
it 'should not track untracked fields' do
|
279
|
+
expect(subject[:email]).to be_nil
|
280
280
|
end
|
281
281
|
end
|
282
282
|
end
|
283
283
|
|
284
|
-
describe
|
285
|
-
context
|
284
|
+
describe '#tracked_edits' do
|
285
|
+
context 'create action' do
|
286
286
|
subject { tag.history_tracks.first.tracked_edits }
|
287
|
-
it
|
288
|
-
subject[:add].
|
287
|
+
it 'consider all edits as ;add' do
|
288
|
+
expect(subject[:add]).to eq({ title: 'test' }.with_indifferent_access)
|
289
289
|
end
|
290
290
|
end
|
291
|
-
context
|
292
|
-
subject
|
291
|
+
context 'destroy action' do
|
292
|
+
subject do
|
293
293
|
tag.destroy
|
294
294
|
tag.history_tracks.last.tracked_edits
|
295
|
-
|
296
|
-
it
|
297
|
-
subject[:remove].
|
295
|
+
end
|
296
|
+
it 'consider all edits as ;remove' do
|
297
|
+
expect(subject[:remove]).to eq({ title: 'test' }.with_indifferent_access)
|
298
298
|
end
|
299
299
|
end
|
300
|
-
context
|
300
|
+
context 'update action' do
|
301
301
|
subject { user.history_tracks.first.tracked_edits }
|
302
302
|
before do
|
303
|
-
user.update_attributes(name:
|
303
|
+
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
|
304
304
|
end
|
305
|
-
it {
|
306
|
-
it
|
307
|
-
subject[:modify].
|
305
|
+
it { is_expected.to be_a HashWithIndifferentAccess }
|
306
|
+
it 'should track changed field' do
|
307
|
+
expect(subject[:modify]).to eq({ n: { from: 'Aaron', to: 'Aaron2' } }.with_indifferent_access)
|
308
308
|
end
|
309
|
-
it
|
310
|
-
subject[:add].
|
309
|
+
it 'should track added field' do
|
310
|
+
expect(subject[:add]).to eq({ phone: '867-5309' }.with_indifferent_access)
|
311
311
|
end
|
312
|
-
it
|
313
|
-
subject[:remove].
|
312
|
+
it 'should track removed field and consider blank as removed' do
|
313
|
+
expect(subject[:remove]).to eq({ city: 'Toronto', country: 'Canada' }.with_indifferent_access)
|
314
314
|
end
|
315
|
-
it
|
316
|
-
subject[:array].
|
315
|
+
it 'should track changed array field' do
|
316
|
+
expect(subject[:array]).to eq({ aliases: { remove: ['bob'], add: ['', 'bill', 'james'] } }.with_indifferent_access)
|
317
317
|
end
|
318
|
-
it
|
318
|
+
it 'should not track unmodified field' do
|
319
319
|
%w(add modify remove array).each do |edit|
|
320
|
-
subject[edit][:address].
|
320
|
+
expect(subject[edit][:address]).to be_nil
|
321
321
|
end
|
322
322
|
end
|
323
|
-
it
|
323
|
+
it 'should not track untracked fields' do
|
324
324
|
%w(add modify remove array).each do |edit|
|
325
|
-
subject[edit][:email].
|
325
|
+
expect(subject[edit][:email]).to be_nil
|
326
326
|
end
|
327
327
|
end
|
328
328
|
end
|
329
|
-
context
|
329
|
+
context 'with empty values' do
|
330
330
|
subject { Tracker.new }
|
331
|
-
it
|
332
|
-
subject.
|
333
|
-
subject.tracked_edits.
|
331
|
+
it 'should skip empty values' do
|
332
|
+
allow(subject).to receive(:tracked_changes) { { name: { to: '', from: [] }, city: { to: 'Toronto', from: '' } } }
|
333
|
+
expect(subject.tracked_edits).to eq({ add: { city: 'Toronto' } }.with_indifferent_access)
|
334
334
|
end
|
335
335
|
end
|
336
336
|
end
|
337
337
|
|
338
|
-
describe
|
339
|
-
it
|
340
|
-
post.update_attributes(title:
|
341
|
-
post.update_attributes(title:
|
342
|
-
post.version.
|
338
|
+
describe 'on update non-embedded twice' do
|
339
|
+
it 'should assign version on post' do
|
340
|
+
post.update_attributes(title: 'Test2')
|
341
|
+
post.update_attributes(title: 'Test3')
|
342
|
+
expect(post.version).to eq(2)
|
343
343
|
end
|
344
344
|
|
345
|
-
it
|
346
|
-
|
347
|
-
post.update_attributes(title:
|
348
|
-
post.update_attributes(title:
|
349
|
-
|
345
|
+
it 'should create a history track if changed attributes match tracked attributes' do
|
346
|
+
expect do
|
347
|
+
post.update_attributes(title: 'Test2')
|
348
|
+
post.update_attributes(title: 'Test3')
|
349
|
+
end.to change(Tracker, :count).by(2)
|
350
350
|
end
|
351
351
|
|
352
|
-
it
|
353
|
-
post.update_attributes(title:
|
354
|
-
post.update_attributes(title:
|
355
|
-
post.history_tracks.where(version: 2).first.
|
352
|
+
it 'should create a history track of version 2' do
|
353
|
+
post.update_attributes(title: 'Test2')
|
354
|
+
post.update_attributes(title: 'Test3')
|
355
|
+
expect(post.history_tracks.where(version: 2).first).not_to be_nil
|
356
356
|
end
|
357
357
|
|
358
|
-
it
|
359
|
-
post.update_attributes(title:
|
360
|
-
post.update_attributes(title:
|
361
|
-
post.history_tracks.where(version: 2).first.modified.
|
362
|
-
|
363
|
-
|
358
|
+
it 'should assign modified fields' do
|
359
|
+
post.update_attributes(title: 'Test2')
|
360
|
+
post.update_attributes(title: 'Test3')
|
361
|
+
expect(post.history_tracks.where(version: 2).first.modified).to eq(
|
362
|
+
'title' => 'Test3'
|
363
|
+
)
|
364
364
|
end
|
365
365
|
|
366
|
-
it
|
367
|
-
post.update_attributes(title:
|
368
|
-
post.update_attributes(title:
|
369
|
-
post.history_tracks.where(version: 2).first.original.
|
370
|
-
|
371
|
-
|
366
|
+
it 'should assign original fields' do
|
367
|
+
post.update_attributes(title: 'Test2')
|
368
|
+
post.update_attributes(title: 'Test3')
|
369
|
+
expect(post.history_tracks.where(version: 2).first.original).to eq(
|
370
|
+
'title' => 'Test2'
|
371
|
+
)
|
372
372
|
end
|
373
373
|
|
374
|
-
it
|
375
|
-
post.update_attributes(title:
|
376
|
-
post.history_tracks.last.modifier.
|
374
|
+
it 'should assign modifier' do
|
375
|
+
post.update_attributes(title: 'Another Test', modifier: another_user)
|
376
|
+
expect(post.history_tracks.last.modifier).to eq(another_user)
|
377
377
|
end
|
378
378
|
end
|
379
379
|
|
380
|
-
describe
|
381
|
-
it
|
382
|
-
comment.update_attributes(title:
|
383
|
-
comment.version.
|
380
|
+
describe 'on update embedded 1..N (embeds_many)' do
|
381
|
+
it 'should assign version on comment' do
|
382
|
+
comment.update_attributes(title: 'Test2')
|
383
|
+
expect(comment.version).to eq(2) # first track generated on creation
|
384
384
|
end
|
385
385
|
|
386
|
-
it
|
387
|
-
comment.update_attributes(title:
|
388
|
-
comment.history_tracks.where(version: 2).first.
|
386
|
+
it 'should create a history track of version 2' do
|
387
|
+
comment.update_attributes(title: 'Test2')
|
388
|
+
expect(comment.history_tracks.where(version: 2).first).not_to be_nil
|
389
389
|
end
|
390
390
|
|
391
|
-
it
|
392
|
-
comment.update_attributes(t:
|
393
|
-
comment.history_tracks.where(version: 2).first.modified.
|
394
|
-
|
395
|
-
|
391
|
+
it 'should assign modified fields' do
|
392
|
+
comment.update_attributes(t: 'Test2')
|
393
|
+
expect(comment.history_tracks.where(version: 2).first.modified).to eq(
|
394
|
+
't' => 'Test2'
|
395
|
+
)
|
396
396
|
end
|
397
397
|
|
398
|
-
it
|
399
|
-
comment.update_attributes(title:
|
400
|
-
comment.history_tracks.where(version: 2).first.original.
|
401
|
-
|
402
|
-
|
398
|
+
it 'should assign original fields' do
|
399
|
+
comment.update_attributes(title: 'Test2')
|
400
|
+
expect(comment.history_tracks.where(version: 2).first.original).to eq(
|
401
|
+
't' => 'test'
|
402
|
+
)
|
403
403
|
end
|
404
404
|
|
405
|
-
it
|
406
|
-
comment.update_attributes(title:
|
405
|
+
it 'should be possible to undo from parent' do
|
406
|
+
comment.update_attributes(title: 'Test 2')
|
407
407
|
user
|
408
408
|
post.history_tracks.last.undo!(user)
|
409
409
|
comment.reload
|
410
|
-
comment.title.
|
410
|
+
expect(comment.title).to eq('test')
|
411
411
|
end
|
412
412
|
|
413
|
-
it
|
414
|
-
post.update_attributes(title:
|
415
|
-
post.history_tracks.last.modifier.
|
413
|
+
it 'should assign modifier' do
|
414
|
+
post.update_attributes(title: 'Another Test', modifier: another_user)
|
415
|
+
expect(post.history_tracks.last.modifier).to eq(another_user)
|
416
416
|
end
|
417
417
|
end
|
418
418
|
|
419
|
-
describe
|
419
|
+
describe 'on update embedded 1..1 (embeds_one)' do
|
420
420
|
let(:section) { Section.new(title: 'Technology') }
|
421
421
|
|
422
422
|
before(:each) do
|
@@ -426,85 +426,85 @@ describe Mongoid::History do
|
|
426
426
|
post.section
|
427
427
|
end
|
428
428
|
|
429
|
-
it
|
430
|
-
section.version.
|
429
|
+
it 'should assign version on create section' do
|
430
|
+
expect(section.version).to eq(1)
|
431
431
|
end
|
432
432
|
|
433
|
-
it
|
433
|
+
it 'should assign version on section' do
|
434
434
|
section.update_attributes(title: 'Technology 2')
|
435
|
-
section.version.
|
435
|
+
expect(section.version).to eq(2) # first track generated on creation
|
436
436
|
end
|
437
437
|
|
438
|
-
it
|
438
|
+
it 'should create a history track of version 2' do
|
439
439
|
section.update_attributes(title: 'Technology 2')
|
440
|
-
section.history_tracks.where(version: 2).first.
|
440
|
+
expect(section.history_tracks.where(version: 2).first).not_to be_nil
|
441
441
|
end
|
442
442
|
|
443
|
-
it
|
443
|
+
it 'should assign modified fields' do
|
444
444
|
section.update_attributes(title: 'Technology 2')
|
445
|
-
section.history_tracks.where(version: 2).first.modified.
|
446
|
-
|
447
|
-
|
445
|
+
expect(section.history_tracks.where(version: 2).first.modified).to eq(
|
446
|
+
't' => 'Technology 2'
|
447
|
+
)
|
448
448
|
end
|
449
449
|
|
450
|
-
it
|
450
|
+
it 'should assign original fields' do
|
451
451
|
section.update_attributes(title: 'Technology 2')
|
452
|
-
section.history_tracks.where(version: 2).first.original.
|
453
|
-
|
454
|
-
|
452
|
+
expect(section.history_tracks.where(version: 2).first.original).to eq(
|
453
|
+
't' => 'Technology'
|
454
|
+
)
|
455
455
|
end
|
456
456
|
|
457
|
-
it
|
457
|
+
it 'should be possible to undo from parent' do
|
458
458
|
section.update_attributes(title: 'Technology 2')
|
459
459
|
post.history_tracks.last.undo!(user)
|
460
460
|
section.reload
|
461
|
-
section.title.
|
461
|
+
expect(section.title).to eq('Technology')
|
462
462
|
end
|
463
463
|
|
464
|
-
it
|
465
|
-
section.update_attributes(title:
|
466
|
-
post.history_tracks.last.modifier.
|
464
|
+
it 'should assign modifier' do
|
465
|
+
section.update_attributes(title: 'Business', modifier: another_user)
|
466
|
+
expect(post.history_tracks.last.modifier).to eq(another_user)
|
467
467
|
end
|
468
468
|
end
|
469
469
|
|
470
|
-
describe
|
471
|
-
it
|
470
|
+
describe 'on destroy embedded' do
|
471
|
+
it 'should be possible to re-create destroyed embedded' do
|
472
472
|
comment.destroy
|
473
473
|
comment.history_tracks.last.undo!(user)
|
474
474
|
post.reload
|
475
|
-
post.comments.first.title.
|
475
|
+
expect(post.comments.first.title).to eq('test')
|
476
476
|
end
|
477
477
|
|
478
|
-
it
|
478
|
+
it 'should be possible to re-create destroyed embedded from parent' do
|
479
479
|
comment.destroy
|
480
480
|
post.history_tracks.last.undo!(user)
|
481
481
|
post.reload
|
482
|
-
post.comments.first.title.
|
482
|
+
expect(post.comments.first.title).to eq('test')
|
483
483
|
end
|
484
484
|
|
485
|
-
it
|
485
|
+
it 'should be possible to destroy after re-create embedded from parent' do
|
486
486
|
comment.destroy
|
487
487
|
post.history_tracks.last.undo!(user)
|
488
488
|
post.history_tracks.last.undo!(user)
|
489
489
|
post.reload
|
490
|
-
post.comments.count.
|
490
|
+
expect(post.comments.count).to eq(0)
|
491
491
|
end
|
492
492
|
|
493
|
-
it
|
493
|
+
it 'should be possible to create with redo after undo create embedded from parent' do
|
494
494
|
comment # initialize
|
495
|
-
post.comments.create!(title:
|
495
|
+
post.comments.create!(title: 'The second one')
|
496
496
|
track = post.history_tracks.last
|
497
497
|
track.undo!(user)
|
498
498
|
track.redo!(user)
|
499
499
|
post.reload
|
500
|
-
post.comments.count.
|
500
|
+
expect(post.comments.count).to eq(2)
|
501
501
|
end
|
502
502
|
end
|
503
503
|
|
504
|
-
describe
|
504
|
+
describe 'embedded with cascading callbacks' do
|
505
505
|
|
506
|
-
let(:tag_foo) { post.tags.create(title:
|
507
|
-
let(:tag_bar) { post.tags.create(title:
|
506
|
+
let(:tag_foo) { post.tags.create(title: 'foo', updated_by: user) }
|
507
|
+
let(:tag_bar) { post.tags.create(title: 'bar') }
|
508
508
|
|
509
509
|
# it "should have cascaded the creation callbacks and set timestamps" do
|
510
510
|
# tag_foo; tag_bar # initialize
|
@@ -512,167 +512,169 @@ describe Mongoid::History do
|
|
512
512
|
# tag_foo.updated_at.should_not be_nil
|
513
513
|
# end
|
514
514
|
|
515
|
-
it
|
516
|
-
update_hash = {
|
517
|
-
post.update_attributes(update_hash[
|
518
|
-
post.tags.last.title.
|
515
|
+
it 'should allow an update through the parent model' do
|
516
|
+
update_hash = { 'post' => { 'tags_attributes' => { '1234' => { 'id' => tag_bar.id, 'title' => 'baz' } } } }
|
517
|
+
post.update_attributes(update_hash['post'])
|
518
|
+
expect(post.tags.last.title).to eq('baz')
|
519
519
|
end
|
520
520
|
|
521
|
-
it
|
521
|
+
it 'should be possible to destroy through parent model using canoncial _destroy macro' do
|
522
522
|
tag_foo
|
523
523
|
tag_bar # initialize
|
524
|
-
post.tags.count.
|
525
|
-
update_hash = {
|
526
|
-
post.update_attributes(update_hash[
|
527
|
-
post.tags.count.
|
528
|
-
post.history_tracks.to_a.last.action.
|
524
|
+
expect(post.tags.count).to eq(2)
|
525
|
+
update_hash = { 'post' => { 'tags_attributes' => { '1234' => { 'id' => tag_bar.id, 'title' => 'baz', '_destroy' => 'true' } } } }
|
526
|
+
post.update_attributes(update_hash['post'])
|
527
|
+
expect(post.tags.count).to eq(1)
|
528
|
+
expect(post.history_tracks.to_a.last.action).to eq('destroy')
|
529
529
|
end
|
530
530
|
|
531
|
-
it
|
532
|
-
update_hash = {
|
531
|
+
it 'should write relationship name for association_chain hiearchy instead of class name when using _destroy macro' do
|
532
|
+
update_hash = { 'tags_attributes' => { '1234' => { 'id' => tag_foo.id, '_destroy' => '1' } } }
|
533
533
|
post.update_attributes(update_hash)
|
534
534
|
|
535
535
|
# historically this would have evaluated to 'Tags' and an error would be thrown
|
536
536
|
# on any call that walked up the association_chain, e.g. 'trackable'
|
537
|
-
tag_foo.history_tracks.last.association_chain.last[
|
538
|
-
|
537
|
+
expect(tag_foo.history_tracks.last.association_chain.last['name']).to eq('tags')
|
538
|
+
expect { tag_foo.history_tracks.last.trackable }.not_to raise_error
|
539
539
|
end
|
540
540
|
end
|
541
541
|
|
542
|
-
describe
|
543
|
-
it
|
544
|
-
post.update_attributes(title:
|
542
|
+
describe 'non-embedded' do
|
543
|
+
it 'should undo changes' do
|
544
|
+
post.update_attributes(title: 'Test2')
|
545
545
|
post.history_tracks.where(version: 1).last.undo!(user)
|
546
546
|
post.reload
|
547
|
-
post.title.
|
547
|
+
expect(post.title).to eq('Test')
|
548
548
|
end
|
549
549
|
|
550
|
-
it
|
550
|
+
it 'should undo destruction' do
|
551
551
|
post.destroy
|
552
552
|
post.history_tracks.where(version: 1).last.undo!(user)
|
553
|
-
Post.find(post.id).title.
|
553
|
+
expect(Post.find(post.id).title).to eq('Test')
|
554
554
|
end
|
555
555
|
|
556
|
-
it
|
556
|
+
it 'should create a new history track after undo' do
|
557
557
|
comment # initialize
|
558
|
-
post.update_attributes(title:
|
558
|
+
post.update_attributes(title: 'Test2')
|
559
559
|
post.history_tracks.last.undo!(user)
|
560
560
|
post.reload
|
561
|
-
post.history_tracks.count.
|
561
|
+
expect(post.history_tracks.count).to eq(3)
|
562
562
|
end
|
563
563
|
|
564
|
-
it
|
565
|
-
post.update_attributes(title:
|
564
|
+
it 'should assign user as the modifier of the newly created history track' do
|
565
|
+
post.update_attributes(title: 'Test2')
|
566
566
|
post.history_tracks.where(version: 1).last.undo!(user)
|
567
567
|
post.reload
|
568
|
-
post.history_tracks.where(version: 2).last.modifier.
|
568
|
+
expect(post.history_tracks.where(version: 2).last.modifier).to eq(user)
|
569
569
|
end
|
570
570
|
|
571
|
-
it
|
572
|
-
post.update_attributes(title:
|
571
|
+
it 'should stay the same after undo and redo' do
|
572
|
+
post.update_attributes(title: 'Test2')
|
573
573
|
track = post.history_tracks.last
|
574
574
|
track.undo!(user)
|
575
575
|
track.redo!(user)
|
576
576
|
post2 = Post.where(_id: post.id).first
|
577
577
|
|
578
|
-
post.title.
|
578
|
+
expect(post.title).to eq(post2.title)
|
579
579
|
end
|
580
580
|
|
581
|
-
it
|
581
|
+
it 'should be destroyed after undo and redo' do
|
582
582
|
post.destroy
|
583
583
|
track = post.history_tracks.where(version: 1).last
|
584
584
|
track.undo!(user)
|
585
585
|
track.redo!(user)
|
586
|
-
Post.where(_id: post.id).first.
|
586
|
+
expect(Post.where(_id: post.id).first).to be_nil
|
587
587
|
end
|
588
588
|
end
|
589
589
|
|
590
|
-
describe
|
591
|
-
it
|
592
|
-
comment.update_attributes(title:
|
590
|
+
describe 'embedded' do
|
591
|
+
it 'should undo changes' do
|
592
|
+
comment.update_attributes(title: 'Test2')
|
593
593
|
comment.history_tracks.where(version: 2).first.undo!(user)
|
594
594
|
comment.reload
|
595
|
-
comment.title.
|
595
|
+
expect(comment.title).to eq('test')
|
596
596
|
end
|
597
597
|
|
598
|
-
it
|
599
|
-
comment.update_attributes(title:
|
598
|
+
it 'should create a new history track after undo' do
|
599
|
+
comment.update_attributes(title: 'Test2')
|
600
600
|
comment.history_tracks.where(version: 2).first.undo!(user)
|
601
601
|
comment.reload
|
602
|
-
comment.history_tracks.count.
|
602
|
+
expect(comment.history_tracks.count).to eq(3)
|
603
603
|
end
|
604
604
|
|
605
|
-
it
|
606
|
-
comment.update_attributes(title:
|
605
|
+
it 'should assign user as the modifier of the newly created history track' do
|
606
|
+
comment.update_attributes(title: 'Test2')
|
607
607
|
comment.history_tracks.where(version: 2).first.undo!(user)
|
608
608
|
comment.reload
|
609
|
-
comment.history_tracks.where(version: 3).first.modifier.
|
609
|
+
expect(comment.history_tracks.where(version: 3).first.modifier).to eq(user)
|
610
610
|
end
|
611
611
|
|
612
|
-
it
|
613
|
-
comment.update_attributes(title:
|
612
|
+
it 'should stay the same after undo and redo' do
|
613
|
+
comment.update_attributes(title: 'Test2')
|
614
614
|
track = comment.history_tracks.where(version: 2).first
|
615
615
|
track.undo!(user)
|
616
616
|
track.redo!(user)
|
617
617
|
comment.reload
|
618
|
-
comment.title.
|
618
|
+
expect(comment.title).to eq('Test2')
|
619
619
|
end
|
620
620
|
end
|
621
621
|
|
622
|
-
describe
|
622
|
+
describe 'trackables' do
|
623
623
|
before :each do
|
624
|
-
comment.update_attributes!(title:
|
625
|
-
comment.update_attributes!(title:
|
626
|
-
comment.update_attributes!(title:
|
627
|
-
end
|
628
|
-
|
629
|
-
describe
|
630
|
-
[nil, :reload].each do |
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
it "should recognize parameter as version number" do
|
639
|
-
comment.undo! user, 3
|
640
|
-
comment.send(method) if method
|
641
|
-
comment.title.should == "Test2"
|
642
|
-
end
|
643
|
-
|
644
|
-
it "should undo last version when no parameter is specified" do
|
645
|
-
comment.undo! user
|
646
|
-
comment.send(method) if method
|
647
|
-
comment.title.should == "Test3"
|
648
|
-
end
|
649
|
-
|
650
|
-
it "should recognize :last options" do
|
651
|
-
comment.undo! user, last: 2
|
652
|
-
comment.send(method) if method
|
653
|
-
comment.title.should == "Test2"
|
654
|
-
end
|
624
|
+
comment.update_attributes!(title: 'Test2') # version == 2
|
625
|
+
comment.update_attributes!(title: 'Test3') # version == 3
|
626
|
+
comment.update_attributes!(title: 'Test4') # version == 4
|
627
|
+
end
|
628
|
+
|
629
|
+
describe 'undo' do
|
630
|
+
{ 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods|
|
631
|
+
methods.each do |method|
|
632
|
+
context "#{method || 'instance'}" do
|
633
|
+
it 'recognizes :from, :to options' do
|
634
|
+
comment.send test_method, user, from: 4, to: 2
|
635
|
+
comment.send(method) if method
|
636
|
+
expect(comment.title).to eq('test')
|
637
|
+
end
|
655
638
|
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
639
|
+
it 'recognizes parameter as version number' do
|
640
|
+
comment.send test_method, user, 3
|
641
|
+
comment.send(method) if method
|
642
|
+
expect(comment.title).to eq('Test2')
|
643
|
+
end
|
661
644
|
|
662
|
-
|
663
|
-
|
664
|
-
|
645
|
+
it 'should undo last version when no parameter is specified' do
|
646
|
+
comment.send test_method, user
|
647
|
+
comment.send(method) if method
|
648
|
+
expect(comment.title).to eq('Test3')
|
649
|
+
end
|
665
650
|
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
651
|
+
it 'recognizes :last options' do
|
652
|
+
comment.send test_method, user, last: 2
|
653
|
+
comment.send(method) if method
|
654
|
+
expect(comment.title).to eq('Test2')
|
655
|
+
end
|
671
656
|
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
657
|
+
if Mongoid::History.mongoid3?
|
658
|
+
context 'protected attributes' do
|
659
|
+
before :each do
|
660
|
+
Comment.attr_accessible(nil)
|
661
|
+
end
|
662
|
+
|
663
|
+
after :each do
|
664
|
+
Comment.attr_protected(nil)
|
665
|
+
end
|
666
|
+
|
667
|
+
it 'should undo last version when no parameter is specified on protected attributes' do
|
668
|
+
comment.send test_method, user
|
669
|
+
comment.send(method) if method
|
670
|
+
expect(comment.title).to eq('Test3')
|
671
|
+
end
|
672
|
+
|
673
|
+
it 'recognizes :last options on model with protected attributes' do
|
674
|
+
comment.send test_method, user, last: 2
|
675
|
+
comment.send(method) if method
|
676
|
+
expect(comment.title).to eq('Test2')
|
677
|
+
end
|
676
678
|
end
|
677
679
|
end
|
678
680
|
end
|
@@ -680,39 +682,39 @@ describe Mongoid::History do
|
|
680
682
|
end
|
681
683
|
end
|
682
684
|
|
683
|
-
describe
|
685
|
+
describe 'redo' do
|
684
686
|
[nil, :reload].each do |method|
|
685
687
|
context "#{method || 'instance'}" do
|
686
688
|
before :each do
|
687
|
-
comment.update_attributes(title:
|
689
|
+
comment.update_attributes(title: 'Test5')
|
688
690
|
end
|
689
691
|
|
690
|
-
it
|
692
|
+
it 'should recognize :from, :to options' do
|
691
693
|
comment.redo! user, from: 2, to: 4
|
692
694
|
comment.send(method) if method
|
693
|
-
comment.title.
|
695
|
+
expect(comment.title).to eq('Test4')
|
694
696
|
end
|
695
697
|
|
696
|
-
it
|
698
|
+
it 'should recognize parameter as version number' do
|
697
699
|
comment.redo! user, 2
|
698
700
|
comment.send(method) if method
|
699
|
-
comment.title.
|
701
|
+
expect(comment.title).to eq('Test2')
|
700
702
|
end
|
701
703
|
|
702
|
-
it
|
704
|
+
it 'should redo last version when no parameter is specified' do
|
703
705
|
comment.redo! user
|
704
706
|
comment.send(method) if method
|
705
|
-
comment.title.
|
707
|
+
expect(comment.title).to eq('Test5')
|
706
708
|
end
|
707
709
|
|
708
|
-
it
|
710
|
+
it 'should recognize :last options' do
|
709
711
|
comment.redo! user, last: 1
|
710
712
|
comment.send(method) if method
|
711
|
-
comment.title.
|
713
|
+
expect(comment.title).to eq('Test5')
|
712
714
|
end
|
713
715
|
|
714
716
|
if Mongoid::History.mongoid3?
|
715
|
-
context
|
717
|
+
context 'protected attributes' do
|
716
718
|
before :each do
|
717
719
|
Comment.attr_accessible(nil)
|
718
720
|
end
|
@@ -721,16 +723,16 @@ describe Mongoid::History do
|
|
721
723
|
Comment.attr_protected(nil)
|
722
724
|
end
|
723
725
|
|
724
|
-
it
|
726
|
+
it 'should recognize parameter as version number' do
|
725
727
|
comment.redo! user, 2
|
726
728
|
comment.send(method) if method
|
727
|
-
comment.title.
|
729
|
+
expect(comment.title).to eq('Test2')
|
728
730
|
end
|
729
731
|
|
730
|
-
it
|
732
|
+
it 'should recognize :from, :to options' do
|
731
733
|
comment.redo! user, from: 2, to: 4
|
732
734
|
comment.send(method) if method
|
733
|
-
comment.title.
|
735
|
+
expect(comment.title).to eq('Test4')
|
734
736
|
end
|
735
737
|
end
|
736
738
|
end
|
@@ -740,7 +742,7 @@ describe Mongoid::History do
|
|
740
742
|
end
|
741
743
|
end
|
742
744
|
|
743
|
-
describe
|
745
|
+
describe 'localized fields' do
|
744
746
|
before :each do
|
745
747
|
class Sausage
|
746
748
|
include Mongoid::Document
|
@@ -750,66 +752,66 @@ describe Mongoid::History do
|
|
750
752
|
track_history on: [:flavour], track_destroy: true
|
751
753
|
end
|
752
754
|
end
|
753
|
-
it
|
755
|
+
it 'should correctly undo and redo' do
|
754
756
|
if Sausage.respond_to?(:localized_fields)
|
755
|
-
sausage = Sausage.create(flavour_translations: { 'en' =>
|
756
|
-
sausage.update_attributes(flavour:
|
757
|
+
sausage = Sausage.create(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' })
|
758
|
+
sausage.update_attributes(flavour: 'Guinness')
|
757
759
|
|
758
760
|
track = sausage.history_tracks.last
|
759
761
|
|
760
762
|
track.undo! user
|
761
|
-
sausage.reload.flavour.
|
763
|
+
expect(sausage.reload.flavour).to eq('Apple')
|
762
764
|
|
763
765
|
track.redo! user
|
764
|
-
sausage.reload.flavour.
|
766
|
+
expect(sausage.reload.flavour).to eq('Guinness')
|
765
767
|
|
766
768
|
sausage.destroy
|
767
|
-
sausage.history_tracks.last.action.
|
769
|
+
expect(sausage.history_tracks.last.action).to eq('destroy')
|
768
770
|
sausage.history_tracks.last.undo! user
|
769
|
-
sausage.reload.flavour.
|
771
|
+
expect(sausage.reload.flavour).to eq('Guinness')
|
770
772
|
end
|
771
773
|
end
|
772
774
|
end
|
773
775
|
|
774
|
-
describe
|
776
|
+
describe 'embedded with a polymorphic trackable' do
|
775
777
|
let(:foo) { Foo.new(title: 'a title', body: 'a body') }
|
776
778
|
before :each do
|
777
779
|
post.comments << foo
|
778
780
|
post.save
|
779
781
|
end
|
780
|
-
it
|
782
|
+
it 'should assign interface name in association chain' do
|
781
783
|
foo.update_attribute(:body, 'a changed body')
|
782
|
-
expected_root = {
|
783
|
-
expected_node = {
|
784
|
-
foo.history_tracks.first.association_chain.
|
784
|
+
expected_root = { 'name' => 'Post', 'id' => post.id }
|
785
|
+
expected_node = { 'name' => 'coms', 'id' => foo.id }
|
786
|
+
expect(foo.history_tracks.first.association_chain).to eq([expected_root, expected_node])
|
785
787
|
end
|
786
788
|
end
|
787
789
|
|
788
|
-
describe
|
789
|
-
context
|
790
|
-
it
|
791
|
-
tag.history_tracks.first.trackable_parent_class.
|
790
|
+
describe '#trackable_parent_class' do
|
791
|
+
context 'a non-embedded model' do
|
792
|
+
it 'should return the trackable parent class' do
|
793
|
+
expect(tag.history_tracks.first.trackable_parent_class).to eq(Tag)
|
792
794
|
end
|
793
|
-
it
|
795
|
+
it 'should return the parent class even if the trackable is deleted' do
|
794
796
|
tracker = tag.history_tracks.first
|
795
797
|
tag.destroy
|
796
|
-
tracker.trackable_parent_class.
|
798
|
+
expect(tracker.trackable_parent_class).to eq(Tag)
|
797
799
|
end
|
798
800
|
end
|
799
|
-
context
|
800
|
-
it
|
801
|
-
comment.update_attributes(title:
|
802
|
-
comment.history_tracks.first.trackable_parent_class.
|
801
|
+
context 'an embedded model' do
|
802
|
+
it 'should return the trackable parent class' do
|
803
|
+
comment.update_attributes(title: 'Foo')
|
804
|
+
expect(comment.history_tracks.first.trackable_parent_class).to eq(Post)
|
803
805
|
end
|
804
|
-
it
|
806
|
+
it 'should return the parent class even if the trackable is deleted' do
|
805
807
|
tracker = comment.history_tracks.first
|
806
808
|
comment.destroy
|
807
|
-
tracker.trackable_parent_class.
|
809
|
+
expect(tracker.trackable_parent_class).to eq(Post)
|
808
810
|
end
|
809
811
|
end
|
810
812
|
end
|
811
813
|
|
812
|
-
describe
|
814
|
+
describe 'when default scope is present' do
|
813
815
|
before do
|
814
816
|
class Post
|
815
817
|
default_scope -> { where(title: nil) }
|
@@ -825,77 +827,77 @@ describe Mongoid::History do
|
|
825
827
|
end
|
826
828
|
end
|
827
829
|
|
828
|
-
describe
|
830
|
+
describe 'post' do
|
829
831
|
|
830
|
-
it
|
832
|
+
it 'should correctly undo and redo' do
|
831
833
|
post.update_attributes(title: 'a new title')
|
832
834
|
track = post.history_tracks.last
|
833
835
|
track.undo! user
|
834
|
-
post.reload.title.
|
836
|
+
expect(post.reload.title).to eq('Test')
|
835
837
|
track.redo! user
|
836
|
-
post.reload.title.
|
838
|
+
expect(post.reload.title).to eq('a new title')
|
837
839
|
end
|
838
840
|
|
839
|
-
it
|
841
|
+
it 'should stay the same after undo and redo' do
|
840
842
|
post.update_attributes(title: 'testing')
|
841
843
|
track = post.history_tracks.last
|
842
844
|
track.undo! user
|
843
845
|
track.redo! user
|
844
|
-
post.reload.title.
|
846
|
+
expect(post.reload.title).to eq('testing')
|
845
847
|
end
|
846
848
|
end
|
847
|
-
describe
|
848
|
-
it
|
849
|
+
describe 'comment' do
|
850
|
+
it 'should correctly undo and redo' do
|
849
851
|
comment.update_attributes(title: 'a new title')
|
850
852
|
track = comment.history_tracks.last
|
851
853
|
track.undo! user
|
852
|
-
comment.reload.title.
|
854
|
+
expect(comment.reload.title).to eq('test')
|
853
855
|
track.redo! user
|
854
|
-
comment.reload.title.
|
856
|
+
expect(comment.reload.title).to eq('a new title')
|
855
857
|
end
|
856
858
|
|
857
|
-
it
|
859
|
+
it 'should stay the same after undo and redo' do
|
858
860
|
comment.update_attributes(title: 'testing')
|
859
861
|
track = comment.history_tracks.last
|
860
862
|
track.undo! user
|
861
863
|
track.redo! user
|
862
|
-
comment.reload.title.
|
864
|
+
expect(comment.reload.title).to eq('testing')
|
863
865
|
end
|
864
866
|
end
|
865
|
-
describe
|
866
|
-
it
|
867
|
+
describe 'user' do
|
868
|
+
it 'should correctly undo and redo' do
|
867
869
|
user.update_attributes(name: 'a new name')
|
868
870
|
track = user.history_tracks.last
|
869
871
|
track.undo! user
|
870
|
-
user.reload.name.
|
872
|
+
expect(user.reload.name).to eq('Aaron')
|
871
873
|
track.redo! user
|
872
|
-
user.reload.name.
|
874
|
+
expect(user.reload.name).to eq('a new name')
|
873
875
|
end
|
874
876
|
|
875
|
-
it
|
877
|
+
it 'should stay the same after undo and redo' do
|
876
878
|
user.update_attributes(name: 'testing')
|
877
879
|
track = user.history_tracks.last
|
878
880
|
track.undo! user
|
879
881
|
track.redo! user
|
880
|
-
user.reload.name.
|
882
|
+
expect(user.reload.name).to eq('testing')
|
881
883
|
end
|
882
884
|
end
|
883
|
-
describe
|
884
|
-
it
|
885
|
+
describe 'tag' do
|
886
|
+
it 'should correctly undo and redo' do
|
885
887
|
tag.update_attributes(title: 'a new title')
|
886
888
|
track = tag.history_tracks.last
|
887
889
|
track.undo! user
|
888
|
-
tag.reload.title.
|
890
|
+
expect(tag.reload.title).to eq('test')
|
889
891
|
track.redo! user
|
890
|
-
tag.reload.title.
|
892
|
+
expect(tag.reload.title).to eq('a new title')
|
891
893
|
end
|
892
894
|
|
893
|
-
it
|
895
|
+
it 'should stay the same after undo and redo' do
|
894
896
|
tag.update_attributes(title: 'testing')
|
895
897
|
track = tag.history_tracks.last
|
896
898
|
track.undo! user
|
897
899
|
track.redo! user
|
898
|
-
tag.reload.title.
|
900
|
+
expect(tag.reload.title).to eq('testing')
|
899
901
|
end
|
900
902
|
end
|
901
903
|
end
|