Method: Effective::Resources::Instance#instance_changes

Defined in:
app/models/effective/resources/instance.rb

#instance_changes(only: nil, except: nil) ⇒ Object

used by effective_logging



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/effective/resources/instance.rb', line 78

def instance_changes(only: nil, except: nil)
  return {} unless (instance.present? && instance.previous_changes.present?)

  # Build up our only and except
  only = Array(only).map(&:to_sym)
  except = Array(except).map(&:to_sym) + BLACKLIST

  changes = instance.previous_changes.symbolize_keys.delete_if do |attribute, (before, after)|
    begin
      (before.kind_of?(ActiveSupport::TimeWithZone) && after.kind_of?(ActiveSupport::TimeWithZone) && before.to_i == after.to_i) ||
      (before == nil && after == false) || (before == nil && after == ''.freeze)
    rescue => e
      true
    end
  end

  changes = changes.except(*except) if except.present?
  changes = changes.slice(*only) if only.present?

  # Log to_s changes on all belongs_to associations
  belong_tos.each do |association|
    next if except.present? && except.include?(association.name)
    next unless only.blank? || only.include?(association.name)

    if (change = changes.delete(association.foreign_key)).present?
      changes[association.name] = [(association.klass.find_by_id(change.first) if changes.first), instance.send(association.name)]
    end
  end

  changes
end