Skip to content

Commit

Permalink
Merge pull request #723 from airblade/the_simplest_off_switch
Browse files Browse the repository at this point in the history
Make PaperTrail.enabled= affect all threads
  • Loading branch information
jaredbeck committed Mar 5, 2016
2 parents 7fd529d + 68ae9f6 commit c2b1548
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Breaking Changes

- `PaperTrail.enabled=` now affects all threads
- [#556](https://github.com/airblade/paper_trail/pull/556) /
[#301](https://github.com/airblade/paper_trail/issues/301) -
If you are tracking who is responsible for changes with `whodunnit`, be aware
Expand Down
10 changes: 7 additions & 3 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Config
attr_writer :track_associations

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
@serializer = PaperTrail::Serializers::YAML
end
Expand Down Expand Up @@ -36,12 +41,11 @@ def track_associations

# Indicates whether PaperTrail is on or off. Default: true.
def enabled
value = PaperTrail.paper_trail_store.fetch(:paper_trail_enabled, true)
value.nil? ? true : value
@mutex.synchronize { !!@enabled }
end

def enabled= enable
PaperTrail.paper_trail_store[:paper_trail_enabled] = enable
@mutex.synchronize { @enabled = enable }
end
end
end
35 changes: 0 additions & 35 deletions spec/paper_trail/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,5 @@ module PaperTrail
expect { described_class.new }.to raise_error(NoMethodError)
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
13 changes: 7 additions & 6 deletions test/paper_trail_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class PaperTrailTest < ActiveSupport::TestCase
test 'Version Number' do
assert PaperTrail.const_defined?(:VERSION)
end

test 'enabled is thread-safe' do
Thread.new do
PaperTrail.enabled = false
end.join
assert PaperTrail.enabled?

context "setting enabled" do
should "affect all threads" do
Thread.new { PaperTrail.enabled = false }.join
assert_equal false, PaperTrail.enabled?
end
teardown { PaperTrail.enabled = true }
end

test 'create with plain model class' do
Expand Down

0 comments on commit c2b1548

Please sign in to comment.