Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix nested transactions bug [Closes #1]

  • Loading branch information...
commit a59386e31939d64a5c0f78b8732a29499ac97fae 1 parent c0c9109
@bogdan bogdan authored
Showing with 13 additions and 8 deletions.
  1. +9 −7 lib/ar_after_transaction.rb
  2. +4 −1 spec/ar_after_transaction_spec.rb
View
16 lib/ar_after_transaction.rb
@@ -22,8 +22,8 @@ def transaction_with_callbacks(&block)
raise
ensure
unless transactions_open?
- execute_after_transaction_callbacks if clean
- clear_transaction_callbacks
+ callbacks = get_after_transaction_callbacks
+ execute_callbacks(callbacks) if clean
end
end
@@ -45,12 +45,14 @@ def normally_open_transactions
Rails.env.test? ? 1 : 0
end
- def execute_after_transaction_callbacks
- @@after_transaction_hooks.each { |hook| hook.call }
+ def execute_callbacks(callbacks)
+ callbacks.each { |hook| hook.call }
end
- def clear_transaction_callbacks
- @@after_transaction_hooks.clear
+ def get_after_transaction_callbacks
+ result = @@after_transaction_hooks
+ @@after_transaction_hooks = []
+ result
end
end
@@ -59,4 +61,4 @@ def after_transaction(&block)
end
end
-ActiveRecord::Base.send(:include, ARAfterTransaction)
+ActiveRecord::Base.send(:include, ARAfterTransaction)
View
5 spec/ar_after_transaction_spec.rb
@@ -15,6 +15,9 @@ def do_it
def do_after
after_transaction do
+ ActiveRecord::Base.transaction do
+ # nested transaction should not cause infinitive recursion
+ end
self.class.test_stack << :after
end
end
@@ -85,4 +88,4 @@ def oops
}.should raise_error(AnExpectedError)
User.test_stack.should == [:normal, :normal]
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.