Skip to content

Commit

Permalink
Merge remote branch 'benstein/master'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/after_commit/connection_adapters.rb
  • Loading branch information
jasoncodes committed Apr 28, 2010
2 parents fdef057 + 2261f4d commit 60b18cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/after_commit/connection_adapters.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions test/after_commit_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Expand Up @@ -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)");

Expand Down

0 comments on commit 60b18cd

Please sign in to comment.