class Audited::Audit
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
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.
# 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
@private
# File lib/audited/audit.rb, line 115 def self.assign_revision_attributes(record, attributes) attributes.each do |attr, val| record = record.dup if record.frozen? if record.respond_to?("#{attr}=") record.attributes.key?(attr.to_s) ? record[attr] = val : record.send("#{attr}=", val) end end record end
Returns the list of classes that are being audited
# File lib/audited/audit.rb, line 90 def self.audited_classes audited_class_names.map(&:constantize) end
@private
# File lib/audited/audit.rb, line 105 def self.reconstruct_attributes(audits) attributes = {} result = audits.collect do |audit| attributes.merge!(audit.new_attributes)[:version] = audit.version yield attributes if block_given? end block_given? ? result : attributes end
Public Instance Methods
Return all audits older than the current one.
# File lib/audited/audit.rb, line 40 def ancestors self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version) end
Returns a hash of the changed attributes with the new values
# 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
Returns a hash of the changed attributes with the old values
# 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
Return an instance of what the object looked like at this revision. If the object has been destroyed, this will be a new record.
# 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
@private
# File lib/audited/audit.rb, line 83 def user_as_string user_as_model || username end
Allows user to be set to either a string or an ActiveRecord object @private
# File lib/audited/audit.rb, line 72 def user_as_string=(user) # 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
Private Instance Methods
# File lib/audited/audit.rb, line 135 def set_audit_user self.user = Thread.current[:audited_user] if Thread.current[:audited_user] nil # prevent stopping callback chains end
# File lib/audited/audit.rb, line 140 def set_request_uuid self.request_uuid ||= SecureRandom.uuid end
# File lib/audited/audit.rb, line 130 def set_version_number max = self.class.auditable_finder(auditable_id, auditable_type).descending.first.try(:version) || 0 self.version = max + 1 end