Class: SimpleAudit::Audit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/simple_audit/audit.rb

Overview

Changes of the audited models will be stored here.

Instance Method Summary collapse

Instance Method Details

#delta(other_audit) ⇒ Object

Computes the differences of the change logs between two audits.

Returns a hash containing arrays of the form

{
  :key_1 => [<value_in_other_audit>, <value_in_this_audit>],
  :key_2 => [<value_in_other_audit>, <value_in_this_audit>],
}


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simple_audit/audit.rb', line 16

def delta(other_audit)

  return self.change_log if other_audit.nil?

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

    # .. then for keys present only in other audit
    (other_audit.change_log.keys - self.change_log.keys).each do |k|
      d[k] = [other_audit.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_audit.change_log[k]
        d[k] = [other_audit.change_log[k], self.change_log[k]]
      end
    end

  end

end