Module: CIA::Auditable::ClassMethods

Defined in:
lib/cia/auditable.rb

Instance Method Summary collapse

Instance Method Details

#audit_attribute(*attributes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cia/auditable.rb', line 22

def audit_attribute(*attributes)
  options = (attributes.last.is_a?(Hash) ? attributes.pop : {})
  setup_auditing_callbacks(options)

  self.audited_attributes = attributes.map(&:to_s)

  raise "cannot have :if and :unless" if options[:if] && options[:unless]
  self.audited_attribute_options = options

  has_many :cia_events, :class_name => "CIA::Event", :as => :source
  has_many :cia_attribute_changes, :class_name => "CIA::AttributeChange", :as => :source
end

#setup_auditing_callbacks(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cia/auditable.rb', line 35

def setup_auditing_callbacks(options)
  return if audited_attributes_callbacks_added
  self.audited_attributes_callbacks_added = true

  [:create, :update, :destroy].each do |callback|
    method, args = if options[:callback] == :after_commit
      send("after_#{callback}"){ |record| record.stored_cia_changes(record.cia_changes) }
      if ActiveRecord::VERSION::MAJOR == 2
        ["after_commit_on_#{callback}", []]
      else # rails 3+
        [:after_commit, [{:on => callback}]]
      end
    else
      ["after_#{callback}", []]
    end
    send(method, *args) { |record| CIA.record(callback, record) }
  end
end