Class: Mongoid::SimpleAudit::Modification

Inherits:
Object
  • Object
show all
Includes:
Attributes::Dynamic, Document, Timestamps::Created
Defined in:
lib/mongoid/simple_audit/modification.rb

Instance Method Summary collapse

Instance Method Details

#delta(other_change_log) ⇒ Object

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>],
}


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_logObject

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



49
50
51
52
53
54
# 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