Skip to content
Browse files

move callbacks and has_many :versions to included block for clarity

  • Loading branch information...
1 parent d82262f commit 46bd0b73ac653e216153d5a6505006fb4c245dcd @gvarela gvarela committed
Showing with 14 additions and 16 deletions.
  1. +14 −16 lib/acts_as_versioned.rb
View
30 lib/acts_as_versioned.rb
@@ -163,8 +163,6 @@ def acts_as_versioned(options = {}, &extension)
# don't allow multiple calls
return if self.included_modules.include?(ActiveRecord::Acts::Versioned::Behaviors)
- include ActiveRecord::Acts::Versioned::Behaviors
-
cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
:version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
:version_association_options, :version_if_changed
@@ -193,23 +191,19 @@ def acts_as_versioned(options = {}, &extension)
options[:extend] = self.const_get(extension_module_name)
end
- class_eval do
- has_many :versions, version_association_options
+ 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].map(&:to_s)
+ end
- before_save :set_new_version
- after_save :save_version
- after_save :clear_old_versions
+ include options[:extend] if options[:extend].is_a?(Module)
- 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].map(&:to_s)
- end
-
- include options[:extend] if options[:extend].is_a?(Module)
- end
+ include ActiveRecord::Acts::Versioned::Behaviors
- # create the dynamic versioned model
+ #
+ # Create the dynamic versioned model
+ #
const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
def self.reloadable?;
false;
@@ -266,7 +260,11 @@ module Behaviors
extend ActiveSupport::Concern
included do
+ has_many :versions, self.version_association_options
+ before_save :set_new_version
+ after_save :save_version
+ after_save :clear_old_versions
end
module InstanceMethods

0 comments on commit 46bd0b7

Please sign in to comment.
Something went wrong with that request. Please try again.