Created
October 16, 2009 09:57
-
-
Save svs/211687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#controllers/application.rb | |
... | |
class Hash | |
def diff(other) | |
keys = self.keys | |
keys.each.select{|k| self[k] != other[k]} | |
end | |
end | |
#config/init.rb | |
Merb::BootLoader.before_app_loads do | |
... | |
require "lib/logger.rb" | |
... | |
end | |
Merb::BootLoader.after_app_loads do | |
... | |
Misfit::Logger.start(['Clients','Branches','Centers','Loans']) | |
... | |
end | |
# lib/logger.rb | |
module Misfit | |
module Logger | |
def self.start(controllers) | |
controllers.each do |c| | |
puts "logging #{c}\n" | |
cont = Kernel.const_get(c) | |
cont.class_eval do | |
include Misfit::ControllerFunctions | |
end | |
cont.add_filter(cont._before_filters, :get_object_state, {:only => ['update']}) | |
cont.add_filter(cont._after_filters, :_log, {:only => ['update','create']}) | |
end | |
end | |
end | |
end | |
module Misfit | |
module ControllerFunctions | |
def self.included(base) | |
Merb.logger.info("#{base.to_s} included Controllerfunctions") | |
end | |
def self.extended(base) | |
base.extend ClassMethods | |
Merb.logger.info("#{base.to_s} extended Controllerfunctions") | |
end | |
def get_object_state | |
model = self.class.to_s.singular | |
object = eval"#{model}.get(params[:id])" | |
@ributes = object.attributes | |
end | |
def _log | |
f = File.open("log/#{self.class}.log","a") | |
object = eval("@#{self.class.to_s.downcase.singular}") | |
if object | |
attributes = object.attributes | |
diff = @ributes.diff(attributes) | |
diff_string = diff.map{|k| "#{k} from #{@ributes[k]} to #{attributes[k]}" if k != :updated_at}.join("\t") | |
log = "#{Time.now}\t#{session.user.login}\t#{diff_string}\n" | |
f.write(log) | |
f.close | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment