Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaperTrail.enabled now affects all threads, again #720

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
[#614](https://github.com/airblade/paper_trail/pull/614)
- Added `unversioned_attributes` option to `reify`.
[#579](https://github.com/airblade/paper_trail/pull/579)
- Added a `config.paper_trail.enabled` option for controlling PaperTrail from the environment config.
[#695](https://github.com/airblade/paper_trail/pull/695)

### Fixed

- None
- Fixed deprecation warning for Active Record after_callback / after_commit
[#695](https://github.com/airblade/paper_trail/pull/695)

## 4.0.2 (2016-01-19)

Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,20 @@ class.

### Globally

On a global level you can turn PaperTrail off like this:
On a thread global level you can turn PaperTrail off like this:

```ruby
PaperTrail.enabled = false
```

For example, you might want to disable PaperTrail in your Rails application's
You might want to disable PaperTrail in your Rails application's
test environment to speed up your tests. This will do it (note: this gets done
automatically for `RSpec` and `Cucumber`, please see the [Testing
section](#testing)):

```ruby
# in config/environments/test.rb
config.after_initialize do
PaperTrail.enabled = false
end
config.paper_trail.enabled = false
```

If you disable PaperTrail in your test environment but want to enable it for
Expand Down
6 changes: 1 addition & 5 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def enabled=(value)
# PaperTrail is enabled by default.
# @api public
def enabled?
!!PaperTrail.config.enabled
PaperTrail.config.enabled?
end

def serialized_attributes?
Expand Down Expand Up @@ -170,10 +170,6 @@ def config
end
end

ActiveSupport.on_load(:active_record) do
include PaperTrail::Model
end

# Require frameworks
require 'paper_trail/frameworks/sinatra'
if defined?(::Rails) && ActiveRecord::VERSION::STRING >= '3.2'
Expand Down
22 changes: 18 additions & 4 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 @@ -35,13 +40,22 @@ def track_associations
alias_method :track_associations?, :track_associations

# Indicates whether PaperTrail is on or off. Default: true.
# @api public
# @deprecated
def enabled
value = PaperTrail.paper_trail_store.fetch(:paper_trail_enabled, true)
value.nil? ? true : value
ActiveSupport::Deprecation.warn("Use enabled?", caller(1))
enabled?
end

# Indicates whether PaperTrail is on or off. Default: true.
# @api public
# @deprecated
def enabled?
@mutex.synchronize { !!@enabled }
end

def enabled= enable
PaperTrail.paper_trail_store[:paper_trail_enabled] = enable
def enabled= value
@mutex.synchronize { @enabled = value }
end
end
end
4 changes: 4 additions & 0 deletions lib/paper_trail/frameworks/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
# since otherwise the model(s) will get loaded in via the `Rails::Engine`.
require "paper_trail/frameworks/active_record/models/paper_trail/version_association"
require "paper_trail/frameworks/active_record/models/paper_trail/version"

ActiveSupport.on_load(:active_record) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to remain within lib/paper_trail.rb because this file only conditionally gets loaded when ActiveRecord is utilized outside of the context of rails. Or perhaps there should be an lib/paper_trail/frameworks/active_model.rb. Regardless not sure it belongs as part of this PR, seems unrelated. Happy to make a PR for that piece separately if you'd like.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see now that you also added a similar block inside of the Rails::Engine. Did this have issues too related to thread safety?

include PaperTrail::Model
end
5 changes: 5 additions & 0 deletions lib/paper_trail/frameworks/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module PaperTrail
module Rails
class Engine < ::Rails::Engine
paths['app/models'] << 'lib/paper_trail/frameworks/active_record/models'
config.paper_trail = ActiveSupport::OrderedOptions.new
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how this works? I never understood what is supposed to go in the Rails::Engine beyond code for autoloading libraries, this looks neat but I don't understand it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializer 'paper_trail.initialisation' do |app|
ActiveRecord::Base.send :include, PaperTrail::Model
PaperTrail.enabled = app.config.paper_trail.fetch(:enabled, true)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/modules/paper_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
it { is_expected.to respond_to(:config) }

it "should allow for config values to be set" do
expect(subject.config.enabled).to eq(true)
expect(subject.config).to be_enabled
subject.config.enabled = false
expect(subject.config.enabled).to eq(false)
expect(subject.config).to_not be_enabled
end

it "should accept blocks and yield the config instance" do
expect(subject.config.enabled).to eq(true)
expect(subject.config).to be_enabled
subject.config { |c| c.enabled = false }
expect(subject.config.enabled).to eq(false)
expect(subject.config).to_not be_enabled
end
end

Expand Down
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