Skip to content

Commit

Permalink
Make Config.enabled backed by a thread variable #539
Browse files Browse the repository at this point in the history
  • Loading branch information
stormsilver committed May 26, 2015
1 parent 74d8e69 commit 1ec7baa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
module PaperTrail
class Config
include Singleton
attr_accessor :enabled, :timestamp_field, :serializer, :version_limit
attr_accessor :timestamp_field, :serializer, :version_limit
attr_reader :serialized_attributes
attr_writer :track_associations

def initialize
@enabled = true # Indicates whether PaperTrail is on or off.
@timestamp_field = :created_at
@serializer = PaperTrail::Serializers::YAML

Expand All @@ -33,5 +32,14 @@ def track_associations
@track_associations ||= PaperTrail::VersionAssociation.table_exists?
end
alias_method :track_associations?, :track_associations

# Indicates whether PaperTrail is on or off.
def enabled
Thread.current[:paper_trail_enabled].nil? || Thread.current[:paper_trail_enabled]
end

def enabled= enable
Thread.current[:paper_trail_enabled] = enable
end
end
end
7 changes: 7 additions & 0 deletions test/paper_trail_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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?
end

test 'create with plain model class' do
widget = Widget.create
Expand Down

0 comments on commit 1ec7baa

Please sign in to comment.