Class: Audited::Audit

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::Observing
Defined in:
lib/audited/audit.rb

Overview

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

  • auditable: the ActiveRecord model that was changed

  • user: the user that performed the change; a string or an ActiveRecord model

  • action: one of create, update, or delete

  • audited_changes: a serialized hash of all the changes

  • comment: a comment set with the audit

  • version: the version of the model

  • request_uuid: a uuid based that allows audits from the same controller request

  • created_at: Time that the change was performed

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_user(user, &block) ⇒ Object

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.



97
98
99
100
101
102
# File 'lib/audited/audit.rb', line 97

def self.as_user(user, &block)
  Thread.current[:audited_user] = user
  yield
ensure
  Thread.current[:audited_user] = nil
end

.audited_classesObject

Returns the list of classes that are being audited



90
91
92
# File 'lib/audited/audit.rb', line 90

def self.audited_classes
  audited_class_names.map(&:constantize)
end

.collection_cache_key(collection = all, timestamp_column = :created_at) ⇒ Object

use created_at as timestamp cache key



129
130
131
# File 'lib/audited/audit.rb', line 129

def self.collection_cache_key(collection = all, timestamp_column = :created_at)
  super(collection, :created_at)
end

Instance Method Details

#ancestorsObject

Return all audits older than the current one.



40
41
42
# File 'lib/audited/audit.rb', line 40

def ancestors
  self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version)
end

#new_attributesObject

Returns a hash of the changed attributes with the new values



54
55
56
57
58
59
# File 'lib/audited/audit.rb', line 54

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_attributesObject

Returns a hash of the changed attributes with the old values



62
63
64
65
66
67
68
# File 'lib/audited/audit.rb', line 62

def old_attributes
  (audited_changes || {}).inject({}.with_indifferent_access) do |attrs, (attr, values)|
    attrs[attr] = Array(values).first

    attrs
  end
end

#revisionObject

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



46
47
48
49
50
51
# File 'lib/audited/audit.rb', line 46

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