Skip to content

Commit

Permalink
Make sure that without_locking reverts AR::Base.lock_optimistically t…
Browse files Browse the repository at this point in the history
…o its old value even if the given block raises an exception.
  • Loading branch information
FooBarWidget committed Mar 23, 2009
1 parent a42dba6 commit 1bd3d25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/acts_as_versioned.rb
Expand Up @@ -471,9 +471,11 @@ def without_revision(&block)
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
Expand Down
25 changes: 25 additions & 0 deletions test/versioned_test.rb
Expand Up @@ -342,4 +342,29 @@ def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed
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

0 comments on commit 1bd3d25

Please sign in to comment.