Permalink
Browse files

Merge remote branch 'benstein/master'

Conflicts:
	lib/after_commit/connection_adapters.rb
  • Loading branch information...
2 parents 27287d4 + 2261f4d commit 0226f929348eafd6e7c8cff33d6a6466d86dbaba @pat committed May 12, 2010
Showing with 29 additions and 1 deletion.
  1. +3 −1 lib/after_commit/connection_adapters.rb
  2. +24 −0 test/after_commit_test.rb
  3. +2 −0 test/test_helper.rb
View
4 lib/after_commit/connection_adapters.rb
@@ -49,6 +49,7 @@ def commit_db_transaction_with_callback
# callback for each record that failed to be committed.
def rollback_db_transaction_with_callback
return if @disable_rollback
+ increment_transaction_pointer
begin
result = nil
trigger_before_rollback_callbacks
@@ -57,6 +58,7 @@ def rollback_db_transaction_with_callback
result
ensure
AfterCommit.cleanup(self)
+ decrement_transaction_pointer
end
end
alias_method_chain :rollback_db_transaction, :callback
@@ -108,7 +110,7 @@ def trigger_after_commit_callbacks
record.send :callback, :after_commit
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)");

0 comments on commit 0226f92

Please sign in to comment.