public
Description: ActiveRecord plugin for versioning your models.
Clone URL: git://github.com/technoweenie/acts_as_versioned.git
favor after_save callbacks, its easier to force tasks to run before or 
after after_save callbacks
technoweenie (author)
Wed Jun 25 02:29:18 -0700 2008
commit  c7e812f2b94096558c442c0b399bc8ade346fede
tree    c541f2f1f598de3e6b180df420411f7b6960db73
parent  50e62820ce669b34014dc5215f0761b4b04ec1ed
...
66
67
68
69
 
70
71
72
...
214
215
216
217
218
 
219
220
221
...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 
 
 
 
 
 
 
 
 
286
287
288
...
389
390
391
 
392
393
394
...
66
67
68
 
69
70
71
72
...
214
215
216
 
 
217
218
219
220
...
270
271
272
 
 
 
 
 
273
 
 
 
 
 
 
274
275
276
277
278
279
280
281
282
283
284
285
...
386
387
388
389
390
391
392
0
@@ -66,7 +66,7 @@ module ActiveRecord #:nodoc:
0
     #
0
     # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
0
     module Versioned
0
- CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?]
0
+ CALLBACKS = [:set_new_version, :save_version, :save_version?]
0
       def self.included(base) # :nodoc:
0
         base.extend ClassMethods
0
       end
0
@@ -214,8 +214,7 @@ module ActiveRecord #:nodoc:
0
               end
0
             end
0
             before_save :set_new_version
0
- after_create :save_version_on_create
0
- after_update :save_version
0
+ after_save :save_version
0
             after_save :clear_old_versions
0
 
0
             unless options[:if_changed].nil?
0
@@ -271,18 +270,16 @@ module ActiveRecord #:nodoc:
0
           base.extend ClassMethods
0
         end
0
 
0
- # Saves a version of the model if applicable
0
- def save_version
0
- save_version_on_create if save_version?
0
- end
0
-
0
         # Saves a version of the model in the versioned table. This is called in the after_save callback by default
0
- def save_version_on_create
0
- rev = self.class.versioned_class.new
0
- clone_versioned_model(self, rev)
0
- rev.version = send(self.class.version_column)
0
- rev.send("#{self.class.versioned_foreign_key}=", id)
0
- rev.save
0
+ def save_version
0
+ if @is_new_record || save_version?
0
+ @is_new_record = nil
0
+ rev = self.class.versioned_class.new
0
+ clone_versioned_model(self, rev)
0
+ rev.version = send(self.class.version_column)
0
+ rev.send("#{self.class.versioned_foreign_key}=", id)
0
+ rev.save
0
+ end
0
         end
0
 
0
         # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
0
@@ -389,6 +386,7 @@ module ActiveRecord #:nodoc:
0
         protected
0
           # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
0
           def set_new_version
0
+ @is_new_record = new_record?
0
             self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?)
0
           end
0
 
...
18
19
20
 
 
21
22
23
...
36
37
38
 
 
39
40
41
...
51
52
53
 
 
54
55
56
...
67
68
69
 
 
70
...
18
19
20
21
22
23
24
25
...
38
39
40
41
42
43
44
45
...
55
56
57
58
59
60
61
62
...
73
74
75
76
77
78
0
@@ -18,6 +18,8 @@ ActiveRecord::Schema.define(:version => 0) do
0
     t.column :revisor_id, :integer
0
   end
0
   
0
+ add_index :page_versions, [:page_id, :version], :unique => true
0
+
0
   create_table :authors, :force => true do |t|
0
     t.column :page_id, :integer
0
     t.column :name, :string
0
@@ -36,6 +38,8 @@ ActiveRecord::Schema.define(:version => 0) do
0
     t.column :version_type, :string, :limit => 255
0
     t.column :updated_at, :datetime
0
   end
0
+
0
+ add_index :locked_pages_revisions, [:page_id, :version], :unique => true
0
 
0
   create_table :widgets, :force => true do |t|
0
     t.column :name, :string, :limit => 50
0
@@ -51,6 +55,8 @@ ActiveRecord::Schema.define(:version => 0) do
0
     t.column :updated_at, :datetime
0
   end
0
   
0
+ add_index :widget_versions, [:widget_id, :version], :unique => true
0
+
0
   create_table :landmarks, :force => true do |t|
0
     t.column :name, :string
0
     t.column :latitude, :float
0
@@ -67,4 +73,6 @@ ActiveRecord::Schema.define(:version => 0) do
0
     t.column :doesnt_trigger_version,:string
0
     t.column :version, :integer
0
   end
0
+
0
+ add_index :landmark_versions, [:landmark_id, :version], :unique => true
0
 end
...
147
148
149
150
 
151
152
153
154
 
155
156
157
158
 
159
160
161
...
168
169
170
171
 
172
173
174
175
 
176
177
178
179
 
180
181
182
...
322
323
324
325
 
326
327
 
328
329
330
331
332
333
334
335
 
 
 
336
337
338
339
340
341
 
342
343
344
345
346
347
...
147
148
149
 
150
151
152
153
 
154
155
156
157
 
158
159
160
161
...
168
169
170
 
171
172
173
174
 
175
176
177
178
 
179
180
181
182
...
322
323
324
 
325
326
 
327
328
329
330
331
332
 
 
 
333
334
335
336
337
338
339
340
 
341
342
343
344
 
345
346
0
@@ -147,15 +147,15 @@ class VersionedTest < Test::Unit::TestCase
0
 
0
     p = Page.create! :title => "title"
0
     assert_equal 1, p.version # version does not increment
0
- assert_equal 1, p.versions(true).size
0
+ assert_equal 1, p.versions.count
0
 
0
     p.update_attributes(:title => 'new title')
0
     assert_equal 1, p.version # version does not increment
0
- assert_equal 1, p.versions(true).size
0
+ assert_equal 1, p.versions.count
0
 
0
     p.update_attributes(:title => 'a title')
0
     assert_equal 2, p.version
0
- assert_equal 2, p.versions(true).size
0
+ assert_equal 2, p.versions.count
0
 
0
     # reset original if condition
0
     Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
0
@@ -168,15 +168,15 @@ class VersionedTest < Test::Unit::TestCase
0
 
0
     p = Page.create! :title => "title"
0
     assert_equal 1, p.version # version does not increment
0
- assert_equal 1, p.versions(true).size
0
+ assert_equal 1, p.versions.count
0
 
0
     p.update_attributes(:title => 'a title')
0
     assert_equal 1, p.version # version does not increment
0
- assert_equal 1, p.versions(true).size
0
+ assert_equal 1, p.versions.count
0
 
0
     p.update_attributes(:title => 'b title')
0
     assert_equal 2, p.version
0
- assert_equal 2, p.versions(true).size
0
+ assert_equal 2, p.versions.count
0
 
0
     # reset original if condition
0
     Page.version_condition = old_condition
0
@@ -322,25 +322,24 @@ class VersionedTest < Test::Unit::TestCase
0
   def test_should_find_version_count
0
     assert_equal 2, pages(:welcome).versions.size
0
   end
0
-
0
+
0
   def test_if_changed_creates_version_if_a_listed_column_is_changed
0
- landmarks(:washington).name="Washington"
0
+ landmarks(:washington).name = "Washington"
0
     assert landmarks(:washington).changed?
0
     assert landmarks(:washington).altered?
0
   end
0
 
0
   def test_if_changed_creates_version_if_all_listed_columns_are_changed
0
- landmarks(:washington).name="Washington"
0
- landmarks(:washington).latitude=1.0
0
- landmarks(:washington).longitude=1.0
0
+ landmarks(:washington).name = "Washington"
0
+ landmarks(:washington).latitude = 1.0
0
+ landmarks(:washington).longitude = 1.0
0
     assert landmarks(:washington).changed?
0
     assert landmarks(:washington).altered?
0
   end
0
 
0
   def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed
0
- landmarks(:washington).doesnt_trigger_version="This should not trigger version"
0
+ landmarks(:washington).doesnt_trigger_version = "This should not trigger version"
0
     assert landmarks(:washington).changed?
0
     assert !landmarks(:washington).altered?
0
   end
0
-
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.