Module: Auditing::Base::InstanceMethods

Defined in:
lib/auditing/base.rb

Instance Method Summary collapse

Instance Method Details

#class_exists?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/auditing/base.rb', line 78

def class_exists?(class_name)
  klass = Module.const_get(class_name)
  return klass.is_a?(Class)
rescue NameError
  return false
end

#dump_data(value) ⇒ Object



33
34
35
# File 'lib/auditing/base.rb', line 33

def dump_data(value)
  ActiveSupport::Base64.encode64(Marshal.dump(value))
end

#log_association_create(child_object, hash) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/auditing/base.rb', line 54

def log_association_create(child_object, hash)
  add_audit(:action      => 'added',
            :association => child_object,
            :field_name  => hash[:field],
            :old_value   => dump_data(nil),
            :new_value   => dump_data(hash[:value]) )
end

#log_association_destroy(item) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/auditing/base.rb', line 70

def log_association_destroy(item)
  mark_as_undoable = audits.where({:association_id => item.id, :association_type => item.class.to_s})
  mark_as_undoable.each do |i|
    i.update_attribute('undoable', false)
  end
  add_audit(:action => 'removed', :association => item, :undoable => false)
end

#log_association_update(child_object, hash) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/auditing/base.rb', line 62

def log_association_update(child_object, hash)
  add_audit(:action      => 'updated',
            :association => child_object,
            :field_name  => hash[:field],
            :old_value   => ( dump_data(hash[:old_value]) if hash[:old_value] ) || dump_data(nil),
            :new_value   => dump_data(hash[:new_value]) ) 
end

#log_creationObject



37
38
39
# File 'lib/auditing/base.rb', line 37

def log_creation
  add_audit(:action => 'created', :undoable => false)
end

#log_updateObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/auditing/base.rb', line 41

def log_update
  if changed?
    changes.each.select {|k,v| auditing_fields.include?(k)}.each do |field, change|
      next if change[0].to_s == change[1].to_s

      add_audit(:action     => 'updated',
                :field_name => field,
                :old_value  => dump_data(change[0]),
                :new_value  => dump_data(change[1]) )
    end
  end
end