class Audited::Audit

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/audited/audit.rb, line 133
def self.as_user(user)
  last_audited_user = ::Audited.store[:audited_user] 
  ::Audited.store[:audited_user] = user
  yield
ensure
  ::Audited.store[:audited_user] = last_audited_user
end
assign_revision_attributes(record, attributes) click to toggle source

@private

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

    if record.respond_to?("#{attr}=")
      record.attributes.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/audited/audit.rb, line 126
def self.audited_classes
  audited_class_names.map(&:constantize)
end
collection_cache_key(collection = all, *) click to toggle source

use created_at as timestamp cache key

Calls superclass method
# File lib/audited/audit.rb, line 164
def self.collection_cache_key(collection = all, *)
  super(collection, :created_at)
end
reconstruct_attributes(audits) click to toggle source

@private

# File lib/audited/audit.rb, line 142
def self.reconstruct_attributes(audits)
  audits.each_with_object({}) do |audit, all|
    all.merge!(audit.new_attributes)
    all[:audit_version] = audit.version
 end
end

Public Instance Methods

ancestors() click to toggle source

Return all audits older than the current one.

# File lib/audited/audit.rb, line 59
def ancestors
  self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version)
end
new_attributes() click to toggle source

Returns a hash of the changed attributes with the new values

# File lib/audited/audit.rb, line 73
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/audited/audit.rb, line 81
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/audited/audit.rb, line 65
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(audit_version: version))
  end
end
undo() click to toggle source

Allows user to undo changes

# File lib/audited/audit.rb, line 90
def undo
  case action
  when 'create'
    # destroys a newly created record
    auditable.destroy!
  when 'destroy'
    # creates a new record with the destroyed record attributes
    auditable_type.constantize.create!(audited_changes)
  when 'update'
    # changes back attributes
    auditable.update!(audited_changes.transform_values(&:first))
  else
    raise StandardError, "invalid action given #{action}"
  end
end
user()
Also aliased as: user_as_model
Alias for: user_as_string
user=(user)
Also aliased as: user_as_model=
Alias for: user_as_string=
user_as_model()
Alias for: user
user_as_model=(user)
Alias for: user=
user_as_string() click to toggle source

@private

# File lib/audited/audit.rb, line 119
def user_as_string
  user_as_model || 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/audited/audit.rb, line 108
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=

Private Instance Methods

set_audit_user() click to toggle source
# File lib/audited/audit.rb, line 175
def set_audit_user
  self.user ||= ::Audited.store[:audited_user] # from .as_user
  self.user ||= ::Audited.store[:current_user].try!(:call) # from Sweeper
  nil # prevent stopping callback chains
end
set_remote_address() click to toggle source
# File lib/audited/audit.rb, line 186
def set_remote_address
  self.remote_address ||= ::Audited.store[:current_remote_address]
end
set_request_uuid() click to toggle source
# File lib/audited/audit.rb, line 181
def set_request_uuid
  self.request_uuid ||= ::Audited.store[:current_request_uuid]
  self.request_uuid ||= SecureRandom.uuid
end
set_version_number() click to toggle source
# File lib/audited/audit.rb, line 170
def set_version_number
  max = self.class.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0
  self.version = max + 1
end