aboutsummaryrefslogtreecommitdiffstats
path: root/lib/acts_as_versioned.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/acts_as_versioned.rb')
-rw-r--r--lib/acts_as_versioned.rb44
1 files changed, 23 insertions, 21 deletions
diff --git a/lib/acts_as_versioned.rb b/lib/acts_as_versioned.rb
index 5299e0dc7..efc44b795 100644
--- a/lib/acts_as_versioned.rb
+++ b/lib/acts_as_versioned.rb
@@ -95,12 +95,7 @@ module ActiveRecord #:nodoc:
# end
#
# * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
- # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
- # Use this instead if you want to write your own attribute setters (and ignore if_changed):
- #
- # def name=(new_name)
- # write_changed_attribute :name, new_name
- # end
+ # either a symbol or array of symbols.
#
# * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block
# to create an anonymous mixin:
@@ -185,7 +180,7 @@ module ActiveRecord #:nodoc:
self.version_sequence_name = options[:sequence_name]
self.max_version_limit = options[:limit].to_i
self.version_condition = options[:if] || true
- self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column, 'created_at', 'created_on']
+ self.non_versioned_columns = [self.primary_key, inheritance_column, self.version_column, 'lock_version', versioned_inheritance_column, 'created_at', 'created_on'] + options[:non_versioned_columns].to_a.map(&:to_s)
self.version_association_options = {
:class_name => "#{self.to_s}::#{versioned_class_name}",
:foreign_key => versioned_foreign_key,
@@ -201,16 +196,16 @@ module ActiveRecord #:nodoc:
options[:extend] = self.const_get(extension_module_name)
end
- class_eval do
+ class_eval <<-CLASS_METHODS
has_many :versions, version_association_options do
# finds earliest version of this record
def earliest
- @earliest ||= find(:first, :order => 'version')
+ @earliest ||= find(:first, :order => '#{version_column}')
end
# find latest version of this record
def latest
- @latest ||= find(:first, :order => 'version desc')
+ @latest ||= find(:first, :order => '#{version_column} desc')
end
end
before_save :set_new_version
@@ -220,11 +215,11 @@ module ActiveRecord #:nodoc:
unless options[:if_changed].nil?
self.track_altered_attributes = true
options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
- self.version_if_changed = options[:if_changed]
+ self.version_if_changed = options[:if_changed].map(&:to_s)
end
include options[:extend] if options[:extend].is_a?(Module)
- end
+ CLASS_METHODS
# create the dynamic versioned model
const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
@@ -262,6 +257,8 @@ module ActiveRecord #:nodoc:
:foreign_key => versioned_foreign_key
versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module)
versioned_class.set_sequence_name version_sequence_name if version_sequence_name
+
+ create_versioned_table
end
end
@@ -276,7 +273,7 @@ module ActiveRecord #:nodoc:
@saving_version = nil
rev = self.class.versioned_class.new
clone_versioned_model(self, rev)
- rev.version = send(self.class.version_column)
+ rev.send("#{self.class.version_column}=", send(self.class.version_column))
rev.send("#{self.class.versioned_foreign_key}=", id)
rev.save
end
@@ -288,7 +285,7 @@ module ActiveRecord #:nodoc:
return if self.class.max_version_limit == 0
excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
if excess_baggage > 0
- self.class.versioned_class.delete_all ["version <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
+ self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
end
end
@@ -297,10 +294,10 @@ module ActiveRecord #:nodoc:
if version.is_a?(self.class.versioned_class)
return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record?
else
- return false unless version = versions.find_by_version(version)
+ return false unless version = versions.send("find_by_#{self.class.version_column}", version)
end
self.clone_versioned_model(version, self)
- send("#{self.class.version_column}=", version.version)
+ send("#{self.class.version_column}=", version.send(self.class.version_column))
true
end
@@ -327,7 +324,7 @@ module ActiveRecord #:nodoc:
end
def altered?
- track_altered_attributes ? (version_if_changed.map(&:to_s) - changed).length < version_if_changed.length : changed?
+ track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
end
# Clones a model. Used when saving a new version or reverting a model's version.
@@ -392,7 +389,7 @@ module ActiveRecord #:nodoc:
# Gets the next available version for the current record, or 1 for a new record
def next_version
- (new_record? ? 0 : versions.calculate(:max, :version).to_i) + 1
+ (new_record? ? 0 : versions.calculate(:max, version_column).to_i) + 1
end
module ClassMethods
@@ -409,13 +406,16 @@ module ActiveRecord #:nodoc:
# Rake migration task to create the versioned table using options passed to acts_as_versioned
def create_versioned_table(create_table_options = {})
# create version column in main table if it does not exist
- if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
- self.connection.add_column table_name, :version, :integer
+ if !self.content_columns.find { |c| [version_column.to_s, 'lock_version'].include? c.name }
+ self.connection.add_column table_name, version_column, :integer
+ self.reset_column_information
end
+ return if connection.tables.include?(versioned_table_name)
+
self.connection.create_table(versioned_table_name, create_table_options) do |t|
t.column versioned_foreign_key, :integer
- t.column :version, :integer
+ t.column version_column, :integer
end
updated_col = nil
@@ -439,6 +439,8 @@ module ActiveRecord #:nodoc:
if updated_col.nil?
self.connection.add_column versioned_table_name, :updated_at, :timestamp
end
+
+ self.connection.create_index versioned_table_name, versioned_foreign_key
end
# Rake migration task to drop the versioned table