0
@@ -177,15 +177,6 @@ module ActiveRecord #:nodoc:
0
:version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
0
:version_association_options, :version_if_changed
0
- alias_method :non_versioned_fields, :non_versioned_columns
0
- alias_method :non_versioned_fields=, :non_versioned_columns=
0
- alias_method :non_versioned_fields, :non_versioned_columns
0
- alias_method :non_versioned_fields=, :non_versioned_columns=
0
self.versioned_class_name = options[:class_name] || "Version"
0
self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
0
self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
0
@@ -280,11 +271,6 @@ module ActiveRecord #:nodoc:
0
base.extend ClassMethods
0
- # Finds a specific version of this record
0
- def find_version(version = nil)
0
- self.class.find_version(id, version)
0
# Saves a version of the model if applicable
0
save_version_on_create if save_version?
0
@@ -293,9 +279,9 @@ module ActiveRecord #:nodoc:
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
-
self.clone_versioned_model(self, rev)
0
+
clone_versioned_model(self, rev)
0
rev.version = send(self.class.version_column)
0
- rev.send("#{self.class.versioned_foreign_key}=",
self.id)
0
+ rev.send("#{self.class.versioned_foreign_key}=",
id)
0
@@ -305,24 +291,19 @@ module ActiveRecord #:nodoc:
0
return if self.class.max_version_limit == 0
0
excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
0
- sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
0
- self.class.versioned_class.connection.execute sql
0
+ self.class.versioned_class.delete_all ["version <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
0
# Reverts a model to a given version. Takes either a version number or an instance of the versioned model
0
if version.is_a?(self.class.versioned_class)
0
- return false unless version.send(self.class.versioned_foreign_key) ==
self.id and !version.new_record?
0
+ return false unless version.send(self.class.versioned_foreign_key) ==
id and !version.new_record?
0
return false unless version = versions.find_by_version(version)
0
self.clone_versioned_model(version, self)
0
- se
lf.send("#{self.class.version_column}=", version.version)
0
+ se
nd("#{self.class.version_column}=", version.version)
0
@@ -347,20 +328,15 @@ module ActiveRecord #:nodoc:
0
- # Returns an array of attribute keys that are versioned. See non_versioned_columns
0
- def versioned_attributes
0
- self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
0
-
self.track_altered_attributes ? (self.version_if_changed.map(&:to_s) - changed).length == 0 : changed?
0
+
track_altered_attributes ? (version_if_changed.map(&:to_s) - changed).length == 0 : changed?
0
# Clones a model. Used when saving a new version or reverting a model's version.
0
def clone_versioned_model(orig_model, new_model)
0
- self.versioned_attributes.each do |key|
0
- new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key)
0
+ self.class.versioned_columns.each do |col|
0
+ new_model.send("#{col.name}=", orig_model.send(col.name)) if orig_model.has_attribute?(col.name)
0
if orig_model.is_a?(self.class.versioned_class)
0
@@ -413,40 +389,18 @@ module ActiveRecord #:nodoc:
0
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
0
- self.send("#{self.class.version_column}=",
self.next_version) if new_record? || (!locking_enabled? && save_version?)
0
+ self.send("#{self.class.version_column}=",
next_version) if new_record? || (!locking_enabled? && save_version?)
0
# Gets the next available version for the current record, or 1 for a new record
0
- return 1 if new_record?
0
- (versions.calculate(:max, :version) || 0) + 1
0
+ (new_record? ? 0 : versions.calculate(:max, :version).to_i) + 1
0
- # Finds a specific version of a specific row of this model
0
- def find_version(id, version = nil)
0
- return find(id) unless version
0
- conditions = ["#{versioned_foreign_key} = ? AND version = ?", id, version]
0
- options = { :conditions => conditions, :limit => 1 }
0
- if result = find_versions(id, options).first
0
- raise RecordNotFound, "Couldn't find #{name} with ID=#{id} and VERSION=#{version}"
0
- # Finds versions of a specific model. Takes an options hash like <tt>find</tt>
0
- def find_versions(id, options = {})
0
- versioned_class.find :all, {
0
- :conditions => ["#{versioned_foreign_key} = ?", id],
0
- :order => 'version' }.merge(options)
0
# Returns an array of columns that are versioned. See non_versioned_columns
0
-
self.columns.select { |c| !non_versioned_columns.include?(c.name) }
0
+
@versioned_columns ||= columns.select { |c| !non_versioned_columns.include?(c.name) }
0
# Returns an instance of the dynamic versioned model
0
@@ -470,17 +424,17 @@ module ActiveRecord #:nodoc:
0
self.versioned_columns.each do |col|
0
updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
0
self.connection.add_column versioned_table_name, col.name, col.type,
0
- :default => col.default,
0
+ :default => col.default,
0
:precision => col.precision
0
if type_col = self.columns_hash[inheritance_column]
0
self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
0
- :limit => type_col.limit,
0
- :default => type_col.default,
0
- :scale => type_col.scale,
0
+ :limit => type_col.limit,
0
+ :default => type_col.default,
0
+ :scale => type_col.scale,
0
:precision => type_col.precision
Comments
No one has commented yet.