Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 1.02 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. # app/controllers/application_controller.rb
  2. class ApplicationController < ActionController
  3.   after_filter :audit, :only => [:create, :update, :destroy]
  4.  
  5.   def audit
  6.     # Cant remember if its controller.controller_name or just controller_name
  7.     object_name = controller.controller_name.singularize
  8.     object = instance_variable_get(object_name)
  9.  
  10.     object.audit_log(session[:user_id], controller.action_name)
  11.   end
  12.  
  13. end
  14.  
  15. # app/controllers/products_controller.rb
  16. class ProductsController < ApplicationController
  17.  
  18.   def create
  19.     @product = Product.create(params[:product])
  20.   end
  21.  
  22. end
  23.  
  24. # app/models/product.rb
  25. class Product < ActiveRecord
  26.   include Auditable
  27. end
  28.  
  29. # lib/auditable.rb
  30. module Audtiable
  31.  
  32.   def audit_log(user_id, action_name)
  33.     case action_name
  34.     when :create;  log_message = "User: #{user_id} created product: #{self.attributes.inspect}"
  35.     when :update;  log_message = "woop woop woop"
  36.     when :destroy; log_message = "tam tam"
  37.     end
  38.  
  39.     logger.info(log_message)
  40.   end
  41.  
  42. end