Class: Audited::Audit

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

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.


116
117
118
119
120
121
# File 'lib/audited/audit.rb', line 116

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

.audited_classesObject

Returns the list of classes that are being audited


109
110
111
# File 'lib/audited/audit.rb', line 109

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


148
149
150
# File 'lib/audited/audit.rb', line 148

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.


59
60
61
# File 'lib/audited/audit.rb', line 59

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


73
74
75
76
77
78
# 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_attributesObject

Returns a hash of the changed attributes with the old values


81
82
83
84
85
86
87
# 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

#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.


65
66
67
68
69
70
# 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(version: version))
  end
end