module Audited::Auditor::AuditedInstanceMethods

def audit_create

def audit_create
  write_audit(action: "create", audited_changes: audited_attributes,
    comment: audit_comment)
end

def audit_destroy

def audit_destroy
  unless new_record?
    write_audit(action: "destroy", audited_changes: audited_attributes,
      comment: audit_comment)
  end
end

def audit_touch

def audit_touch
  unless (changes = audited_changes(for_touch: true)).empty?
    write_audit(action: "update", audited_changes: changes,
      comment: audit_comment)
  end
end

def audit_update

def audit_update
  unless (changes = audited_changes).empty? && (audit_comment.blank? || audited_options[:update_with_comment_only] == false)
    write_audit(action: "update", audited_changes: changes,
      comment: audit_comment)
  end
end

def audited_attributes

List of attributes that are audited.
def audited_attributes
  audited_attributes = attributes.except(*self.class.non_audited_columns)
  audited_attributes = redact_values(audited_attributes)
  audited_attributes = filter_encrypted_attrs(audited_attributes)
  normalize_enum_changes(audited_attributes)
end

def audited_changes(for_touch: false)

def audited_changes(for_touch: false)
  all_changes = if for_touch
    previous_changes
  elsif respond_to?(:changes_to_save)
    changes_to_save
  else
    changes
  end
  filtered_changes = \
    if audited_options[:only].present?
      all_changes.slice(*self.class.audited_columns)
    else
      all_changes.except(*self.class.non_audited_columns)
    end
  if for_touch && (last_audit = audits.last&.audited_changes)
    filtered_changes.reject! do |k, v|
      last_audit[k].to_json == v.to_json ||
      last_audit[k].to_json == v[1].to_json
    end
  end
  filtered_changes = redact_values(filtered_changes)
  filtered_changes = filter_encrypted_attrs(filtered_changes)
  filtered_changes = normalize_enum_changes(filtered_changes)
  filtered_changes.to_hash
end

def auditing_enabled

def auditing_enabled
  run_conditional_check(audited_options[:if]) &&
    run_conditional_check(audited_options[:unless], matching: false) &&
    self.class.auditing_enabled
end

def audits_to(version = nil)

def audits_to(version = nil)
  if version == :previous
    version = if audit_version
      audit_version - 1
    else
      previous = audits.descending.offset(1).first
      previous ? previous.version : 1
    end
  end
  audits.to_version(version)
end

def combine_audits(audits_to_combine)

Combine multiple audits into one.
def combine_audits(audits_to_combine)
  combine_target = audits_to_combine.last
  combine_target.audited_changes = audits_to_combine.pluck(:audited_changes).reduce(&:merge)
  combine_target.comment = "#{combine_target.comment}\nThis audit is the result of multiple audits being combined."
  transaction do
    begin
      combine_target.save!
      audits_to_combine.unscope(:limit).where("version < ?", combine_target.version).delete_all
    rescue ActiveRecord::Deadlocked
      # Ignore Deadlocks, if the same record is getting its old audits combined more than once at the same time then
      # both combining operations will be the same. Ignoring this error allows one of the combines to go through successfully.
    end
  end
end

def combine_audits_if_needed

def combine_audits_if_needed
  max_audits = audited_options[:max_audits]
  if max_audits && (extra_count = audits.count - max_audits) > 0
    audits_to_combine = audits.limit(extra_count + 1)
    combine_audits(audits_to_combine)
  end
end

def comment_required_state?

def comment_required_state?
  auditing_enabled &&
    audited_changes.present? &&
    ((audited_options[:on].include?(:create) && new_record?) ||
    (audited_options[:on].include?(:update) && persisted? && changed?))
end

def filter_attr_values(audited_changes: {}, attrs: [], placeholder: "[FILTERED]")

Parameters:
  • placeholder (String) -- Placeholder to replace original attr values
  • attrs (Array) -- Array of attrs, values of which will be replaced to placeholder value
  • audited_changes (Hash) -- Hash of changes to be saved to audited version record
def filter_attr_values(audited_changes: {}, attrs: [], placeholder: "[FILTERED]")
  attrs.each do |attr|
    next unless audited_changes.key?(attr)
    changes = audited_changes[attr]
    values = changes.is_a?(Array) ? changes.map { placeholder } : placeholder
    audited_changes[attr] = values
  end
  audited_changes
end

def filter_encrypted_attrs(filtered_changes)

def filter_encrypted_attrs(filtered_changes)
  filter_attr_values(
    audited_changes: filtered_changes,
    attrs: respond_to?(:encrypted_attributes) ? Array(encrypted_attributes).map(&:to_s) : []
  )
end

def normalize_enum_changes(changes)

def normalize_enum_changes(changes)
  return changes if Audited.store_synthesized_enums
  self.class.defined_enums.each do |name, values|
    if changes.has_key?(name)
      changes[name] = \
        if changes[name].is_a?(Array)
          changes[name].map { |v| values[v] }
        elsif rails_below?("5.0")
          changes[name]
        else
          values[changes[name]]
        end
    end
  end
  changes
end

def own_and_associated_audits

Returns a list combined of record audits and associated audits.
def own_and_associated_audits
  Audited.audit_class.unscoped.where(auditable: self)
    .or(Audited.audit_class.unscoped.where(associated: self))
    .order(created_at: :desc)
end

def presence_of_audit_comment

def presence_of_audit_comment
  if comment_required_state?
    errors.add(:audit_comment, :blank) unless audit_comment.present?
  end
end

def rails_below?(rails_version)

def rails_below?(rails_version)
  Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new(rails_version)
end

def reconstruct_attributes(audits)

def reconstruct_attributes(audits)
  attributes = {}
  audits.each { |audit| attributes.merge!(audit.new_attributes) }
  attributes
end

def redact_values(filtered_changes)

def redact_values(filtered_changes)
  filter_attr_values(
    audited_changes: filtered_changes,
    attrs: Array(audited_options[:redacted]).map(&:to_s),
    placeholder: audited_options[:redaction_value] || REDACTED
  )
end

def require_comment

def require_comment
  if auditing_enabled && audit_comment.blank?
    errors.add(:audit_comment, :blank)
    throw(:abort)
  end
end

def revision(version)

Returns nil for versions greater than revisions count
Get a specific revision specified by the version number, or +:previous+
def revision(version)
  if version == :previous || audits.last.version >= version
    revision_with Audited.audit_class.reconstruct_attributes(audits_to(version))
  end
end

def revision_at(date_or_time)

Find the oldest revision recorded prior to the date/time provided.
def revision_at(date_or_time)
  audits = self.audits.up_until(date_or_time)
  revision_with Audited.audit_class.reconstruct_attributes(audits) unless audits.empty?
end

def revision_with(attributes)

def revision_with(attributes)
  dup.tap do |revision|
    revision.id = id
    revision.send :instance_variable_set, "@new_record", destroyed?
    revision.send :instance_variable_set, "@persisted", !destroyed?
    revision.send :instance_variable_set, "@readonly", false
    revision.send :instance_variable_set, "@destroyed", false
    revision.send :instance_variable_set, "@_destroyed", false
    revision.send :instance_variable_set, "@marked_for_destruction", false
    Audited.audit_class.assign_revision_attributes(revision, attributes)
    # Remove any association proxies so that they will be recreated
    # and reference the correct object for this revision. The only way
    # to determine if an instance variable is a proxy object is to
    # see if it responds to certain methods, as it forwards almost
    # everything to its target.
    revision.instance_variables.each do |ivar|
      proxy = revision.instance_variable_get ivar
      if !proxy.nil? && proxy.respond_to?(:proxy_respond_to?)
        revision.instance_variable_set ivar, nil
      end
    end
  end
end

def revisions(from_version = 1)


end
user.version
user.name
user.revisions.each do |revision|

Gets an array of the revisions available
def revisions(from_version = 1)
  return [] unless audits.from_version(from_version).exists?
  all_audits = audits.select([:audited_changes, :version, :action]).to_a
  targeted_audits = all_audits.select { |audit| audit.version >= from_version }
  previous_attributes = reconstruct_attributes(all_audits - targeted_audits)
  targeted_audits.map do |audit|
    previous_attributes.merge!(audit.new_attributes)
    revision_with(previous_attributes.merge!(version: audit.version))
  end
end

def run_conditional_check(condition, matching: true)

def run_conditional_check(condition, matching: true)
  return true if condition.blank?
  return condition.call(self) == matching if condition.respond_to?(:call)
  return send(condition) == matching if respond_to?(condition.to_sym, true)
  true
end

def save_with_auditing

Temporarily turns on auditing while saving.
def save_with_auditing
  with_auditing { save }
end

def save_without_auditing

Temporarily turns off auditing while saving.
def save_without_auditing
  without_auditing { save }
end

def with_auditing(&block)


end
@foo.save
@foo.with_auditing do

Executes the block with the auditing callbacks enabled.
def with_auditing(&block)
  self.class.with_auditing(&block)
end

def without_auditing(&block)


end
@foo.save
@foo.without_auditing do

Executes the block with the auditing callbacks disabled.
def without_auditing(&block)
  self.class.without_auditing(&block)
end

def write_audit(attrs)

def write_audit(attrs)
  self.audit_comment = nil
  if auditing_enabled
    attrs[:associated] = send(audit_associated_with) unless audit_associated_with.nil?
    run_callbacks(:audit) {
      audit = audits.create(attrs)
      combine_audits_if_needed if attrs[:action] != "create"
      audit
    }
  end
end