class Audited::Audit
def self.as_user(user)
by +user+. This method is hopefully threadsafe, making it ideal
All audits made during the block called will be recorded as made
def self.as_user(user) last_audited_user = ::Audited.store[:audited_user] ::Audited.store[:audited_user] = user yield ensure ::Audited.store[:audited_user] = last_audited_user end
def self.assign_revision_attributes(record, attributes)
- Private: -
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
def self.audited_classes
def self.audited_classes audited_class_names.map(&:constantize) end
def self.collection_cache_key(collection = all, *)
def self.collection_cache_key(collection = all, *) super(collection, :created_at) end
def self.reconstruct_attributes(audits)
- Private: -
def self.reconstruct_attributes(audits) audits.each_with_object({}) do |audit, all| all.merge!(audit.new_attributes) all[:audit_version] = audit.version end end
def ancestors
def ancestors self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version) end
def new_attributes
def new_attributes (audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs| attrs[attr] = (action == "update" ? values.last : values) end end
def old_attributes
def old_attributes (audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs| attrs[attr] = (action == "update" ? values.first : values) end end
def revision
Return an instance of what the object looked like at this revision. If
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(audit_version: version)) end end
def set_audit_user
def set_audit_user self.user ||= ::Audited.store[:audited_user] # from .as_user self.user ||= ::Audited.store[:current_user].try!(:call) # from Sweeper nil # prevent stopping callback chains end
def set_remote_address
def set_remote_address self.remote_address ||= ::Audited.store[:current_remote_address] end
def set_request_uuid
def set_request_uuid self.request_uuid ||= ::Audited.store[:current_request_uuid] self.request_uuid ||= SecureRandom.uuid end
def set_version_number
def set_version_number if action == "create" self.version = 1 else collection = Rails::VERSION::MAJOR >= 6 ? self.class.unscoped : self.class max = collection.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0 self.version = max + 1 end end
def undo
def undo case action when "create" # destroys a newly created record auditable.destroy! when "destroy" # creates a new record with the destroyed record attributes auditable_type.constantize.create!(audited_changes) when "update" # changes back attributes auditable.update!(audited_changes.transform_values(&:first)) else raise StandardError, "invalid action given #{action}" end end
def user_as_string
- Private: -
def user_as_string user_as_model || username end
def user_as_string=(user)
- Private: -
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