Module: Relational::Audit::InstanceMethod

Defined in:
lib/relational/audit.rb

Instance Method Summary collapse

Instance Method Details

#add_audit(event_type, related_to) ⇒ Object



72
73
74
75
76
77
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
# File 'lib/relational/audit.rb', line 72

def add_audit event_type, related_to

  hash_changes = self.changes

  options = self.class.audit_options

  unless options[:only].blank?
    hash_changes.delete_if { |key, value| !options[:only].include?(key.intern) }
  end

  unless options[:ignore].blank?
    hash_changes.delete_if { |key, value| options[:ignore].include?(key.intern) }
  end

  return if hash_changes.blank?

  serialized_changes = YAML::dump hash_changes

  audit = ::RelationalAudit::Audit.new(
      :entity_type => self.class.name,
      :entity_id => self.id,
      :entity_changes => serialized_changes,
      :changes_by => ::Relational::Audit.audit_changes_by,
      :event => event_type,
      :transaction_id => ::Relational::Audit::audit_transaction_id
  )

  related_to.each do |entity|
    audit.audit_relations.build(
        :relation_type => entity.class.name,
        :relation_id => entity.id
    )
  end

  audit.save
end

#add_child_audit(event_type) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/relational/audit.rb', line 109

def add_child_audit(event_type)
  related_to = self.class.audit_entities.collect do |entity|
    self.send(entity)
  end
  related_to.push(self)

  add_audit(event_type, related_to.compact)
end

#add_created_child_auditObject



118
119
120
# File 'lib/relational/audit.rb', line 118

def add_created_child_audit
  self.add_child_audit(::Relational::Audit::Event::CREATE)
end

#add_created_relational_auditObject



60
61
62
# File 'lib/relational/audit.rb', line 60

def add_created_relational_audit
  add_audit(::Relational::Audit::Event::CREATE, [self])
end

#add_destroyed_child_auditObject



126
127
128
# File 'lib/relational/audit.rb', line 126

def add_destroyed_child_audit
  self.add_child_audit(::Relational::Audit::Event::DESTROY)
end

#add_destroyed_relational_auditObject



68
69
70
# File 'lib/relational/audit.rb', line 68

def add_destroyed_relational_audit
  add_audit(::Relational::Audit::Event::DESTROY, [self])
end

#add_updated_child_auditObject



122
123
124
# File 'lib/relational/audit.rb', line 122

def add_updated_child_audit
  self.add_child_audit(::Relational::Audit::Event::UPDATE)
end

#add_updated_relational_auditObject



64
65
66
# File 'lib/relational/audit.rb', line 64

def add_updated_relational_audit
  add_audit(::Relational::Audit::Event::UPDATE, [self])
end

#audits(order = 'DESC') ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/relational/audit.rb', line 40

def audits(order='DESC')
  grouped_audits = {}
  self.raw_audits.order("id #{order}").each do |audit|
    grouped_audits[audit.transaction_id] ||= []
    grouped_audits[audit.transaction_id] << audit
  end

  entity_audits = grouped_audits.collect do |transaction_id, audits|
    unless audits.blank?
      merged_audit = {:changes_by => audits.first.changes_by, :changes => {}}
      audits.each do |audit|
        merged_audit[:changes].merge!(audit.changeset)
      end
      merged_audit
    end
  end.compact

  entity_audits
end

#raw_auditsObject



36
37
38
# File 'lib/relational/audit.rb', line 36

def raw_audits
  ::RelationalAudit::Audit.joins('join audit_relations on audits.id = audit_relations.audit_id').where("audit_relations.relation_type = ? and audit_relations.relation_id = ?", self.class.name, self.id)
end