class Mongoid::SimpleAudit::Modification

Public Instance Methods

delta(other_change_log) click to toggle source

Computes the differences of the change logs between two audits.

Returns a hash containing arrays of the form

{
  :key_1 => [<value_in_other_change_log>, <value_in_this_audit>],
  :key_2 => [<value_in_other_change_log>, <value_in_this_audit>],
}
# File lib/mongoid/simple_audit/modification.rb, line 21
def delta(other_change_log)
  return self.change_log if other_change_log.nil?

  {}.tap do |d|
    
    # first for keys present only in this audit
    (self.change_log.keys - other_change_log.change_log.keys).each do |k|
      d[k] = [nil, self.change_log[k]]
    end

    # .. then for keys present only in other audit
    (other_change_log.change_log.keys - self.change_log.keys).each do |k|
      d[k] = [other_change_log.change_log[k], nil]
    end

    # .. finally for keys present in both, but with different values
    self.change_log.keys.each do |k|
      if self.change_log[k] != other_change_log.change_log[k]
        d[k] = [other_change_log.change_log[k], self.change_log[k]]
      end
    end

  end

end
symbolized_change_log() click to toggle source

as mongoid stores hashes keys as strings, here us a way to get the change log symbolized TODO: refactor

# File lib/mongoid/simple_audit/modification.rb, line 49
def symbolized_change_log
  sym_log = self.change_log.dup
  sym_log = sym_log.symbolize_keys
  sym_log.each{ |k,v| sym_log[k] = v.symbolize_keys if v.is_a? Hash }
  sym_log
end