public
Description: ActiveRecord plugin for versioning your models.
Clone URL: git://github.com/technoweenie/acts_as_versioned.git
added tests and rdocs

git-svn-id: http://svn.techno-weenie.net/projects/acts_as_versioned@77 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sat Sep 17 11:15:49 -0700 2005
commit  d27c6c6f636cfa7d8c84336cca5ca369ec79b728
tree    2bc601134dcfd0d41af1971a006f7f6fbb8e75ac
parent  2bd26b98d1dcc235aaadb48a4110ac411e1878b2
...
 
 
 
 
1
2
3
4
...
1
2
3
4
5
6
7
8
0
@@ -1,3 +1,7 @@
0
+*0.1.1*
0
+
0
+* Adding tests and rdocs
0
+
0
 *0.1*
0
 
0
 * Initial transfer from Rails ticket: http://dev.rubyonrails.com/ticket/1974
0
\ No newline at end of file
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
 
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
55
56
57
...
60
61
62
63
 
64
65
 
66
67
 
68
69
70
71
72
73
 
 
 
 
 
74
75
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
78
79
...
262
263
264
 
 
 
 
265
266
267
 
 
268
269
270
 
 
 
271
272
 
273
274
275
 
 
 
276
277
278
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
22
23
 
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
57
58
59
...
62
63
64
 
65
66
 
67
68
 
69
70
 
 
 
 
 
71
72
73
74
75
76
77
 
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
...
288
289
290
291
292
293
294
295
 
 
296
297
298
299
 
300
301
302
303
304
305
306
307
 
308
309
310
311
312
313
0
@@ -1,57 +1,59 @@
0
-#Copyright (c) 2005 Rick Olson
0
-#
0
-#Permission is hereby granted, free of charge, to any person obtaining
0
-#a copy of this software and associated documentation files (the
0
-#"Software"), to deal in the Software without restriction, including
0
-#without limitation the rights to use, copy, modify, merge, publish,
0
-#distribute, sublicense, and/or sell copies of the Software, and to
0
-#permit persons to whom the Software is furnished to do so, subject to
0
-#the following conditions:
0
-#
0
-#The above copyright notice and this permission notice shall be
0
-#included in all copies or substantial portions of the Software.
0
-#
0
-#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
-#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
-#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
-#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
-#LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
-#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
-#WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
+# Copyright (c) 2005 Rick Olson
0
+#
0
+# Permission is hereby granted, free of charge, to any person obtaining
0
+# a copy of this software and associated documentation files (the
0
+# "Software"), to deal in the Software without restriction, including
0
+# without limitation the rights to use, copy, modify, merge, publish,
0
+# distribute, sublicense, and/or sell copies of the Software, and to
0
+# permit persons to whom the Software is furnished to do so, subject to
0
+# the following conditions:
0
+#
0
+# The above copyright notice and this permission notice shall be
0
+# included in all copies or substantial portions of the Software.
0
+#
0
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
 
0
-module ActiveRecord
0
+module ActiveRecord #:nodoc:
0
   module Acts #:nodoc:
0
- module Versioned #:nodoc:
0
+ # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
0
+ # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version
0
+ # column is present as well.
0
+ #
0
+ # class Page < ActiveRecord::Base
0
+ # # assumes pages_versions table
0
+ # acts_as_versioned
0
+ # end
0
+ #
0
+ # Example:
0
+ #
0
+ # page = Page.create(:title => 'hello world!')
0
+ # page.version # => 1
0
+ #
0
+ # page.title = 'hello world'
0
+ # page.save
0
+ # page.version # => 2
0
+ # page.versions.size # => 2
0
+ #
0
+ # page.revert_to(1) # using version number
0
+ # page.title # => 'hello world!'
0
+ #
0
+ # page.revert_to(page.versions.last) # using versioned instance
0
+ # page.title # => 'hello world'
0
+ #
0
+ # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
0
+ module Versioned
0
       def self.included(base) # :nodoc:
0
         base.extend ClassMethods
0
       end
0
 
0
- # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
0
- # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version
0
- # column is present as well.
0
- #
0
- # class Page < ActiveRecord::Base
0
- # # assumes pages_versions table
0
- # acts_as_versioned
0
- # end
0
- #
0
- # Example:
0
- #
0
- # page = Page.create(:title => 'hello world!')
0
- # page.version # => 1
0
- #
0
- # page.title = 'hello world'
0
- # page.save
0
- # page.version # => 2
0
- # page.versions.size # => 2
0
- #
0
- # page.revert_to(1) # using version number
0
- # page.title # => 'hello world!'
0
- #
0
- # page.revert_to(page.versions.last) # using versioned instance
0
- # page.title # => 'hello world'
0
       module ClassMethods
0
- # Configuration options are:
0
+ # == Configuration options
0
         #
0
         # * <tt>class_name</tt> - versioned model class name (default: PageVersion in the above example)
0
         # * <tt>table_name</tt> - versioned model table name (default: page_versions in the above example)
0
@@ -60,20 +62,44 @@ module ActiveRecord
0
         # * <tt>version_column</tt> - name of the column in the model that keeps the version number (default: version)
0
         # * <tt>limit</tt> - number of revisions to keep, defaults to unlimited
0
         # * <tt>if</tt> - symbol of method to check before saving a new version. If this method returns false, a new version is not saved.
0
- # For finer control, pass either a Proc or modify Model#version_condition_met?
0
+ # For finer control, pass either a Proc or modify Model#version_condition_met?
0
         #
0
- # acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
0
+ # acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
0
         #
0
- # or...
0
+ # or...
0
         #
0
- # class Auction
0
- # def version_condition_met? # totally bypasses the <tt>:if</tt> option
0
- # !expired?
0
- # end
0
- # end
0
+ # class Auction
0
+ # def version_condition_met? # totally bypasses the <tt>:if</tt> option
0
+ # !expired?
0
+ # end
0
+ # end
0
         #
0
         # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
0
- # either a symbol or array of symbols.
0
+ # either a symbol or array of symbols.
0
+ #
0
+ # == Database Schema
0
+ #
0
+ # The model that you're versioning needs to have a 'version' attribute. The model is versioned
0
+ # into a table called #{model}_versions where the model name is singlular. The _versions table should
0
+ # contain all the fields you want versioned, the same version column, and a #{model}_id foreign key field.
0
+ #
0
+ # A lock_version field is also accepted if your model uses Optimistic Locking. If your table uses Single Table inheritance,
0
+ # then that field is reflected in the versioned model as 'versioned_type' by default.
0
+ #
0
+ # Acts_as_versioned comes prepared with the ActiveRecord::Acts::Versioned::ActMethods::ClassMethods#create_versioned_table
0
+ # method, perfect for a migration. It will also create the version column if the main model does not already have it.
0
+ #
0
+ # class AddVersions < ActiveRecord::Migration
0
+ # def self.up
0
+ # # create_versioned_table takes the same options hash
0
+ # # that create_table does
0
+ # Post.create_versioned_table
0
+ # end
0
+ #
0
+ # def self.down
0
+ # Post.drop_versioned_table
0
+ # end
0
+ # end
0
         def acts_as_versioned(options = {})
0
           # don't allow multiple calls
0
           return if defined?(self.versioned_class_name)
0
@@ -262,17 +288,26 @@ module ActiveRecord
0
         unless defined?(ACTS_AS_VERSIONED_CALLBACKS)
0
           ACTS_AS_VERSIONED_CALLBACKS = [:set_new_version, :save_version_on_create, :save_version, :clear_changed_attributes]
0
         end
0
+
0
+ ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name|
0
+ alias_method "orig_#{attr_name}".to_sym, attr_name
0
+ end
0
         
0
- ACTS_AS_VERSIONED_CALLBACKS.each { |attr_name| alias_method "orig_#{attr_name}".to_sym, attr_name }
0
- def empty_callback() end
0
+ def empty_callback() end #:nodoc:
0
+
0
         def enable_acts_as_versioned_callbacks
0
           self.class.class_eval do
0
- ACTS_AS_VERSIONED_CALLBACKS.each { |attr_name| alias_method attr_name, "orig_#{attr_name}".to_sym }
0
+ ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name|
0
+ alias_method attr_name, "orig_#{attr_name}".to_sym
0
+ end
0
           end
0
         end
0
+
0
         def disable_acts_as_versioned_callbacks
0
           self.class.class_eval do
0
- ACTS_AS_VERSIONED_CALLBACKS.each { |attr_name| alias_method attr_name, :empty_callback }
0
+ ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name|
0
+ alias_method attr_name, :empty_callback
0
+ end
0
           end
0
         end
0
 
...
1
2
 
 
3
...
 
1
2
3
4
0
@@ -1 +1,2 @@
0
-# coming soon
0
\ No newline at end of file
0
+$:.unshift "../lib"
0
+Dir["**/*_test.rb"].each { |f| load f }
0
\ No newline at end of file

Comments

    No one has commented yet.