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
  • audited_changes: a serialized hash of all the changes
  • comment: a comment set with the audit
  • created_at: Time that the change was performed

Methods

External Aliases

user= -> user_as_model=
user -> user_as_model

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 38
38:     def as_user(user, &block)
39:       Thread.current[:acts_as_audited_user] = user
40: 
41:       yieldval = yield
42: 
43:       Thread.current[:acts_as_audited_user] = nil
44: 
45:       yieldval
46:     end

@private

[Source]

    # File lib/acts_as_audited/audit.rb, line 59
59:     def assign_revision_attributes(record, attributes)
60:       attributes.each do |attr, val|
61:         record = record.dup if record.frozen?
62: 
63:         if record.respond_to?("#{attr}=")
64:           record.attributes.has_key?(attr.to_s) ?
65:             record[attr] = val :
66:             record.send("#{attr}=", val)
67:         end
68:       end
69:       record
70:     end

Returns the list of classes that are being audited

[Source]

    # File lib/acts_as_audited/audit.rb, line 31
31:     def audited_classes
32:       audited_class_names.map(&:constantize)
33:     end

@private

[Source]

    # File lib/acts_as_audited/audit.rb, line 49
49:     def reconstruct_attributes(audits)
50:       attributes = {}
51:       result = audits.collect do |audit|
52:         attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
53:         yield attributes if block_given?
54:       end
55:       block_given? ? result : attributes
56:     end

Public Instance methods

Return all audits older than the current one.

[Source]

     # File lib/acts_as_audited/audit.rb, line 103
103:   def ancestors
104:     self.class.where(['auditable_id = ? and auditable_type = ? and version <= ?',
105:       auditable_id, auditable_type, version])
106:   end

Returns a hash of the changed attributes with the new values

[Source]

     # File lib/acts_as_audited/audit.rb, line 109
109:   def new_attributes
110:     (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
111:       attrs[attr] = values.is_a?(Array) ? values.last : values
112:       attrs
113:     end
114:   end

Returns a hash of the changed attributes with the old values

[Source]

     # File lib/acts_as_audited/audit.rb, line 117
117:   def old_attributes
118:     (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
119:       attrs[attr] = Array(values).first
120:       attrs
121:     end
122:   end

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

[Source]

     # File lib/acts_as_audited/audit.rb, line 95
 95:   def revision
 96:     clazz = auditable_type.constantize
 97:     ( clazz.find_by_id(auditable_id) || clazz.new ).tap do |m|
 98:       Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version}))
 99:     end
100:   end
user()

Alias for user_as_string

user=(user)

Alias for user_as_string=

@private

[Source]

    # File lib/acts_as_audited/audit.rb, line 87
87:   def user_as_string
88:     self.user_as_model || self.username
89:   end

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

[Source]

    # File lib/acts_as_audited/audit.rb, line 76
76:   def user_as_string=(user)
77:     # reset both either way
78:     self.user_as_model = self.username = nil
79:     user.is_a?(ActiveRecord::Base) ?
80:       self.user_as_model = user :
81:       self.username = user
82:   end

[Validate]