module Relational::Audit::InstanceMethod

Public Instance Methods

add_audit(event_type, related_to) click to toggle source
# 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) click to toggle source
# 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_audit() click to toggle source
# File lib/relational/audit.rb, line 118
def add_created_child_audit
  self.add_child_audit(::Relational::Audit::Event::CREATE)
end
add_created_relational_audit() click to toggle source
# File lib/relational/audit.rb, line 60
def add_created_relational_audit
  add_audit(::Relational::Audit::Event::CREATE, [self])
end
add_destroyed_child_audit() click to toggle source
# File lib/relational/audit.rb, line 126
def add_destroyed_child_audit
  self.add_child_audit(::Relational::Audit::Event::DESTROY)
end
add_destroyed_relational_audit() click to toggle source
# File lib/relational/audit.rb, line 68
def add_destroyed_relational_audit
  add_audit(::Relational::Audit::Event::DESTROY, [self])
end
add_updated_child_audit() click to toggle source
# File lib/relational/audit.rb, line 122
def add_updated_child_audit
  self.add_child_audit(::Relational::Audit::Event::UPDATE)
end
add_updated_relational_audit() click to toggle source
# File lib/relational/audit.rb, line 64
def add_updated_relational_audit
  add_audit(::Relational::Audit::Event::UPDATE, [self])
end
audits(order='DESC') click to toggle source
# 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_audits() click to toggle source
# 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