1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Rename Request#enabled_for_controller= to #enabled=

This commit is contained in:
Jared Beck 2018-03-23 21:58:45 -04:00
parent 9c99efeef5
commit e91804afb7
12 changed files with 48 additions and 46 deletions

View file

@ -1344,14 +1344,14 @@ helper method.
# in test/test_helper.rb
def with_versioning
was_enabled = PaperTrail.enabled?
was_enabled_for_controller = PaperTrail.enabled_for_controller?
was_enabled_for_request = PaperTrail.request.enabled?
PaperTrail.enabled = true
PaperTrail.enabled_for_controller = true
PaperTrail.request.enabled = true
begin
yield
ensure
PaperTrail.enabled = was_enabled
PaperTrail.enabled_for_controller = was_enabled_for_controller
PaperTrail.request.enabled = was_enabled_for_request
end
end
```

View file

@ -72,20 +72,20 @@ module PaperTrail
def enabled_for_controller=(value)
::ActiveSupport::Deprecation.warn(
"PaperTrail.enabled_for_controller= is deprecated, " \
"use PaperTrail.request.enabled_for_controller=",
"use PaperTrail.request.enabled=",
caller(1)
)
request.enabled_for_controller = value
request.enabled = value
end
# @deprecated
def enabled_for_controller?
::ActiveSupport::Deprecation.warn(
"PaperTrail.enabled_for_controller? is deprecated, " \
"use PaperTrail.request.enabled_for_controller?",
"use PaperTrail.request.enabled?",
caller(1)
)
request.enabled_for_controller?
request.enabled?
end
# @deprecated

View file

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

View file

@ -4,7 +4,8 @@ module PaperTrail
module Rails
# Extensions to rails controllers. Provides convenient ways to pass certain
# information to the model layer, with `controller_info` and `whodunnit`.
# Also includes a convenient on/off switch, `enabled_for_controller`.
# Also includes a convenient on/off switch,
# `paper_trail_enabled_for_controller`.
module Controller
def self.included(controller)
controller.before_action(
@ -78,14 +79,14 @@ module PaperTrail
#
# @api public
def set_paper_trail_enabled_for_controller
::PaperTrail.request.enabled_for_controller = paper_trail_enabled_for_controller
::PaperTrail.request.enabled = paper_trail_enabled_for_controller
end
# Tells PaperTrail who is responsible for any changes that occur.
#
# @api public
def set_paper_trail_whodunnit
if ::PaperTrail.request.enabled_for_controller?
if ::PaperTrail.request.enabled?
::PaperTrail.request.whodunnit = user_for_paper_trail
end
end
@ -95,7 +96,7 @@ module PaperTrail
#
# @api public
def set_paper_trail_controller_info
if ::PaperTrail.request.enabled_for_controller?
if ::PaperTrail.request.enabled?
::PaperTrail.request.controller_info = info_for_paper_trail
end
end

View file

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

View file

@ -113,7 +113,7 @@ module PaperTrail
# @api private
def enabled?
PaperTrail.enabled? &&
PaperTrail.request.enabled_for_controller? &&
PaperTrail.request.enabled? &&
PaperTrail.request.enabled_for_model?(@record.class)
end

View file

@ -57,16 +57,15 @@ module PaperTrail
# Sets whether PaperTrail is enabled or disabled for the current request.
# @api public
def enabled_for_controller=(value)
store[:request_enabled_for_controller] = value
def enabled=(value)
store[:enabled] = value
end
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
#
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
# @api public
def enabled_for_controller?
!!store[:request_enabled_for_controller]
def enabled?
!!store[:enabled]
end
# Sets whether PaperTrail is enabled or disabled for this model in the
@ -156,7 +155,7 @@ module PaperTrail
# @api private
def store
RequestStore.store[:paper_trail] ||= {
request_enabled_for_controller: true
enabled: true
}
end
@ -169,7 +168,7 @@ module PaperTrail
case k
when :controller_info,
/enabled_for_/,
:request_enabled_for_controller,
:enabled,
:whodunnit
next
when :transaction_id

View file

@ -3,16 +3,16 @@
require "spec_helper"
RSpec.describe ArticlesController, type: :controller do
describe "PaperTrail.request.enabled_for_controller?" do
describe "PaperTrail.request.enabled?" do
context "PaperTrail.enabled? == true" do
before { PaperTrail.enabled = true }
it "returns true" do
assert PaperTrail.enabled?
expect(PaperTrail.enabled?).to eq(true)
post :create, params_wrapper(article: { title: "Doh", content: FFaker::Lorem.sentence })
expect(assigns(:article)).not_to be_nil
assert PaperTrail.request.enabled_for_controller?
assert_equal 1, assigns(:article).versions.length
expect(PaperTrail.request.enabled?).to eq(true)
expect(assigns(:article).versions.length).to eq(1)
end
after { PaperTrail.enabled = false }
@ -20,10 +20,10 @@ RSpec.describe ArticlesController, type: :controller do
context "PaperTrail.enabled? == false" do
it "returns false" do
assert !PaperTrail.enabled?
expect(PaperTrail.enabled?).to eq(false)
post :create, params_wrapper(article: { title: "Doh", content: FFaker::Lorem.sentence })
assert !PaperTrail.request.enabled_for_controller?
assert_equal 0, assigns(:article).versions.length
expect(PaperTrail.request.enabled?).to eq(false)
expect(assigns(:article).versions.length).to eq(0)
end
end
end

View file

@ -20,7 +20,7 @@ RSpec.describe WidgetsController, type: :controller, versioning: true do
it "controller metadata methods should get evaluated" do
request.env["HTTP_USER_AGENT"] = "User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
expect(PaperTrail.request.enabled_for_controller?).to(eq(true))
expect(PaperTrail.request.enabled?).to eq(true)
expect(PaperTrail.request.whodunnit).to(eq(153))
expect(PaperTrail.request.controller_info.present?).to(eq(true))
expect(PaperTrail.request.controller_info.keys.include?(:ip)).to(eq(true))
@ -33,7 +33,7 @@ RSpec.describe WidgetsController, type: :controller, versioning: true do
request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
expect(assigns(:widget).versions.length).to(eq(0))
expect(PaperTrail.request).not_to be_enabled_for_controller
expect(PaperTrail.request.enabled?).to eq(false)
expect(PaperTrail.request.whodunnit).to be_nil
expect(PaperTrail.request.controller_info).to eq({})
end

View file

@ -35,22 +35,22 @@ module PaperTrail
end
end
describe ".enabled_for_controller?" do
describe ".enabled?" do
it "returns true" do
expect(PaperTrail.request.enabled_for_controller?).to eq(true)
expect(PaperTrail.request.enabled?).to eq(true)
end
end
describe ".enabled_for_controller=" do
it "sets enabled_for_controller? to true" do
PaperTrail.request.enabled_for_controller = true
expect(PaperTrail.request.enabled_for_controller?).to eq(true)
PaperTrail.request.enabled_for_controller = false
expect(PaperTrail.request.enabled_for_controller?).to eq(false)
describe ".enabled=" do
it "sets enabled? to true" do
PaperTrail.request.enabled = true
expect(PaperTrail.request.enabled?).to eq(true)
PaperTrail.request.enabled = false
expect(PaperTrail.request.enabled?).to eq(false)
end
after do
PaperTrail.request.enabled_for_controller = true
PaperTrail.request.enabled = true
end
end

View file

@ -115,7 +115,6 @@ RSpec.describe PaperTrail do
end
it_behaves_like "it delegates to request", :clear_transaction_id, nil
it_behaves_like "it delegates to request", :enabled_for_controller=, [true]
it_behaves_like "it delegates to request", :enabled_for_model, [Widget, true]
it_behaves_like "it delegates to request", :enabled_for_model?, [Widget]
it_behaves_like "it delegates to request", :whodunnit=, [:some_whodunnit]
@ -125,6 +124,14 @@ RSpec.describe PaperTrail do
it_behaves_like "it delegates to request", :transaction_id=, 123
it_behaves_like "it delegates to request", :transaction_id, nil
describe "#enabled_for_controller=" do
it "is deprecated" do
allow(::PaperTrail.request).to receive(:enabled=)
::PaperTrail.enabled_for_controller = true
expect(::PaperTrail.request).to have_received(:enabled=).with(true)
end
end
describe "whodunnit with block" do
it "delegates to request" do
allow(described_class.request).to receive(:with)

View file

@ -9,16 +9,11 @@ RSpec.describe "Articles management", type: :request, order: :defined do
specify { expect(PaperTrail).not_to be_enabled }
it "does not create a version" do
expect(PaperTrail.request).to be_enabled_for_controller
expect(PaperTrail.request).to be_enabled
expect {
post articles_path, params_wrapper(valid_params)
}.not_to change(PaperTrail::Version, :count)
end
it "does not leak the state of the `PaperTrail.request.enabled_for_controller?` \
into the next test" do
expect(PaperTrail.request).to be_enabled_for_controller
end
end
with_versioning do