Class Audit
In: lib/acts_as_audited/audit.rb
Parent: ActiveRecord::Base

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
  • audit_changes: a serialized hash of all the changes
  • created_at: Time that the change was performed

Methods

External Aliases

user= -> user_as_model=
user_as_string= -> user=
user -> user_as_model
user_as_string -> user

Public Class methods

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.

[Source]

    # File lib/acts_as_audited/audit.rb, line 32
32:   def self.as_user(user, &block)
33:     Thread.current[:acts_as_audited_user] = user
34: 
35:     yield
36: 
37:     Thread.current[:acts_as_audited_user] = nil
38:   end

[Source]

     # File lib/acts_as_audited/audit.rb, line 95
 95:   def self.assign_revision_attributes(record, attributes)
 96:     attributes.each do |attr, val|
 97:       if record.respond_to?("#{attr}=")
 98:         record.attributes.has_key?(attr.to_s) ?
 99:           record[attr] = val :
100:           record.send("#{attr}=", val)
101:       end
102:     end
103:     record
104:   end

[Source]

    # File lib/acts_as_audited/audit.rb, line 22
22:   def self.audited_classes
23:     self.audited_class_names.map(&:constantize)
24:   end

[Source]

    # File lib/acts_as_audited/audit.rb, line 86
86:   def self.reconstruct_attributes(audits)
87:     attributes = {}
88:     result = audits.collect do |audit|
89:       attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
90:       yield attributes if block_given?
91:     end
92:     block_given? ? result : attributes
93:   end

Public Instance methods

[Source]

    # File lib/acts_as_audited/audit.rb, line 64
64:   def ancestors
65:     self.class.find(:all, :order => 'version',
66:       :conditions => ['auditable_id = ? and auditable_type = ? and version <= ?',
67:       auditable_id, auditable_type, version])
68:   end

Returns a hash of the changed attributes with the new values

[Source]

    # File lib/acts_as_audited/audit.rb, line 71
71:   def new_attributes
72:     (audit_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
73:       attrs[attr] = Array(values).last
74:       attrs
75:     end
76:   end

Returns a hash of the changed attributes with the old values

[Source]

    # File lib/acts_as_audited/audit.rb, line 79
79:   def old_attributes
80:     (audit_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
81:       attrs[attr] = Array(values).first
82:       attrs
83:     end
84:   end

[Source]

    # File lib/acts_as_audited/audit.rb, line 57
57:   def revision
58:     clazz = auditable_type.constantize
59:     returning clazz.find_by_id(auditable_id) || clazz.new do |m|
60:       Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version}))
61:     end
62:   end

[Validate]