Make PaperTrail.enabled= affect all threads
Fixes https://github.com/airblade/paper_trail/issues/635
This commit is contained in:
parent
7fd529dc01
commit
68ae9f6108
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
### Breaking Changes
|
### Breaking Changes
|
||||||
|
|
||||||
|
- `PaperTrail.enabled=` now affects all threads
|
||||||
- [#556](https://github.com/airblade/paper_trail/pull/556) /
|
- [#556](https://github.com/airblade/paper_trail/pull/556) /
|
||||||
[#301](https://github.com/airblade/paper_trail/issues/301) -
|
[#301](https://github.com/airblade/paper_trail/issues/301) -
|
||||||
If you are tracking who is responsible for changes with `whodunnit`, be aware
|
If you are tracking who is responsible for changes with `whodunnit`, be aware
|
||||||
|
|
|
@ -8,6 +8,11 @@ module PaperTrail
|
||||||
attr_writer :track_associations
|
attr_writer :track_associations
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
# Variables which affect all threads, whose access is synchronized.
|
||||||
|
@mutex = Mutex.new
|
||||||
|
@enabled = true
|
||||||
|
|
||||||
|
# Variables which affect all threads, whose access is *not* synchronized.
|
||||||
@timestamp_field = :created_at
|
@timestamp_field = :created_at
|
||||||
@serializer = PaperTrail::Serializers::YAML
|
@serializer = PaperTrail::Serializers::YAML
|
||||||
end
|
end
|
||||||
|
@ -36,12 +41,11 @@ module PaperTrail
|
||||||
|
|
||||||
# Indicates whether PaperTrail is on or off. Default: true.
|
# Indicates whether PaperTrail is on or off. Default: true.
|
||||||
def enabled
|
def enabled
|
||||||
value = PaperTrail.paper_trail_store.fetch(:paper_trail_enabled, true)
|
@mutex.synchronize { !!@enabled }
|
||||||
value.nil? ? true : value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def enabled= enable
|
def enabled= enable
|
||||||
PaperTrail.paper_trail_store[:paper_trail_enabled] = enable
|
@mutex.synchronize { @enabled = enable }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,40 +13,5 @@ module PaperTrail
|
||||||
expect { described_class.new }.to raise_error(NoMethodError)
|
expect { described_class.new }.to raise_error(NoMethodError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#enabled" do
|
|
||||||
context "when paper_trail_enabled is true" do
|
|
||||||
it "returns true" do
|
|
||||||
store = double
|
|
||||||
allow(store).to receive(:fetch).
|
|
||||||
with(:paper_trail_enabled, true).
|
|
||||||
and_return(true)
|
|
||||||
allow(PaperTrail).to receive(:paper_trail_store).and_return(store)
|
|
||||||
expect(described_class.instance.enabled).to eq(true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when paper_trail_enabled is false" do
|
|
||||||
it "returns false" do
|
|
||||||
store = double
|
|
||||||
allow(store).to receive(:fetch).
|
|
||||||
with(:paper_trail_enabled, true).
|
|
||||||
and_return(false)
|
|
||||||
allow(PaperTrail).to receive(:paper_trail_store).and_return(store)
|
|
||||||
expect(described_class.instance.enabled).to eq(false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when paper_trail_enabled is nil" do
|
|
||||||
it "returns true" do
|
|
||||||
store = double
|
|
||||||
allow(store).to receive(:fetch).
|
|
||||||
with(:paper_trail_enabled, true).
|
|
||||||
and_return(nil)
|
|
||||||
allow(PaperTrail).to receive(:paper_trail_store).and_return(store)
|
|
||||||
expect(described_class.instance.enabled).to eq(true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,12 +8,13 @@ class PaperTrailTest < ActiveSupport::TestCase
|
||||||
test 'Version Number' do
|
test 'Version Number' do
|
||||||
assert PaperTrail.const_defined?(:VERSION)
|
assert PaperTrail.const_defined?(:VERSION)
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'enabled is thread-safe' do
|
context "setting enabled" do
|
||||||
Thread.new do
|
should "affect all threads" do
|
||||||
PaperTrail.enabled = false
|
Thread.new { PaperTrail.enabled = false }.join
|
||||||
end.join
|
assert_equal false, PaperTrail.enabled?
|
||||||
assert PaperTrail.enabled?
|
end
|
||||||
|
teardown { PaperTrail.enabled = true }
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'create with plain model class' do
|
test 'create with plain model class' do
|
||||||
|
|
Loading…
Reference in New Issue