Skip to content

Instantly share code, notes, and snippets.

@despinozac
Created March 7, 2016 23:03
Show Gist options
  • Save despinozac/0621c7ffade2ce348dc5 to your computer and use it in GitHub Desktop.
Save despinozac/0621c7ffade2ce348dc5 to your computer and use it in GitHub Desktop.
Auditable Module
module Auditable
extend ActiveSupport::Concern
# Array de exclusion de auditoŕia
BLACK_LIST = ["created_at", "updated_at", "metadata"]
included do
before_update :audit_record_update
after_create :audit_record_create
before_destroy :audit_record_destroy
end
private
def audit_record_update
recurso = self.class.name.underscore
self.changes.each do |key,value|
unless BLACK_LIST.include?(key)
Audit.create( user_id: Thread.current[:current_user], recurso: recurso, pk_registro: self.id,
columna: key, accion: "update", anterior: value.first
) unless Thread.current[:current_user].nil?
end
end
end
def audit_record_create
recurso = self.class.name.underscore
Audit.create( user_id: Thread.current[:current_user], recurso: recurso, pk_registro: self.id,
columna: "", accion: "create", anterior: "") unless recurso == "audit"
end
def audit_record_destroy
recurso = self.class.name.underscore
self.attributes.each do |attribute|
unless BLACK_LIST.include?(attribute)
Audit.create( user_id: Thread.current[:current_user], recurso: recurso, pk_registro: self.id,
columna: attribute.first, accion: "destroy", anterior: attribute.last ) unless recurso == "audit"
end
end
end
end
ActiveRecord::Base.send(:include, Auditable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment