Merge branch 'master' of https://github.com/jhoglund/paper_trail into jhoglund-master
* 'master' of https://github.com/jhoglund/paper_trail: Edited README.md via GitHub paper_trail_enabled_if overrides global settings Updated readme description Added the possibility to disable/enable PaperTrail from a controller
This commit is contained in:
commit
81c3decad6
11
README.md
11
README.md
|
@ -1,3 +1,14 @@
|
||||||
|
# Extended
|
||||||
|
|
||||||
|
Added the possibility to disable/enable PaperTrail from a controller.
|
||||||
|
|
||||||
|
class ApplicationController
|
||||||
|
def paper_trail_enabled_if
|
||||||
|
request.user_agent != 'Disable User-Agent'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# PaperTrail
|
# 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.
|
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.
|
||||||
|
|
|
@ -20,6 +20,20 @@ module PaperTrail
|
||||||
!!PaperTrail.config.enabled
|
!!PaperTrail.config.enabled
|
||||||
end
|
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
|
||||||
|
|
||||||
|
# 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.
|
# Returns who is reponsible for any changes that occur.
|
||||||
def self.whodunnit
|
def self.whodunnit
|
||||||
|
|
|
@ -4,6 +4,7 @@ module PaperTrail
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.before_filter :set_paper_trail_whodunnit
|
base.before_filter :set_paper_trail_whodunnit
|
||||||
base.before_filter :set_paper_trail_controller_info
|
base.before_filter :set_paper_trail_controller_info
|
||||||
|
base.before_filter :set_paper_trail_enabled_if
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
@ -40,6 +41,13 @@ module PaperTrail
|
||||||
|
|
||||||
private
|
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.
|
# Tells PaperTrail who is responsible for any changes that occur.
|
||||||
def set_paper_trail_whodunnit
|
def set_paper_trail_whodunnit
|
||||||
::PaperTrail.whodunnit = user_for_paper_trail
|
::PaperTrail.whodunnit = user_for_paper_trail
|
||||||
|
|
|
@ -167,10 +167,10 @@ module PaperTrail
|
||||||
changed - self.class.ignore
|
changed - self.class.ignore
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns `true` if PaperTrail is globally enabled and active for this class,
|
# Returns `true` if controller method paper_trail_enabled_if doesn't return false,
|
||||||
# `false` otherwise.
|
# PaperTrail is globally enabled and active for this class, `false` otherwise.
|
||||||
def switched_on?
|
def switched_on?
|
||||||
PaperTrail.enabled? && self.class.paper_trail_active
|
!PaperTrail.request_disabled? && PaperTrail.enabled? && self.class.paper_trail_active
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,5 @@ class ApplicationController < ActionController::Base
|
||||||
def info_for_paper_trail
|
def info_for_paper_trail
|
||||||
{:ip => request.remote_ip, :user_agent => request.user_agent}
|
{:ip => request.remote_ip, :user_agent => request.user_agent}
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
class WidgetsController < ApplicationController
|
class WidgetsController < ApplicationController
|
||||||
|
|
||||||
|
def paper_trail_enabled_if
|
||||||
|
request.user_agent != 'Disable User-Agent'
|
||||||
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@widget = Widget.create params[:widget]
|
@widget = Widget.create params[:widget]
|
||||||
head :ok
|
head :ok
|
||||||
|
|
|
@ -7,6 +7,36 @@ class ControllerTest < ActionController::TestCase
|
||||||
@request.env['REMOTE_ADDR'] = '127.0.0.1'
|
@request.env['REMOTE_ADDR'] = '127.0.0.1'
|
||||||
end
|
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
|
test 'create' do
|
||||||
post :create, :widget => { :name => 'Flugel' }
|
post :create, :widget => { :name => 'Flugel' }
|
||||||
widget = assigns(:widget)
|
widget = assigns(:widget)
|
||||||
|
|
Loading…
Reference in New Issue