From bed876819207e3ae84a285470b39544c2850f38b Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 9 Feb 2009 21:55:02 +0100 Subject: * here comes da wicked sick auditing observer hack --- app/controllers/nodes_controller.rb | 1 + app/controllers/pages_controller.rb | 2 ++ app/models/auditing_observer.rb | 8 ++++++++ config/environment.rb | 1 + lib/auditing.rb | 26 ++++++++++++++++++++++++++ test/fixtures/users.yml | 4 +--- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 app/models/auditing_observer.rb create mode 100644 lib/auditing.rb diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index 1baf052..34394df 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -1,4 +1,5 @@ class NodesController < ApplicationController + include Auditing layout 'admin' diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 7e3f955..363d1e1 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,4 +1,6 @@ class PagesController < ApplicationController + include Auditing + # GET /pages # GET /pages.xml def index diff --git a/app/models/auditing_observer.rb b/app/models/auditing_observer.rb new file mode 100644 index 0000000..acce18c --- /dev/null +++ b/app/models/auditing_observer.rb @@ -0,0 +1,8 @@ +class AuditingObserver < Auditing::Observer + observe :node, :page + + # TODO: Insert super secure auditing here + def before_save(record) + RAILS_DEFAULT_LOGGER.debug ">>>>>>>>>>>>> #{controller.inspect}" + end +end diff --git a/config/environment.rb b/config/environment.rb index aad0815..4577575 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -30,6 +30,7 @@ Rails::Initializer.run do |config| # Activate observers that should always be running # config.active_record.observers = :cacher, :garbage_collector, :forum_observer + config.active_record.observers = :auditing_observer # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. diff --git a/lib/auditing.rb b/lib/auditing.rb new file mode 100644 index 0000000..5379148 --- /dev/null +++ b/lib/auditing.rb @@ -0,0 +1,26 @@ + module Auditing + def self.included(base) + ActiveRecord::Base.observers.each do |observer| + observer = if observer.respond_to?(:to_sym) + observer.to_s.camelize.constantize.instance + elsif observer.respond_to?(:instance) + observer.instance + else + raise ArgumentError, "#{observer} is an invalid class name" + end + base.around_filter(observer) if observer.is_a?(Auditing::Observer) + end + end + + class Observer < ActiveRecord::Observer + attr_accessor :controller + + def before(controller) + self.controller = controller + end + + def after(controller) + self.controller = nil + end + end +end \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index a6570e0..3afb3fd 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -13,6 +13,4 @@ aaron: email: aaron@example.com salt: 5be6f9cdd04fd7ab3c91cd32a5334ba2339b8005 crypted_password: 740a48caf7dd5ff11318d812d57c0a0928cfbc12 # 'monkey' - created_at: <%= 1.days.ago.to_s :db %> - - + created_at: <%= 1.days.ago.to_s :db %> \ No newline at end of file -- cgit v1.3