class Audit

Audit saves the changes to ActiveRecord models. It has the following attributes:

Public Class Methods

as_user(user) { || ... } click to toggle source

All audits made during the block called will be recorded as made by user. This method is hopefully threadsafe, making it ideal for background operations that require audit information.

# File lib/acts_as_audited/audit.rb, line 38
def as_user(user, &block)
  Thread.current[:acts_as_audited_user] = user

  yieldval = yield

  Thread.current[:acts_as_audited_user] = nil

  yieldval
end
assign_revision_attributes(record, attributes) click to toggle source

@private

# File lib/acts_as_audited/audit.rb, line 59
def assign_revision_attributes(record, attributes)
  attributes.each do |attr, val|
    record = record.dup if record.frozen?

    if record.respond_to?("#{attr}=")
      record.attributes.has_key?(attr.to_s) ?
        record[attr] = val :
        record.send("#{attr}=", val)
    end
  end
  record
end
audited_classes() click to toggle source

Returns the list of classes that are being audited

# File lib/acts_as_audited/audit.rb, line 31
def audited_classes
  audited_class_names.map(&:constantize)
end
reconstruct_attributes(audits) { |attributes| ... } click to toggle source

@private

# File lib/acts_as_audited/audit.rb, line 49
def reconstruct_attributes(audits)
  attributes = {}
  result = audits.collect do |audit|
    attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
    yield attributes if block_given?
  end
  block_given? ? result : attributes
end

Public Instance Methods

ancestors() click to toggle source

Return all audits older than the current one.

# File lib/acts_as_audited/audit.rb, line 103
def ancestors
  self.class.where(['auditable_id = ? and auditable_type = ? and version <= ?',
    auditable_id, auditable_type, version])
end
new_attributes() click to toggle source

Returns a hash of the changed attributes with the new values

# File lib/acts_as_audited/audit.rb, line 109
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
old_attributes() click to toggle source

Returns a hash of the changed attributes with the old values

# File lib/acts_as_audited/audit.rb, line 117
def old_attributes
  (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
    attrs[attr] = Array(values).first
    attrs
  end
end
revision() click to toggle source

Return an instance of what the object looked like at this revision. If the object has been destroyed, this will be a new record.

# File lib/acts_as_audited/audit.rb, line 95
def revision
  clazz = auditable_type.constantize
  ( clazz.find_by_id(auditable_id) || clazz.new ).tap do |m|
    Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version}))
  end
end
user() click to toggle source
Also aliased as: user_as_model
Alias for: user_as_string
user=(user) click to toggle source
Also aliased as: user_as_model=
Alias for: user_as_string=
user_as_model() click to toggle source
Alias for: user
user_as_model=(user) click to toggle source
Alias for: user=
user_as_string() click to toggle source

@private

# File lib/acts_as_audited/audit.rb, line 87
def user_as_string
  self.user_as_model || self.username
end
Also aliased as: user
user_as_string=(user) click to toggle source

Allows user to be set to either a string or an ActiveRecord object @private

# File lib/acts_as_audited/audit.rb, line 76
def user_as_string=(user)
  # reset both either way
  self.user_as_model = self.username = nil
  user.is_a?(ActiveRecord::Base) ?
    self.user_as_model = user :
    self.username = user
end
Also aliased as: user=