Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup callbacks from the proper transaction

  • Loading branch information...
commit 2261f4d03609ead0eb76e6e7a767226ff227e3ec 1 parent 6fe6463
@benstein benstein authored
View
4 lib/after_commit/connection_adapters.rb
@@ -38,6 +38,7 @@ def commit_db_transaction_with_callback
# should recieve the after_commit callback, but do fire the after_rollback
# callback for each record that failed to be committed.
def rollback_db_transaction_with_callback
+ increment_transaction_pointer
begin
result = nil
trigger_before_rollback_callbacks
@@ -46,6 +47,7 @@ def rollback_db_transaction_with_callback
result
ensure
AfterCommit.cleanup(self)
+ decrement_transaction_pointer
end
end
alias_method_chain :rollback_db_transaction, :callback
@@ -105,7 +107,7 @@ def trigger_after_commit_callbacks
end
end
end
-
+
def trigger_after_commit_on_create_callbacks
# Trigger the after_commit_on_create callback for each of the committed
# records.
View
24 test/after_commit_test.rb
@@ -39,6 +39,17 @@ def do_after_destroy
end
end
+class CountingRecord < ActiveRecord::Base
+ attr_accessor :after_commit_on_create_called
+ cattr_accessor :counter
+ @@counter=0
+
+ after_commit_on_create :do_after_create
+ def do_after_create
+ @@counter+=1
+ end
+end
+
class Foo < ActiveRecord::Base
attr_reader :creating
@@ -116,6 +127,19 @@ def test_after_commit_does_not_trigger_when_transaction_rolls_back
assert_equal false, record.after_commit_called
end
+
+ def test_after_commit_does_not_trigger_when_unrelated_transaction_commits
+ begin
+ CountingRecord.transaction do
+ CountingRecord.create!
+ raise "fail"
+ end
+ rescue
+ end
+ assert_equal 0, CountingRecord.counter
+ CountingRecord.create!
+ assert_equal 1, CountingRecord.counter
+ end
def test_two_transactions_are_separate
Bar.delete_all
View
2  test/test_helper.rb
@@ -13,11 +13,13 @@
ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
begin
ActiveRecord::Base.connection.execute("drop table mock_records");
+ ActiveRecord::Base.connection.execute("drop table counting_records");
ActiveRecord::Base.connection.execute("drop table foos");
ActiveRecord::Base.connection.execute("drop table bars");
rescue
end
ActiveRecord::Base.connection.execute("create table mock_records(id int)");
+ActiveRecord::Base.connection.execute("create table counting_records(id int)");
ActiveRecord::Base.connection.execute("create table foos(id int)");
ActiveRecord::Base.connection.execute("create table bars(id int)");
Please sign in to comment.
Something went wrong with that request. Please try again.