acts_as_revisionable 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/acts_as_revisionable.gemspec +66 -0
- data/lib/acts_as_revisionable/revision_record.rb +227 -0
- data/lib/acts_as_revisionable.rb +193 -0
- data/spec/acts_as_revisionable_spec.rb +176 -0
- data/spec/full_spec.rb +448 -0
- data/spec/revision_record_spec.rb +428 -0
- data/spec/spec_helper.rb +23 -0
- metadata +141 -0
@@ -0,0 +1,428 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'zlib'
|
3
|
+
|
4
|
+
describe ActsAsRevisionable::RevisionRecord do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
ActsAsRevisionable::Test.create_database
|
8
|
+
end
|
9
|
+
|
10
|
+
after :all do
|
11
|
+
ActsAsRevisionable::Test.delete_database
|
12
|
+
end
|
13
|
+
|
14
|
+
class TestRevisionableRecord
|
15
|
+
attr_accessor :attributes
|
16
|
+
|
17
|
+
def self.base_class
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.inheritance_column
|
22
|
+
'type'
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.store_full_sti_class
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize (attributes = {})
|
30
|
+
@attributes = attributes
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.reflections
|
34
|
+
@reflections || {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.reflections= (vals)
|
38
|
+
@reflections = vals
|
39
|
+
end
|
40
|
+
|
41
|
+
def id
|
42
|
+
attributes['id']
|
43
|
+
end
|
44
|
+
|
45
|
+
def id= (val)
|
46
|
+
attributes['id'] = val
|
47
|
+
end
|
48
|
+
|
49
|
+
def name= (val)
|
50
|
+
attributes['name'] = val
|
51
|
+
end
|
52
|
+
|
53
|
+
def value= (val)
|
54
|
+
attributes['value'] = val
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.revisionable_associations
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.type_name_with_module (type_name)
|
62
|
+
type_name
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class TestRevisionableAssociationRecord < TestRevisionableRecord
|
67
|
+
def self.reflections
|
68
|
+
@reflections || {}
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.reflections= (vals)
|
72
|
+
@reflections = vals
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class TestRevisionableSubAssociationRecord < TestRevisionableRecord
|
77
|
+
def self.reflections
|
78
|
+
@reflections || {}
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.reflections= (vals)
|
82
|
+
@reflections = vals
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
module ActsAsRevisionable
|
87
|
+
class TestModuleRecord < TestRevisionableRecord
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class TestInheritanceRecord < TestRevisionableRecord
|
92
|
+
def self.base_class
|
93
|
+
TestRevisionableRecord
|
94
|
+
end
|
95
|
+
|
96
|
+
def initialize (attributes = {})
|
97
|
+
super({'type' => 'TestInheritanceRecord'}.merge(attributes))
|
98
|
+
end
|
99
|
+
|
100
|
+
def type= (val)
|
101
|
+
attributes['type'] = val
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
before(:each) do
|
106
|
+
TestRevisionableRecord.reflections = nil
|
107
|
+
TestRevisionableAssociationRecord.reflections = nil
|
108
|
+
TestRevisionableSubAssociationRecord.reflections = nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should set the revision number before it creates the record" do
|
112
|
+
ActsAsRevisionable::RevisionRecord.delete_all
|
113
|
+
revision1 = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new('id' => 1))
|
114
|
+
revision1.save!
|
115
|
+
revision2 = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new('id' => 1))
|
116
|
+
revision2.save!
|
117
|
+
revision1.revision.should == 1
|
118
|
+
revision2.revision.should == 2
|
119
|
+
revision2.revision = 20
|
120
|
+
revision2.save!
|
121
|
+
revision3 = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new('id' => 1))
|
122
|
+
revision3.save!
|
123
|
+
revision3.revision.should == 21
|
124
|
+
ActsAsRevisionable::RevisionRecord.delete_all
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should serialize all the attributes of the original model" do
|
128
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
129
|
+
original = TestRevisionableRecord.new(attributes)
|
130
|
+
revision = ActsAsRevisionable::RevisionRecord.new(original)
|
131
|
+
revision.revisionable_id.should == 1
|
132
|
+
revision.revisionable_type.should == "TestRevisionableRecord"
|
133
|
+
revision.revision_attributes.should == attributes
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should serialize all the attributes of revisionable has_many associations" do
|
137
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => Time.now}
|
138
|
+
association_attributes_1 = {'id' => 2, 'name' => 'association_1'}
|
139
|
+
association_attributes_2 = {'id' => 3, 'name' => 'association_2'}
|
140
|
+
original = TestRevisionableRecord.new(attributes)
|
141
|
+
revisionable_associations = [TestRevisionableAssociationRecord.new(association_attributes_1), TestRevisionableAssociationRecord.new(association_attributes_2)]
|
142
|
+
revisionable_associations_reflection = stub(:association, :name => :revisionable_associations, :macro => :has_many, :options => {:dependent => :destroy})
|
143
|
+
non_revisionable_associations_reflection = stub(:association, :name => :non_revisionable_associations, :macro => :has_many, :options => {})
|
144
|
+
|
145
|
+
TestRevisionableRecord.should_receive(:revisionable_associations).and_return(:revisionable_associations => true)
|
146
|
+
TestRevisionableRecord.reflections = {:revisionable_associations => revisionable_associations_reflection, :non_revisionable_associations => non_revisionable_associations_reflection}
|
147
|
+
original.should_not_receive(:non_revisionable_associations)
|
148
|
+
original.should_receive(:revisionable_associations).and_return(revisionable_associations)
|
149
|
+
|
150
|
+
revision = ActsAsRevisionable::RevisionRecord.new(original)
|
151
|
+
revision.revision_attributes.should == attributes.merge('revisionable_associations' => [association_attributes_1, association_attributes_2])
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should serialize all the attributes of revisionable has_one associations" do
|
155
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => Date.today}
|
156
|
+
association_attributes = {'id' => 2, 'name' => 'association_1'}
|
157
|
+
original = TestRevisionableRecord.new(attributes)
|
158
|
+
revisionable_association = TestRevisionableAssociationRecord.new(association_attributes)
|
159
|
+
revisionable_association_reflection = stub(:association, :name => :revisionable_association, :macro => :has_one, :options => {:dependent => :destroy})
|
160
|
+
non_revisionable_association_reflection = stub(:association, :name => :non_revisionable_association, :macro => :has_one, :options => {})
|
161
|
+
|
162
|
+
TestRevisionableRecord.should_receive(:revisionable_associations).and_return(:revisionable_association => true)
|
163
|
+
TestRevisionableRecord.reflections = {:revisionable_association => revisionable_association_reflection, :non_revisionable_association => non_revisionable_association_reflection}
|
164
|
+
original.should_not_receive(:non_revisionable_association)
|
165
|
+
original.should_receive(:revisionable_association).and_return(revisionable_association)
|
166
|
+
|
167
|
+
revision = ActsAsRevisionable::RevisionRecord.new(original)
|
168
|
+
revision.revision_attributes.should == attributes.merge('revisionable_association' => association_attributes)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should serialize all revisionable has_many_and_belongs_to_many associations" do
|
172
|
+
attributes = {'id' => 1, 'name' => 'revision'}
|
173
|
+
original = TestRevisionableRecord.new(attributes)
|
174
|
+
revisionable_associations_reflection = stub(:association, :name => :revisionable_associations, :macro => :has_and_belongs_to_many, :options => {:dependent => :destroy})
|
175
|
+
non_revisionable_associations_reflection = stub(:association, :name => :non_revisionable_associations, :macro => :has_and_belongs_to_many, :options => {})
|
176
|
+
|
177
|
+
TestRevisionableRecord.should_receive(:revisionable_associations).and_return(:revisionable_associations => true)
|
178
|
+
TestRevisionableRecord.reflections = {:revisionable_associations => revisionable_associations_reflection, :non_revisionable_associations => non_revisionable_associations_reflection}
|
179
|
+
original.should_receive(:revisionable_association_ids).and_return([2, 3, 4])
|
180
|
+
|
181
|
+
revision = ActsAsRevisionable::RevisionRecord.new(original)
|
182
|
+
revision.revision_attributes.should == attributes.merge('revisionable_associations' => [2, 3, 4])
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should serialize revisionable associations of revisionable associations with :dependent => :destroy" do
|
186
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => Time.now}
|
187
|
+
association_attributes_1 = {'id' => 2, 'name' => 'association_1'}
|
188
|
+
association_attributes_2 = {'id' => 3, 'name' => 'association_2'}
|
189
|
+
original = TestRevisionableRecord.new(attributes)
|
190
|
+
association_1 = TestRevisionableAssociationRecord.new(association_attributes_1)
|
191
|
+
association_2 = TestRevisionableAssociationRecord.new(association_attributes_2)
|
192
|
+
revisionable_associations = [association_1, association_2]
|
193
|
+
revisionable_associations_reflection = stub(:association, :name => :revisionable_associations, :macro => :has_many, :options => {:dependent => :destroy})
|
194
|
+
sub_association_attributes = {'id' => 4, 'name' => 'sub_association_1'}
|
195
|
+
sub_association = TestRevisionableSubAssociationRecord.new(sub_association_attributes)
|
196
|
+
sub_association_reflection = stub(:sub_association, :name => :sub_association, :macro => :has_one, :options => {:dependent => :destroy})
|
197
|
+
|
198
|
+
TestRevisionableRecord.should_receive(:revisionable_associations).and_return(:revisionable_associations => {:sub_association => true})
|
199
|
+
TestRevisionableRecord.reflections = {:revisionable_associations => revisionable_associations_reflection}
|
200
|
+
TestRevisionableAssociationRecord.reflections = {:sub_association => sub_association_reflection}
|
201
|
+
original.should_receive(:revisionable_associations).and_return(revisionable_associations)
|
202
|
+
association_1.should_receive(:sub_association).and_return(sub_association)
|
203
|
+
association_2.should_receive(:sub_association).and_return(nil)
|
204
|
+
|
205
|
+
revision = ActsAsRevisionable::RevisionRecord.new(original)
|
206
|
+
revision.revision_attributes.should == attributes.merge('revisionable_associations' => [association_attributes_1.merge('sub_association' => sub_association_attributes), association_attributes_2.merge('sub_association' => nil)])
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should be able to restore the original model using Ruby serialization" do
|
210
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
211
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(attributes), :ruby)
|
212
|
+
revision.data = Zlib::Deflate.deflate(Marshal.dump(attributes))
|
213
|
+
restored = revision.restore
|
214
|
+
restored.class.should == TestRevisionableRecord
|
215
|
+
restored.id.should == 1
|
216
|
+
restored.attributes.should == attributes
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should be able to restore the original model using YAML serialization" do
|
220
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
221
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(attributes), :yaml)
|
222
|
+
revision.data = Zlib::Deflate.deflate(YAML.dump(attributes))
|
223
|
+
restored = revision.restore
|
224
|
+
restored.class.should == TestRevisionableRecord
|
225
|
+
restored.id.should == 1
|
226
|
+
restored.attributes.should == attributes
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should be able to restore the original model using XML serialization" do
|
230
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
231
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(attributes), :xml)
|
232
|
+
revision.data = Zlib::Deflate.deflate(YAML.dump(attributes))
|
233
|
+
restored = revision.restore
|
234
|
+
restored.class.should == TestRevisionableRecord
|
235
|
+
restored.id.should == 1
|
236
|
+
restored.attributes.should == attributes
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should be able to restore associations" do
|
240
|
+
restored = TestRevisionableRecord.new
|
241
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => Time.now, :associations => {'id' => 2, 'value' => 'val'}}
|
242
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
243
|
+
revision.data = Zlib::Deflate.deflate(Marshal.dump(attributes))
|
244
|
+
associations_reflection = stub(:associations, :name => :associations, :macro => :has_many, :options => {:dependent => :destroy})
|
245
|
+
TestRevisionableRecord.reflections = {:associations => associations_reflection}
|
246
|
+
TestRevisionableRecord.should_receive(:new).and_return(restored)
|
247
|
+
revision.should_receive(:restore_association).with(restored, :associations, {'id' => 2, 'value' => 'val'})
|
248
|
+
restored = revision.restore
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should be able to restore the has_many associations" do
|
252
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
253
|
+
record = TestRevisionableRecord.new
|
254
|
+
|
255
|
+
associations_reflection = stub(:associations, :name => :associations, :macro => :has_many, :options => {:dependent => :destroy})
|
256
|
+
TestRevisionableRecord.reflections = {:associations => associations_reflection}
|
257
|
+
associations = mock(:associations)
|
258
|
+
record.should_receive(:associations).and_return(associations)
|
259
|
+
associated_record = TestRevisionableAssociationRecord.new
|
260
|
+
associations.should_receive(:build).and_return(associated_record)
|
261
|
+
|
262
|
+
revision.send(:restore_association, record, :associations, {'id' => 1, 'value' => 'val'})
|
263
|
+
associated_record.id.should == 1
|
264
|
+
associated_record.attributes.should == {'id' => 1, 'value' => 'val'}
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should be able to restore the has_one associations" do
|
268
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
269
|
+
record = TestRevisionableRecord.new
|
270
|
+
|
271
|
+
association_reflection = stub(:associations, :name => :association, :macro => :has_one, :klass => TestRevisionableAssociationRecord, :options => {:dependent => :destroy})
|
272
|
+
TestRevisionableRecord.reflections = {:association => association_reflection}
|
273
|
+
associated_record = TestRevisionableAssociationRecord.new
|
274
|
+
TestRevisionableAssociationRecord.should_receive(:new).and_return(associated_record)
|
275
|
+
record.should_receive(:association=).with(associated_record)
|
276
|
+
|
277
|
+
revision.send(:restore_association, record, :association, {'id' => 1, 'value' => 'val'})
|
278
|
+
associated_record.id.should == 1
|
279
|
+
associated_record.attributes.should == {'id' => 1, 'value' => 'val'}
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should be able to restore the has_and_belongs_to_many associations" do
|
283
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
284
|
+
record = TestRevisionableRecord.new
|
285
|
+
|
286
|
+
associations_reflection = stub(:associations, :name => :associations, :macro => :has_and_belongs_to_many, :options => {})
|
287
|
+
TestRevisionableRecord.reflections = {:associations => associations_reflection}
|
288
|
+
record.should_receive(:association_ids=).with([2, 3, 4])
|
289
|
+
|
290
|
+
revision.send(:restore_association, record, :associations, [2, 3, 4])
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should be able to restore associations of associations" do
|
294
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
295
|
+
record = TestRevisionableRecord.new
|
296
|
+
|
297
|
+
associations_reflection = stub(:associations, :name => :associations, :macro => :has_many, :options => {:dependent => :destroy})
|
298
|
+
TestRevisionableRecord.reflections = {:associations => associations_reflection}
|
299
|
+
associations = mock(:associations)
|
300
|
+
record.should_receive(:associations).and_return(associations)
|
301
|
+
associated_record = TestRevisionableAssociationRecord.new
|
302
|
+
associations.should_receive(:build).and_return(associated_record)
|
303
|
+
|
304
|
+
sub_associated_record = TestRevisionableSubAssociationRecord.new
|
305
|
+
TestRevisionableAssociationRecord.should_receive(:new).and_return(sub_associated_record)
|
306
|
+
sub_association_reflection = stub(:sub_association, :name => :sub_association, :macro => :has_one, :klass => TestRevisionableAssociationRecord, :options => {:dependent => :destroy})
|
307
|
+
TestRevisionableAssociationRecord.reflections = {:sub_association => sub_association_reflection}
|
308
|
+
associated_record.should_receive(:sub_association=).with(sub_associated_record)
|
309
|
+
|
310
|
+
revision.send(:restore_association, record, :associations, {'id' => 1, 'value' => 'val', :sub_association => {'id' => 2, 'value' => 'sub'}})
|
311
|
+
associated_record.id.should == 1
|
312
|
+
associated_record.attributes.should == {'id' => 1, 'value' => 'val'}
|
313
|
+
sub_associated_record.id.should == 2
|
314
|
+
sub_associated_record.attributes.should == {'id' => 2, 'value' => 'sub'}
|
315
|
+
end
|
316
|
+
|
317
|
+
it "should be able to restore a record for a model that has changed and add errors to the restored record" do
|
318
|
+
restored = TestRevisionableRecord.new
|
319
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => Time.now, 'deleted_attribute' => 'abc', :bad_association => {'id' => 3, 'value' => :val}, :associations => {'id' => 2, 'value' => 'val', 'other' => 'val2'}}
|
320
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new)
|
321
|
+
revision.data = Zlib::Deflate.deflate(Marshal.dump(attributes))
|
322
|
+
TestRevisionableRecord.should_receive(:new).and_return(restored)
|
323
|
+
|
324
|
+
associations = mock(:associations)
|
325
|
+
restored.should_receive(:associations).and_return(associations)
|
326
|
+
associated_record = TestRevisionableAssociationRecord.new
|
327
|
+
associations.should_receive(:build).and_return(associated_record)
|
328
|
+
|
329
|
+
mock_record_errors = {}
|
330
|
+
restored.stub!(:errors).and_return(mock_record_errors)
|
331
|
+
mock_record_errors.should_receive(:add).with(:bad_association, "could not be restored to {\"id\"=>3, \"value\"=>:val}")
|
332
|
+
mock_record_errors.should_receive(:add).with(:deleted_attribute, 'could not be restored to "abc"')
|
333
|
+
mock_record_errors.should_receive(:add).with(:associations, 'could not be restored from the revision')
|
334
|
+
|
335
|
+
mock_association_errors = mock(:errors)
|
336
|
+
associated_record.stub!(:errors).and_return(mock_association_errors)
|
337
|
+
mock_association_errors.should_receive(:add).with(:other, 'could not be restored to "val2"')
|
338
|
+
|
339
|
+
associations_reflection = stub(:associations, :name => :associations, :macro => :has_many, :options => {:dependent => :destroy})
|
340
|
+
TestRevisionableRecord.reflections = {:associations => associations_reflection}
|
341
|
+
|
342
|
+
restored = revision.restore
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should be able to truncate the revisions for a record" do
|
346
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(:name => 'name'))
|
347
|
+
revision.revision = 20
|
348
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:find).with(:first, :conditions => ['revisionable_type = ? AND revisionable_id = ?', 'TestRevisionableRecord', 1], :offset => 15, :order => 'revision DESC').and_return(revision)
|
349
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:delete_all).with(['revisionable_type = ? AND revisionable_id = ? AND revision <= ?', 'TestRevisionableRecord', 1, 20])
|
350
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => 15)
|
351
|
+
end
|
352
|
+
|
353
|
+
it "should be able to truncate the revisions for a record by age" do
|
354
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(:name => 'name'))
|
355
|
+
revision.revision = 20
|
356
|
+
time = 2.weeks.ago
|
357
|
+
minimum_age = stub(:integer, :ago => time, :to_i => 1)
|
358
|
+
Time.stub!(:now).and_return(minimum_age)
|
359
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:find).with(:first, :conditions => ['revisionable_type = ? AND revisionable_id = ? AND created_at <= ?', 'TestRevisionableRecord', 1, time], :offset => nil, :order => 'revision DESC').and_return(revision)
|
360
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:delete_all).with(['revisionable_type = ? AND revisionable_id = ? AND revision <= ?', 'TestRevisionableRecord', 1, 20])
|
361
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :minimum_age => minimum_age)
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should not truncate the revisions for a record if it doesn't have enough" do
|
365
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:find).with(:first, :conditions => ['revisionable_type = ? AND revisionable_id = ?', 'TestRevisionableRecord', 1], :offset => 15, :order => 'revision DESC').and_return(nil)
|
366
|
+
ActsAsRevisionable::RevisionRecord.should_not_receive(:delete_all)
|
367
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => 15)
|
368
|
+
end
|
369
|
+
|
370
|
+
it "should not truncate the revisions for a record if no limit or minimum_age is set" do
|
371
|
+
ActsAsRevisionable::RevisionRecord.should_not_receive(:find)
|
372
|
+
ActsAsRevisionable::RevisionRecord.should_not_receive(:delete_all)
|
373
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => nil, :minimum_age => nil)
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should be able to find a record by revisioned type and id" do
|
377
|
+
revision = ActsAsRevisionable::RevisionRecord.new(TestRevisionableRecord.new(:name => 'name'))
|
378
|
+
ActsAsRevisionable::RevisionRecord.should_receive(:find).with(:first, :conditions => {:revisionable_type => 'TestRevisionableRecord', :revisionable_id => 1, :revision => 2}).and_return(revision)
|
379
|
+
ActsAsRevisionable::RevisionRecord.find_revision(TestRevisionableRecord, 1, 2).should == revision
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should handle module namespaces" do
|
383
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
384
|
+
revision = ActsAsRevisionable::RevisionRecord.new(ActsAsRevisionable::TestModuleRecord.new(attributes))
|
385
|
+
revision.data = Zlib::Deflate.deflate(Marshal.dump(attributes))
|
386
|
+
restored = revision.restore
|
387
|
+
restored.class.should == ActsAsRevisionable::TestModuleRecord
|
388
|
+
end
|
389
|
+
|
390
|
+
it "should handle single table inheritance" do
|
391
|
+
attributes = {'id' => 1, 'name' => 'revision', 'value' => 5}
|
392
|
+
record = TestInheritanceRecord.new(attributes)
|
393
|
+
revision = ActsAsRevisionable::RevisionRecord.new(record)
|
394
|
+
revision.data = Zlib::Deflate.deflate(Marshal.dump(record.attributes))
|
395
|
+
restored = revision.restore
|
396
|
+
restored.class.should == TestInheritanceRecord
|
397
|
+
end
|
398
|
+
|
399
|
+
it "should really save the revision records to the database and restore without any mocking" do
|
400
|
+
ActsAsRevisionable::RevisionRecord.delete_all
|
401
|
+
ActsAsRevisionable::RevisionRecord.count.should == 0
|
402
|
+
|
403
|
+
attributes = {'id' => 1, 'value' => rand(1000000)}
|
404
|
+
original = TestRevisionableRecord.new(attributes)
|
405
|
+
original.attributes['name'] = 'revision 1'
|
406
|
+
ActsAsRevisionable::RevisionRecord.new(original).save!
|
407
|
+
first_revision = ActsAsRevisionable::RevisionRecord.find(:first)
|
408
|
+
original.attributes['name'] = 'revision 2'
|
409
|
+
ActsAsRevisionable::RevisionRecord.new(original).save!
|
410
|
+
original.attributes['name'] = 'revision 3'
|
411
|
+
ActsAsRevisionable::RevisionRecord.new(original).save!
|
412
|
+
ActsAsRevisionable::RevisionRecord.count.should == 3
|
413
|
+
|
414
|
+
record = ActsAsRevisionable::RevisionRecord.find_revision(TestRevisionableRecord, 1, 1).restore
|
415
|
+
record.class.should == TestRevisionableRecord
|
416
|
+
record.id.should == 1
|
417
|
+
record.attributes.should == attributes.merge('name' => 'revision 1')
|
418
|
+
|
419
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => 2)
|
420
|
+
ActsAsRevisionable::RevisionRecord.count.should == 2
|
421
|
+
ActsAsRevisionable::RevisionRecord.find_by_id(first_revision.id).should == nil
|
422
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => 0, :minimum_age => 1.week)
|
423
|
+
ActsAsRevisionable::RevisionRecord.count.should == 2
|
424
|
+
ActsAsRevisionable::RevisionRecord.truncate_revisions(TestRevisionableRecord, 1, :limit => 0)
|
425
|
+
ActsAsRevisionable::RevisionRecord.count.should == 0
|
426
|
+
end
|
427
|
+
|
428
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.expand_path('../../lib/acts_as_revisionable', __FILE__)
|
3
|
+
require 'sqlite3'
|
4
|
+
|
5
|
+
module ActsAsRevisionable
|
6
|
+
module Test
|
7
|
+
def self.create_database
|
8
|
+
db_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp'))
|
9
|
+
Dir.mkdir(db_dir) unless File.exist?(db_dir)
|
10
|
+
db = File.join(db_dir, 'test.sqlite3')
|
11
|
+
ActiveRecord::Base.establish_connection("adapter" => "sqlite3", "database" => db)
|
12
|
+
ActsAsRevisionable::RevisionRecord.create_table
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.delete_database
|
16
|
+
db_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp'))
|
17
|
+
db = File.join(db_dir, 'test.sqlite3')
|
18
|
+
ActiveRecord::Base.connection.disconnect!
|
19
|
+
File.delete(db) if File.exist?(db)
|
20
|
+
Dir.delete(db_dir) if File.exist?(db_dir) and Dir.entries(db_dir).reject{|f| f.match(/^\.+$/)}.empty?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_revisionable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brian Durand
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-22 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 2
|
33
|
+
version: "2.2"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 27
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 3
|
62
|
+
- 0
|
63
|
+
version: 1.3.0
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: jeweler
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
description: ActiveRecord extension that provides revision support so that history can be tracked and changes can be reverted. Emphasis for this plugin versus similar ones is including associations, saving on storage, and extensibility of the model.
|
81
|
+
email: brian@embellishedvisions.com
|
82
|
+
executables: []
|
83
|
+
|
84
|
+
extensions: []
|
85
|
+
|
86
|
+
extra_rdoc_files:
|
87
|
+
- README.rdoc
|
88
|
+
files:
|
89
|
+
- .gitignore
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.rdoc
|
92
|
+
- Rakefile
|
93
|
+
- VERSION
|
94
|
+
- acts_as_revisionable.gemspec
|
95
|
+
- lib/acts_as_revisionable.rb
|
96
|
+
- lib/acts_as_revisionable/revision_record.rb
|
97
|
+
- spec/acts_as_revisionable_spec.rb
|
98
|
+
- spec/full_spec.rb
|
99
|
+
- spec/revision_record_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: http://github.com/bdurand/acts_as_revisionable
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options:
|
107
|
+
- --charset=UTF-8
|
108
|
+
- --main
|
109
|
+
- README.rdoc
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 3
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
requirements: []
|
131
|
+
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.3.7
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: ActiveRecord extension that provides revision support so that history can be tracked and changes can be reverted.
|
137
|
+
test_files:
|
138
|
+
- spec/acts_as_revisionable_spec.rb
|
139
|
+
- spec/full_spec.rb
|
140
|
+
- spec/revision_record_spec.rb
|
141
|
+
- spec/spec_helper.rb
|