Module: ChangeLogger::ActsAsChangeLogger::ClassMethods

Defined in:
lib/change_logger/acts_as_change_logger.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_change_logger(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/change_logger/acts_as_change_logger.rb', line 13

def acts_as_change_logger(options = {})
  send :include, InstanceMethods
  
  cattr_accessor :ignore, :track_templates
  self.ignore = (options[:ignore] || []).map &:to_s
  self.ignore.push('id', 'revision', 'created_at', 'updated_at')
  self.track_templates = (options[:track_templates] || []).map &:to_s
  
  attr_accessor :template_changed
  after_update :record_template_change
  
  has_many :change_logs, :as => :item, :order => 'change_logs.created_at desc'
  after_create :record_object_creation
  before_update :increment_revision
  after_update :record_attribute_updates
  before_destroy :record_object_destruction
  self.reflect_on_all_associations(:has_and_belongs_to_many).each do |reflection|
    if reflection.options.keys.include?(:after_add) || reflection.options.keys.include?(:after_remove)
      logger.warn { "WARNING: change_logger adds after_add and after_remove options to has_and_belongs_to_many relationships. You need to combine your current methods with the record_association_* methods in order for change_logger to work correctly." }
    end
    new_options = { :after_add => :record_association_add, :after_remove => :record_association_remove }.merge(reflection.options)
    has_and_belongs_to_many reflection.name.to_sym, new_options
  end
end