Fixing state leak issue for :enabled_for_controller? method with RSpec and Cucumber helpers. close #293

This commit is contained in:
Ben Atkins 2013-11-16 18:39:08 -05:00
parent 62a6bafdf2
commit 6a6e4d1473
7 changed files with 39 additions and 23 deletions

View File

@ -5,7 +5,6 @@ require 'paper_trail/cleaner'
# Require serializers
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each { |file| require file }
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail
extend PaperTrail::Cleaner
@ -20,18 +19,18 @@ module PaperTrail
!!PaperTrail.config.enabled
end
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
#
# See `PaperTrail::Controller#paper_trail_enabled_for_controller`.
def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
end
# Sets whether PaperTrail is enabled or disabled for the current request.
def self.enabled_for_controller=(value)
paper_trail_store[:request_enabled_for_controller] = value
end
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
#
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
end
# Set the field which records when a version was created.
def self.timestamp_field=(field_name)
PaperTrail.config.timestamp_field = field_name
@ -42,11 +41,6 @@ module PaperTrail
PaperTrail.config.timestamp_field
end
# Returns who is reponsible for any changes that occur.
def self.whodunnit
paper_trail_store[:whodunnit]
end
# Sets who is responsible for any changes that occur.
# You would normally use this in a migration or on the console,
# when working with models directly. In a controller it is set
@ -55,12 +49,9 @@ module PaperTrail
paper_trail_store[:whodunnit] = value
end
# Returns any information from the controller that you want
# PaperTrail to store.
#
# See `PaperTrail::Controller#info_for_paper_trail`.
def self.controller_info
paper_trail_store[:controller_info]
# Returns who is reponsible for any changes that occur.
def self.whodunnit
paper_trail_store[:whodunnit]
end
# Sets any information from the controller that you want PaperTrail
@ -69,6 +60,14 @@ module PaperTrail
paper_trail_store[:controller_info] = value
end
# Returns any information from the controller that you want
# PaperTrail to store.
#
# See `PaperTrail::Rails::Controller#info_for_paper_trail`.
def self.controller_info
paper_trail_store[:controller_info]
end
# Getter and Setter for PaperTrail Serializer
def self.serializer=(value)
PaperTrail.config.serializer = value
@ -98,7 +97,6 @@ module PaperTrail
def self.configure
yield config
end
end
# Ensure `ProtectedAttributes` gem gets required if it is available before the `Version` class gets loaded in

View File

@ -1,6 +1,7 @@
# before hook for Cucumber
before do
::PaperTrail.enabled = false
::PaperTrail.enabled_for_controller = true
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined? ::Rails
end

View File

@ -7,6 +7,7 @@ RSpec.configure do |config|
config.before(:each) do
::PaperTrail.enabled = false
::PaperTrail.enabled_for_controller = true
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined?(::Rails) && defined?(::RSpec::Rails)
end

View File

@ -5,7 +5,6 @@ module PaperTrail
base.send :extend, ClassMethods
end
module ClassMethods
# Declare this in your model to track every create, update, and destroy. Each version of
# the model is available in the `versions` association.

View File

@ -0,0 +1,17 @@
require 'spec_helper'
describe "Articles" do
let(:valid_params) { { :article => { :title => 'Doh', :content => Faker::Lorem.sentence } } }
context "versioning disabled" do
specify { PaperTrail.enabled?.should be_false }
it "should not create a version" do
expect { post articles_path(valid_params) }.to_not change(PaperTrail::Version, :count)
end
it "should not leak the state of the `PaperTrail.enabled_for_controlller?` into the next test" do
PaperTrail.enabled_for_controller?.should be_true
end
end
end

View File

@ -39,5 +39,5 @@ RSpec.configure do |config|
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
# config.order = 'random'
end

View File

@ -1,4 +1,4 @@
Dummy::Application.routes.draw do
resources :articles, :only => [:create]
resources :widgets
resources :widgets, :only => [:create, :update, :destroy]
end