
Untitled
By: a guest on Jan 28th, 2012 | syntax:
None | size: 1.02 KB | hits: 15 | expires: Never
# app/controllers/application_controller.rb
class ApplicationController < ActionController
after_filter :audit, :only => [:create, :update, :destroy]
def audit
# Cant remember if its controller.controller_name or just controller_name
object_name = controller.controller_name.singularize
object = instance_variable_get(object_name)
object.audit_log(session[:user_id], controller.action_name)
end
end
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def create
@product = Product.create(params[:product])
end
end
# app/models/product.rb
class Product < ActiveRecord
include Auditable
end
# lib/auditable.rb
module Audtiable
def audit_log(user_id, action_name)
case action_name
when :create; log_message = "User: #{user_id} created product: #{self.attributes.inspect}"
when :update; log_message = "woop woop woop"
when :destroy; log_message = "tam tam"
end
logger.info(log_message)
end
end