module Audited::Audit

def self.included(klass)

def self.included(klass)
  klass.extend(ClassMethods)
  klass.setup_audit
end

def new_attributes

Returns a hash of the changed attributes with the new values
def new_attributes
  (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
    attrs[attr] = values.is_a?(Array) ? values.last : values
    attrs
  end
end

def old_attributes

Returns a hash of the changed attributes with the old values
def old_attributes
  (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
    attrs[attr] = Array(values).first
    attrs
  end
end

def revision

the object has been destroyed, this will be a new record.
Return an instance of what the object looked like at this revision. If
def revision
  clazz = auditable_type.constantize
  (clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
    self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({ :version => version }))
  end
end

def set_audit_user

def set_audit_user
  self.user = Thread.current[:audited_user] if Thread.current[:audited_user]
  nil # prevent stopping callback chains
end

def set_version_number

def set_version_number
  max = self.class.where(
    :auditable_id => auditable_id,
    :auditable_type => auditable_type
  ).order(:version.desc).first.try(:version) || 0
  self.version = max + 1
end