From ff818e3accd8a18c58949445740a05c112a31c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Ho=CC=88glund?= Date: Fri, 18 Mar 2011 09:50:34 +0100 Subject: [PATCH 1/4] Added the possibility to disable/enable PaperTrail from a controller --- README.md | 11 +++++++ lib/paper_trail.rb | 11 ++++++- lib/paper_trail/controller.rb | 10 ++++++- lib/paper_trail/has_paper_trail.rb | 2 +- .../app/controllers/application_controller.rb | 1 + .../app/controllers/widgets_controller.rb | 5 ++++ test/functional/controller_test.rb | 30 +++++++++++++++++++ 7 files changed, 67 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3ea15d8a..bca27e24 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,14 @@ +# Extended + +Added the possibility to dissable/enable PaperTrail from a controller. + + class ApplicationController + def paper_trail_enabled_if + proc{ request.user_agent != 'Disabled User-Agent' } + end + end + + # PaperTrail PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed. diff --git a/lib/paper_trail.rb b/lib/paper_trail.rb index 4a437c86..1ffa7518 100644 --- a/lib/paper_trail.rb +++ b/lib/paper_trail.rb @@ -20,7 +20,16 @@ module PaperTrail !!PaperTrail.config.enabled end - + # Returns if PaperTrails is disabled by controller + def self.request_disabled + paper_trail_store[:request_disabled] + end + + # Sets if PaperTrails is disabled by controller + def self.request_disabled=(value) + paper_trail_store[:request_disabled] = value + end + # Returns who is reponsible for any changes that occur. def self.whodunnit paper_trail_store[:whodunnit] diff --git a/lib/paper_trail/controller.rb b/lib/paper_trail/controller.rb index b4de7c4e..b517d15f 100644 --- a/lib/paper_trail/controller.rb +++ b/lib/paper_trail/controller.rb @@ -4,6 +4,7 @@ module PaperTrail def self.included(base) base.before_filter :set_paper_trail_whodunnit base.before_filter :set_paper_trail_controller_info + base.before_filter :set_paper_trail_enabled_if end protected @@ -39,7 +40,14 @@ module PaperTrail end private - + + # Tells PaperTrail if version should be saved. + def set_paper_trail_enabled_if + if respond_to? :paper_trail_enabled_if + ::PaperTrail.request_disabled = !paper_trail_enabled_if + end + end + # Tells PaperTrail who is responsible for any changes that occur. def set_paper_trail_whodunnit ::PaperTrail.whodunnit = user_for_paper_trail diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index fbc1c5cf..9443fdf1 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -162,7 +162,7 @@ module PaperTrail # Returns `true` if PaperTrail is globally enabled and active for this class, # `false` otherwise. def switched_on? - PaperTrail.enabled? && self.class.paper_trail_active + PaperTrail.enabled? && self.class.paper_trail_active && !PaperTrail.request_disabled end end diff --git a/test/dummy/app/controllers/application_controller.rb b/test/dummy/app/controllers/application_controller.rb index 41b9ce3c..29a85977 100644 --- a/test/dummy/app/controllers/application_controller.rb +++ b/test/dummy/app/controllers/application_controller.rb @@ -13,4 +13,5 @@ class ApplicationController < ActionController::Base def info_for_paper_trail {:ip => request.remote_ip, :user_agent => request.user_agent} end + end diff --git a/test/dummy/app/controllers/widgets_controller.rb b/test/dummy/app/controllers/widgets_controller.rb index a791580c..638c91df 100644 --- a/test/dummy/app/controllers/widgets_controller.rb +++ b/test/dummy/app/controllers/widgets_controller.rb @@ -1,4 +1,9 @@ class WidgetsController < ApplicationController + + def paper_trail_enabled_if + request.user_agent != 'Disable User-Agent' + end + def create @widget = Widget.create params[:widget] head :ok diff --git a/test/functional/controller_test.rb b/test/functional/controller_test.rb index d9c15123..a3cdc829 100644 --- a/test/functional/controller_test.rb +++ b/test/functional/controller_test.rb @@ -6,6 +6,36 @@ class ControllerTest < ActionController::TestCase setup do @request.env['REMOTE_ADDR'] = '127.0.0.1' end + + teardown do + PaperTrail.request_disabled = false + end + + test 'disable on create' do + @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent' + post :create, :widget => { :name => 'Flugel' } + assert_equal 0, assigns(:widget).versions.length + end + + test 'disable on update' do + @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent' + post :create, :widget => { :name => 'Flugel' } + w = assigns(:widget) + assert_equal 0, w.versions.length + put :update, :id => w.id, :widget => { :name => 'Bugle' } + widget = assigns(:widget) + assert_equal 0, widget.versions.length + end + + test 'disable on destroy' do + @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent' + post :create, :widget => { :name => 'Flugel' } + w = assigns(:widget) + assert_equal 0, w.versions.length + delete :destroy, :id => w.id + widget = assigns(:widget) + assert_equal 0, Version.with_item_keys('Widget', w.id).size + end test 'create' do post :create, :widget => { :name => 'Flugel' } From a85266ce407b29c47b37fdcf81e8ad8e7d9b8721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Ho=CC=88glund?= Date: Fri, 18 Mar 2011 09:51:12 +0100 Subject: [PATCH 2/4] Updated readme description --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bca27e24..c75de8b5 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Extended -Added the possibility to dissable/enable PaperTrail from a controller. +Added the possibility to disable/enable PaperTrail from a controller. class ApplicationController def paper_trail_enabled_if - proc{ request.user_agent != 'Disabled User-Agent' } + request.user_agent != 'Disable User-Agent' end end From 9a1bcff68bf854e9a9b4ac346d83384e62846a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Ho=CC=88glund?= Date: Fri, 18 Mar 2011 11:05:57 +0100 Subject: [PATCH 3/4] paper_trail_enabled_if overrides global settings --- lib/paper_trail.rb | 5 +++++ lib/paper_trail/has_paper_trail.rb | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/paper_trail.rb b/lib/paper_trail.rb index 1ffa7518..d1086dd4 100644 --- a/lib/paper_trail.rb +++ b/lib/paper_trail.rb @@ -30,6 +30,11 @@ module PaperTrail paper_trail_store[:request_disabled] = value end + # Sets if PaperTrails is disabled by controller + def self.request_disabled? + paper_trail_store[:request_disabled] === true + end + # Returns who is reponsible for any changes that occur. def self.whodunnit paper_trail_store[:whodunnit] diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 9443fdf1..700baf4a 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -159,10 +159,10 @@ module PaperTrail changed - self.class.ignore end - # Returns `true` if PaperTrail is globally enabled and active for this class, - # `false` otherwise. + # Returns `true` if controller method paper_trail_enabled_if doesn't return false, + # PaperTrail is globally enabled and active for this class, `false` otherwise. def switched_on? - PaperTrail.enabled? && self.class.paper_trail_active && !PaperTrail.request_disabled + !PaperTrail.request_disabled? && PaperTrail.enabled? && self.class.paper_trail_active end end From c84a94e66796ea647c8f462720e6042a7c24070c Mon Sep 17 00:00:00 2001 From: jhoglund Date: Fri, 18 Mar 2011 09:13:04 -0700 Subject: [PATCH 4/4] Edited README.md via GitHub --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c75de8b5..9342acbf 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ Added the possibility to disable/enable PaperTrail from a controller. - class ApplicationController - def paper_trail_enabled_if - request.user_agent != 'Disable User-Agent' + class ApplicationController + def paper_trail_enabled_if + request.user_agent != 'Disable User-Agent' + end end - end # PaperTrail