Skip to content

Commit

Permalink
made it slightly easier to modify non versioned attributes
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/plugins/acts_as_versioned@926 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Mar 4, 2006
1 parent 1b07009 commit 86728f1
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/acts_as_versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,30 @@ module ClassMethods
# end
# end
#
# == Changing What Fields Are Versioned
#
# By default, acts_as_versioned will version all but these fields:
#
# [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
#
# You can add or change those by modifying #non_versioned_fields
#
# class Post < ActiveRecord::Base
# acts_as_versioned
# self.non_versioned_fields << 'comments_count'
# end
#
def acts_as_versioned(options = {}, &extension)
# don't allow multiple calls
return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)

send :include, ActiveRecord::Acts::Versioned::ActMethods

cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
:version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name
send :attr_accessor, :changed_attributes
:version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_fields

send :attr_accessor, :changed_attributes

self.versioned_class_name = options[:class_name] || "Version"
self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{Inflector.underscore(Inflector.demodulize(class_name_of_active_record_descendant(self)))}_versions#{table_name_suffix}"
Expand All @@ -155,6 +170,7 @@ def acts_as_versioned(options = {}, &extension)
self.version_sequence_name = options[:sequence_name]
self.max_version_limit = options[:limit].to_i
self.version_condition = options[:if] || true
self.non_versioned_fields = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]

if block_given?
extension_module_name = "#{self.to_s}#{versioned_class_name}Extension"
Expand Down Expand Up @@ -384,7 +400,7 @@ def find_versions(id, options = {})
:conditions => ["#{versioned_foreign_key} = ?", id],
:order => 'version' }.merge(options)
end

# Returns an array of columns that are versioned. See non_versioned_fields
def versioned_columns
self.columns.select { |c| !non_versioned_fields.include?(c.name) }
Expand All @@ -394,12 +410,7 @@ def versioned_columns
def versioned_class
"#{self.to_s}::#{versioned_class_name}".constantize
end

# An array of fields that are not saved in the versioned table
def non_versioned_fields
[self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
end


# 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
Expand Down

0 comments on commit 86728f1

Please sign in to comment.