class ActiveAudit::Audit

Public Class Methods

create(attributes, options={}) click to toggle source
# File lib/active_audit/audit.rb, line 70
def self.create(attributes, options={})
  object = self.new(attributes)
  object.save(options)
  object
end
deserialize(attributes) click to toggle source
# File lib/active_audit/audit.rb, line 80
def self.deserialize attributes
  self.new attributes.update(recorded_at: Time.at(attributes[:recorded_at]))
end
new(*args) click to toggle source
Calls superclass method
# File lib/active_audit/audit.rb, line 23
def initialize *args
  attributes = args.extract_options!
  if attributes.empty?
    if args.count == 2
      initialize_from_record(*args)
      run_callbacks :initialize
    else
      raise ArgumentError, "You need to supply at least one attribute"
    end
  else
    super attributes
  end
end
serialize(audit) click to toggle source
# File lib/active_audit/audit.rb, line 76
def self.serialize audit
  audit.attributes.select {|k,v| v.present?}.merge(recorded_at: audit.recorded_at.to_i)
end

Public Instance Methods

changed?() click to toggle source
# File lib/active_audit/audit.rb, line 58
def changed?
  ['create', 'destroy'].include?(self.event) || changes.present?
end
save(options={}) { || ... } click to toggle source
# File lib/active_audit/audit.rb, line 62
def save options={}
  self.run_callbacks :save do
    self.run_callbacks :create do
      yield if changed?
    end
  end
end

Private Instance Methods

initialize_from_record(event, record) click to toggle source
# File lib/active_audit/audit.rb, line 37
        def initialize_from_record event, record
  if event == 'update'
    if record.respond_to?(:aasm) && record.aasm.current_event
      event = record.aasm.current_event.to_s.sub(/!$/, '')
    end
    self.changes = record.previous_changes.select { |key, value| record.class.auditing_options[:only].include? key }
    self.changes.merge!(record.association_previous_changes.select { |key, value| record.class.auditing_options[:associations].include? key })
    self.changes = self.changes.map do |key, values|
      if values.any? { |v| v.is_a?(Time) }
        [key, values.map { |x| x && x.utc.iso8601 }]
      else
        [key, values]
      end
    end.to_h
  end
  self.event = event
  self.type = record.class.auditing_options[:type]
  self.item_id = record.id
  set_default_attributes
end