Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix nested transactions bug [Closes #1]

  • Loading branch information...
commit a59386e31939d64a5c0f78b8732a29499ac97fae 1 parent c0c9109
Bogdan Gusiev authored October 14, 2010
16  lib/ar_after_transaction.rb
@@ -22,8 +22,8 @@ def transaction_with_callbacks(&block)
22 22
       raise
23 23
     ensure
24 24
       unless transactions_open?
25  
-        execute_after_transaction_callbacks if clean
26  
-        clear_transaction_callbacks
  25
+        callbacks = get_after_transaction_callbacks
  26
+        execute_callbacks(callbacks) if clean
27 27
       end
28 28
     end
29 29
 
@@ -45,12 +45,14 @@ def normally_open_transactions
45 45
       Rails.env.test? ? 1 : 0
46 46
     end
47 47
 
48  
-    def execute_after_transaction_callbacks
49  
-      @@after_transaction_hooks.each { |hook| hook.call }
  48
+    def execute_callbacks(callbacks)
  49
+      callbacks.each { |hook| hook.call }
50 50
     end
51 51
 
52  
-    def clear_transaction_callbacks
53  
-      @@after_transaction_hooks.clear
  52
+    def get_after_transaction_callbacks
  53
+      result = @@after_transaction_hooks
  54
+      @@after_transaction_hooks = []
  55
+      result
54 56
     end
55 57
   end
56 58
 
@@ -59,4 +61,4 @@ def after_transaction(&block)
59 61
   end
60 62
 end
61 63
 
62  
-ActiveRecord::Base.send(:include, ARAfterTransaction)
  64
+ActiveRecord::Base.send(:include, ARAfterTransaction)
5  spec/ar_after_transaction_spec.rb
@@ -15,6 +15,9 @@ def do_it
15 15
 
16 16
   def do_after
17 17
     after_transaction do
  18
+      ActiveRecord::Base.transaction do
  19
+        # nested transaction should not cause infinitive recursion
  20
+      end
18 21
       self.class.test_stack << :after
19 22
     end
20 23
   end
@@ -85,4 +88,4 @@ def oops
85 88
     }.should raise_error(AnExpectedError)
86 89
     User.test_stack.should == [:normal, :normal]
87 90
   end
88  
-end
  91
+end

0 notes on commit a59386e

Please sign in to comment.
Something went wrong with that request. Please try again.