Permalink
Browse files

Thread safety for #without_versioning

Conflicts:
	lib/paper_trail/has_paper_trail.rb
  • Loading branch information...
1 parent 4024ab1 commit 95a50ae6b8f50b39aef149d0db72188357876125 @dwbutler dwbutler committed with batter Feb 11, 2014
Showing with 44 additions and 11 deletions.
  1. +10 −0 lib/paper_trail.rb
  2. +9 −8 lib/paper_trail/has_paper_trail.rb
  3. +23 −1 test/functional/thread_safety_test.rb
  4. +2 −2 test/unit/model_test.rb
View
@@ -32,6 +32,16 @@ def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
end
+ # Sets whether PaperTrail is enabled or disabled for this model in the current request.
+ def self.enabled_for_model(model, value)
+ paper_trail_store[:"request_enabled_for_#{model}"] = value
+ end
+
+ # Returns `true` if PaperTrail is enabled for this model in the current request, `false` otherwise.
+ def self.enabled_for_model?(model)
+ !!paper_trail_store.fetch(:"request_enabled_for_#{model}", true)
+ end
+
# Set the field which records when a version was created.
def self.timestamp_field=(field_name)
PaperTrail.config.timestamp_field = field_name
@@ -54,9 +54,6 @@ def has_paper_trail(options = {})
paper_trail_options[:meta] ||= {}
- class_attribute :paper_trail_enabled_for_model
- self.paper_trail_enabled_for_model = true
-
class_attribute :versions_association_name
self.versions_association_name = options[:versions] || :versions
@@ -81,7 +78,7 @@ def has_paper_trail(options = {})
# Switches PaperTrail off for this class.
def paper_trail_off!
- self.paper_trail_enabled_for_model = false
+ PaperTrail.enabled_for_model(self, false)
end
def paper_trail_off
@@ -91,14 +88,18 @@ def paper_trail_off
# Switches PaperTrail on for this class.
def paper_trail_on!
- self.paper_trail_enabled_for_model = true
+ PaperTrail.enabled_for_model(self, true)
end
- def paper_trail_on
+ def paper_trail_on
warn "DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1"
self.paper_trail_on!
end
+ def paper_trail_enabled_for_model?
+ PaperTrail.enabled_for_model?(self)
+ end
+
def paper_trail_version_class
@paper_trail_version_class ||= version_class_name.constantize
end
@@ -204,7 +205,7 @@ def next_version
# Executes the given method or block without creating a new version.
def without_versioning(method = nil)
- paper_trail_was_enabled = self.paper_trail_enabled_for_model
+ paper_trail_was_enabled = self.class.paper_trail_enabled_for_model?
self.class.paper_trail_off!
method ? method.to_proc.call(self) : yield
ensure
@@ -333,7 +334,7 @@ def changed_and_not_ignored
end
def paper_trail_switched_on?
- PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.paper_trail_enabled_for_model
+ PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.class.paper_trail_enabled_for_model?
end
def save_version?
@@ -1,7 +1,7 @@
require 'test_helper'
class ThreadSafetyTest < ActionController::TestCase
- test "be thread safe" do
+ test "be thread safe when using #set_paper_trail_whodunnit" do
blocked = true
slow_thread = Thread.new do
@@ -23,4 +23,26 @@ class ThreadSafetyTest < ActionController::TestCase
assert_not_equal slow_thread.value, fast_thread.value
end
+
+ test "be thread safe when using #without_versioning" do
+ enabled = nil
+
+ slow_thread = Thread.new do
+ Widget.new.without_versioning do
+ sleep(0.01)
+ enabled = Widget.paper_trail_enabled_for_model?
+ sleep(0.01)
+ end
+ enabled
+ end
+
+ fast_thread = Thread.new do
+ sleep(0.005)
+ Widget.paper_trail_enabled_for_model?
+ end
+
+ assert_not_equal slow_thread.value, fast_thread.value
+ assert Widget.paper_trail_enabled_for_model?
+ assert PaperTrail.enabled_for_model?(Widget)
+ end
end
View
@@ -495,7 +495,7 @@ def without(&block)
context 'when destroyed "without versioning"' do
should 'leave paper trail off after call' do
@widget.without_versioning :destroy
- assert !Widget.paper_trail_enabled_for_model
+ assert !Widget.paper_trail_enabled_for_model?
end
end
@@ -522,7 +522,7 @@ def without(&block)
end
should 'enable paper trail after call' do
- assert Widget.paper_trail_enabled_for_model
+ assert Widget.paper_trail_enabled_for_model?
end
end
end

0 comments on commit 95a50ae

Please sign in to comment.