0
-#Copyright (c) 2005 Rick Olson
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
-#The above copyright notice and this permission notice shall be
0
-#included in all copies or substantial portions of the Software.
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
+# 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
+# The above copyright notice and this permission notice shall be
0
+# included in all copies or substantial portions of the Software.
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
+module ActiveRecord
#: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
+ # class Page < ActiveRecord::Base
0
+ # # assumes pages_versions table
0
+ # page = Page.create(:title => 'hello world!')
0
+ # page.title = 'hello world'
0
+ # page.versions.size # => 2
0
+ # page.revert_to(1) # using version number
0
+ # page.title # => 'hello world!'
0
+ # page.revert_to(page.versions.last) # using versioned instance
0
+ # page.title # => 'hello world'
0
+ # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
0
def self.included(base) # :nodoc:
0
base.extend ClassMethods
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
- # class Page < ActiveRecord::Base
0
- # # assumes pages_versions table
0
- # page = Page.create(:title => 'hello world!')
0
- # page.title = 'hello world'
0
- # page.versions.size # => 2
0
- # page.revert_to(1) # using version number
0
- # page.title # => 'hello world!'
0
- # page.revert_to(page.versions.last) # using versioned instance
0
- # page.title # => 'hello world'
0
- #
Configuration options are:0
+ #
== Configuration options0
# * <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
- #
acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
0
+ #
acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
0
- # def version_condition_met? # totally bypasses the <tt>:if</tt> option
0
+ # def version_condition_met? # totally bypasses the <tt>:if</tt> option
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
+ # 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
+ # 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
+ # 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
+ # class AddVersions < ActiveRecord::Migration
0
+ # # create_versioned_table takes the same options hash
0
+ # # that create_table does
0
+ # Post.create_versioned_table
0
+ # Post.drop_versioned_table
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
+ ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name|
0
+ alias_method "orig_#{attr_name}".to_sym, attr_name
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
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
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
Comments
No one has commented yet.