diff options
-rw-r--r-- | lib/acts_as_versioned.rb | 8 | ||||
-rw-r--r-- | test/versioned_test.rb | 25 |
2 files changed, 30 insertions, 3 deletions
diff --git a/lib/acts_as_versioned.rb b/lib/acts_as_versioned.rb index 5a0e6dc62..e7a8ce780 100644 --- a/lib/acts_as_versioned.rb +++ b/lib/acts_as_versioned.rb @@ -471,9 +471,11 @@ module ActiveRecord #:nodoc: def without_locking(&block) current = ActiveRecord::Base.lock_optimistically ActiveRecord::Base.lock_optimistically = false if current - result = block.call - ActiveRecord::Base.lock_optimistically = true if current - result + begin + block.call + ensure + ActiveRecord::Base.lock_optimistically = true if current + end end end end diff --git a/test/versioned_test.rb b/test/versioned_test.rb index c0c8e514d..9c4f2c91f 100644 --- a/test/versioned_test.rb +++ b/test/versioned_test.rb @@ -342,4 +342,29 @@ class VersionedTest < Test::Unit::TestCase assert landmarks(:washington).changed? assert !landmarks(:washington).altered? end + + def test_without_locking_temporarily_disables_optimistic_locking + enabled1 = false + block_called = false + + ActiveRecord::Base.lock_optimistically = true + LockedPage.without_locking do + enabled1 = ActiveRecord::Base.lock_optimistically + block_called = true + end + enabled2 = ActiveRecord::Base.lock_optimistically + + assert block_called + assert !enabled1 + assert enabled2 + end + + def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception + assert_raises(RuntimeError) do + LockedPage.without_locking do + raise RuntimeError, "oh noes" + end + end + assert ActiveRecord::Base.lock_optimistically + end end
\ No newline at end of file |