Permalink
Browse files

Do not call before_commit and after_commit for records that were dest…

…royed in the transaction. You can now use after_commit as an equivalent for after_save.
  • Loading branch information...
1 parent 6fe6463 commit a9d6023024969394fc2a613f06c3b1d3ac71dc1a @thieso2 thieso2 committed Feb 4, 2010
Showing with 26 additions and 3 deletions.
  1. +3 −2 lib/after_commit/connection_adapters.rb
  2. +23 −1 test/after_commit_test.rb
@@ -62,7 +62,7 @@ def old_transaction_key
def trigger_before_commit_callbacks
AfterCommit.records(self).each do |record|
- record.send :callback, :before_commit
+ record.send :callback, :before_commit unless AfterCommit.destroyed_records(self).include? record
end
end
@@ -97,9 +97,10 @@ def trigger_before_rollback_callbacks
def trigger_after_commit_callbacks
# Trigger the after_commit callback for each of the committed
# records.
+
AfterCommit.records(self).each do |record|
begin
- record.send :callback, :after_commit
+ record.send :callback, :after_commit unless AfterCommit.destroyed_records(self).include? record
rescue
#
end
View
@@ -4,9 +4,12 @@ class MockRecord < ActiveRecord::Base
attr_accessor :before_commit_on_create_called
attr_accessor :before_commit_on_update_called
attr_accessor :before_commit_on_destroy_called
+ attr_accessor :before_commit_called
attr_accessor :after_commit_on_create_called
attr_accessor :after_commit_on_update_called
attr_accessor :after_commit_on_destroy_called
+ attr_accessor :after_commit_called
+
before_commit_on_create :do_before_create
def do_before_create
@@ -23,6 +26,11 @@ def do_before_destroy
self.before_commit_on_destroy_called = true
end
+ before_commit :do_before_commit
+ def do_before_commit
+ self.before_commit_called = true
+ end
+
after_commit_on_create :do_after_create
def do_after_create
self.after_commit_on_create_called = true
@@ -37,6 +45,11 @@ def do_after_update
def do_after_destroy
self.after_commit_on_destroy_called = true
end
+
+ after_commit :do_after_commit
+ def do_after_commit
+ self.after_commit_called = true
+ end
end
class Foo < ActiveRecord::Base
@@ -107,7 +120,16 @@ def test_after_commit_on_update_is_called
end
def test_after_commit_on_destroy_is_called
- assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
+ record = MockRecord.create!
+
+ # reset states set by create!
+ record.before_commit_called = false
+ record.after_commit_called = false
+ record.destroy
+
+ assert_equal true, record.after_commit_on_destroy_called
+ assert_equal false, record.after_commit_called
+ assert_equal false, record.before_commit_called
end
def test_after_commit_does_not_trigger_when_transaction_rolls_back

0 comments on commit a9d6023

Please sign in to comment.