Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote branch 'benstein/master'

Conflicts:
	lib/after_commit/connection_adapters.rb
  • Loading branch information...
commit 60b18cda7f9ed989a0b30fb95e4a1a0a68432d9f 2 parents fdef057 + 2261f4d
@jasoncodes jasoncodes authored
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
@@ -109,7 +111,7 @@ def trigger_after_commit_callbacks
record.send :callback, :after_commit unless AfterCommit.destroyed_records(self).include? record
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
@@ -52,6 +52,17 @@ def do_after_commit
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
@@ -159,6 +170,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.