diff --git a/lib/paper_trail.rb b/lib/paper_trail.rb index a0ab728b..1ddb4a53 100644 --- a/lib/paper_trail.rb +++ b/lib/paper_trail.rb @@ -1,3 +1,5 @@ +require 'request_store' + # Require core library Dir[File.join(File.dirname(__FILE__), 'paper_trail', '*.rb')].each do |file| require File.join('paper_trail', File.basename(file, '.rb')) @@ -118,7 +120,7 @@ module PaperTrail # Thread-safe hash to hold PaperTrail's data. # Initializing with needed default values. def self.paper_trail_store - Thread.current[:paper_trail] ||= { :request_enabled_for_controller => true } + RequestStore.store[:paper_trail] ||= { :request_enabled_for_controller => true } end # Returns PaperTrail's configuration object. diff --git a/lib/paper_trail/frameworks/sinatra.rb b/lib/paper_trail/frameworks/sinatra.rb index 7af0daaf..10477951 100644 --- a/lib/paper_trail/frameworks/sinatra.rb +++ b/lib/paper_trail/frameworks/sinatra.rb @@ -5,6 +5,7 @@ module PaperTrail # Register this module inside your Sinatra application to gain access to controller-level methods used by PaperTrail def self.registered(app) + app.use RequestStore::Middleware app.helpers self app.before { set_paper_trail_whodunnit } end diff --git a/paper_trail.gemspec b/paper_trail.gemspec index 8f53a107..1fc2cb54 100644 --- a/paper_trail.gemspec +++ b/paper_trail.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'activerecord', ['>= 3.0', '< 6.0'] s.add_dependency 'activesupport', ['>= 3.0', '< 6.0'] + s.add_dependency 'request_store', '~> 1.1.0' s.add_development_dependency 'rake', '~> 10.1.1' s.add_development_dependency 'shoulda', '~> 3.5' diff --git a/spec/requests/articles_spec.rb b/spec/requests/articles_spec.rb index 511b2353..b19f41af 100644 --- a/spec/requests/articles_spec.rb +++ b/spec/requests/articles_spec.rb @@ -9,7 +9,6 @@ describe "Articles management", :type => :request, :order => :defined do it "should not create a version" do expect(PaperTrail).to be_enabled_for_controller expect { post(articles_path, valid_params) }.to_not change(PaperTrail::Version, :count) - expect(PaperTrail).not_to be_enabled_for_controller end it "should not leak the state of the `PaperTrail.enabled_for_controller?` into the next test" do @@ -24,7 +23,6 @@ describe "Articles management", :type => :request, :order => :defined do it "should set that value as the `whodunnit`" do expect { post articles_path, valid_params }.to change(PaperTrail::Version, :count).by(1) expect(article.title).to eq('Doh') - expect(PaperTrail.whodunnit).to eq('foobar') expect(article.versions.last.whodunnit).to eq('foobar') end end diff --git a/test/functional/controller_test.rb b/test/functional/controller_test.rb index 64e15fe0..8eead732 100644 --- a/test/functional/controller_test.rb +++ b/test/functional/controller_test.rb @@ -7,8 +7,10 @@ class ControllerTest < ActionController::TestCase @request.env['REMOTE_ADDR'] = '127.0.0.1' end + # Mimick what RequestStore will do outside of the test env, since it is + # middleware, and doesn't get executed in controller / request specs teardown do - PaperTrail.enabled_for_controller = true + RequestStore.store[:paper_trail] = nil end test 'disable on create' do @@ -82,7 +84,7 @@ class ControllerTest < ActionController::TestCase @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent' post :create, :widget => { :name => 'Flugel' } assert_equal 0, assigns(:widget).versions.length - assert !PaperTrail.enabled_for_controller? + assert !PaperTrail.enabled_for_controller? assert PaperTrail.whodunnit.nil? assert PaperTrail.controller_info.nil? end