class ModelAuditor::Changes

Constants

IGNORED_ATTRIBUTES
TRUE_ARRAY

Public Class Methods

new(model, changes = nil, options = {}) click to toggle source
# File lib/model_auditor/changes.rb, line 8
def initialize(model, changes = nil, options = {})
  @model = model
  @changes = changes || @model.previous_changes
  @options = options
end

Public Instance Methods

audit() click to toggle source
# File lib/model_auditor/changes.rb, line 24
def audit
  return unless changed?

  changes = @changes.keys.inject({}) do |hash, key|
    hash.merge!(human_name(key) => human_value(key))
  end

  changes.presence
end
filter(filtered = nil) click to toggle source
# File lib/model_auditor/changes.rb, line 14
def filter(filtered = nil)
  filtered ||= IGNORED_ATTRIBUTES

  @changes = @changes.reject do |key, _value|
    Array(filtered).include?(key.to_sym)
  end

  self
end

Private Instance Methods

array_diff(key) click to toggle source
# File lib/model_auditor/changes.rb, line 57
def array_diff(key)
  Normalizers::ArrayDiff.new(@changes, key).normalize
end
changed?() click to toggle source
# File lib/model_auditor/changes.rb, line 36
def changed?
  return true if force_changed?

  @model.created_at != @model.updated_at && @model.persisted? && !@model.destroyed?
end
force_changed?() click to toggle source
# File lib/model_auditor/changes.rb, line 42
def force_changed?
  TRUE_ARRAY.include?(@options[:changed])
end
human_name(key) click to toggle source
# File lib/model_auditor/changes.rb, line 46
def human_name(key)
  @model.class.human_attribute_name(key)
end
human_value(key) click to toggle source
# File lib/model_auditor/changes.rb, line 50
def human_value(key)
  value = ModelAuditor::AttributeReader.new(@model, key).value
  return array_diff(key) if value.is_a? Array

  ModelAuditor.normalize_value(value)
end