Rename enable/disable methods to clarify intention.

This commit is contained in:
Andy Stewart 2011-04-05 09:08:54 +01:00
parent 2880476e24
commit bb79fab9e9
6 changed files with 43 additions and 39 deletions

View File

@ -2,11 +2,6 @@
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
@ -29,6 +24,7 @@ There's an excellent [Railscast on implementing Undo with Paper Trail](http://ra
* Allows you to store arbitrary model-level metadata with each version (useful for filtering versions).
* Allows you to store arbitrary controller-level information with each version, e.g. remote IP.
* Can be turned off/on per class (useful for migrations).
* Can be turned off/on per request (useful for testing with an external service).
* Can be turned off/on globally (useful for testing).
* No configuration necessary.
* Stores everything in a single database table by default (generates migration for you), or can use separate tables for separate models.
@ -489,15 +485,11 @@ For diffing two ActiveRecord objects:
Sometimes you don't want to store changes. Perhaps you are only interested in changes made by your users and don't need to store changes you make yourself in, say, a migration -- or when testing your application.
If you are about change some widgets and you don't want a paper trail of your changes, you can turn PaperTrail off like this:
You can turn PaperTrail on or off in three ways: globally, per request, or per class.
>> Widget.paper_trail_off
### Globally
And on again like this:
>> Widget.paper_trail_on
You can also disable PaperTrail for all models:
On a global level you can turn PaperTrail off like this:
>> PaperTrail.enabled = false
@ -529,6 +521,27 @@ And then use it in your tests like this:
end
end
### Per request
You can turn PaperTrail on or off per request by adding a `paper_trail_enabled_for_controller` method to your controller which returns true or false:
class ApplicationController < ActionController::Base
def paper_trail_enabled_for_controller
request.user_agent != 'Disable User-Agent'
end
end
### Per class
If you are about change some widgets and you don't want a paper trail of your changes, you can turn PaperTrail off like this:
>> Widget.paper_trail_off
And on again like this:
>> Widget.paper_trail_on
## Deleting Old Versions

View File

@ -20,19 +20,14 @@ module PaperTrail
!!PaperTrail.config.enabled
end
# Returns if PaperTrails is disabled by controller
def self.request_disabled
paper_trail_store[:request_disabled]
# Returns `true` if PaperTrail is enabled for the controller, `false` otherwise.
def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
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
def self.enabled_for_controller=(value)
paper_trail_store[:request_enabled_for_controller] = value
end
# Returns who is reponsible for any changes that occur.

View File

@ -4,7 +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
base.before_filter :set_paper_trail_enabled_for_controller
end
protected
@ -42,9 +42,9 @@ module PaperTrail
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
def set_paper_trail_enabled_for_controller
if respond_to? :paper_trail_enabled_for_controller
::PaperTrail.enabled_for_controller = paper_trail_enabled_for_controller
end
end

View File

@ -38,10 +38,8 @@ module PaperTrail
cattr_accessor :meta
self.meta = options[:meta] || {}
# Indicates whether or not PaperTrail is active for this class.
# This is independent of whether PaperTrail is globally enabled or disabled.
cattr_accessor :paper_trail_active
self.paper_trail_active = true
cattr_accessor :paper_trail_enabled_for_model
self.paper_trail_enabled_for_model = true
has_many :versions, :class_name => version_class_name, :as => :item, :order => 'created_at ASC, id ASC'
@ -52,12 +50,12 @@ module PaperTrail
# Switches PaperTrail off for this class.
def paper_trail_off
self.paper_trail_active = false
self.paper_trail_enabled_for_model = false
end
# Switches PaperTrail on for this class.
def paper_trail_on
self.paper_trail_active = true
self.paper_trail_enabled_for_model = true
end
end
@ -167,10 +165,8 @@ module PaperTrail
changed - self.class.ignore
end
# 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.request_disabled? && PaperTrail.enabled? && self.class.paper_trail_active
PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.class.paper_trail_enabled_for_model
end
end

View File

@ -1,9 +1,9 @@
class WidgetsController < ApplicationController
def paper_trail_enabled_if
def paper_trail_enabled_for_controller
request.user_agent != 'Disable User-Agent'
end
def create
@widget = Widget.create params[:widget]
head :ok

View File

@ -8,7 +8,7 @@ class ControllerTest < ActionController::TestCase
end
teardown do
PaperTrail.request_disabled = false
PaperTrail.enabled_for_controller = true
end
test 'disable on create' do