Class: Audit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_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

  • changes: a serialized hash of all the changes

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



29
30
31
32
33
34
35
# File 'lib/acts_as_audited/audit.rb', line 29

def self.as_user(user, &block)
  Thread.current[:acts_as_audited_user] = user

  yield

  Thread.current[:acts_as_audited_user] = nil
end

.assign_revision_attributes(record, attributes) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/acts_as_audited/audit.rb', line 92

def self.assign_revision_attributes(record, attributes)
  attributes.each do |attr, val|
    if record.respond_to?("#{attr}=")
      record.attributes.has_key?(attr.to_s) ?
        record[attr] = val :
        record.send("#{attr}=", val)
    end
  end
  record
end

.audited_classesObject



22
23
24
# File 'lib/acts_as_audited/audit.rb', line 22

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

.reconstruct_attributes(audits) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/acts_as_audited/audit.rb', line 83

def self.reconstruct_attributes(audits)
  attributes = {}
  result = audits.collect do |audit|
    attributes.merge!(audit.new_attributes).merge!(:audit_version => audit.audit_version)
    yield attributes if block_given?
  end
  block_given? ? result : attributes
end

Instance Method Details

#ancestorsObject



61
62
63
64
65
# File 'lib/acts_as_audited/audit.rb', line 61

def ancestors
  self.class.find(:all, :order => 'audit_version',
    :conditions => ['auditable_id = ? and auditable_type = ? and audit_version <= ?',
    auditable_id, auditable_type, audit_version])
end

#new_attributesObject

Returns a hash of the changed attributes with the new values



68
69
70
71
72
73
# File 'lib/acts_as_audited/audit.rb', line 68

def new_attributes
  (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



76
77
78
79
80
81
# File 'lib/acts_as_audited/audit.rb', line 76

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

#revisionObject



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

def revision
  clazz = auditable_type.constantize
  returning clazz.find_by_id(auditable_id) || clazz.new do |m|
    Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:audit_version => audit_version}))
  end
end

#user_as_stringObject Also known as: user

:nodoc:



48
49
50
# File 'lib/acts_as_audited/audit.rb', line 48

def user_as_string #:nodoc:
  self.user_as_model || self.username
end

#user_as_string=(user) ⇒ Object Also known as: user=

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



38
39
40
41
42
43
44
# File 'lib/acts_as_audited/audit.rb', line 38

def user_as_string=(user) #:nodoc:
  # 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