aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/transaction_callbacks_test.rb
diff options
context:
space:
mode:
authorJames Miller <bensie@gmail.com>2013-02-13 12:27:06 -0800
committerJames Miller <bensie@gmail.com>2013-02-13 12:27:06 -0800
commitcaabed6c76eea0db99949f34c234ef1b2657392a (patch)
tree2271c8fc2593bf4afd7158165ac7d5c9a9a66a0a /activerecord/test/cases/transaction_callbacks_test.rb
parent5d58948fe72e3b0422790b8adc0fab7bbf9e6573 (diff)
downloadrails-caabed6c76eea0db99949f34c234ef1b2657392a.tar.gz
rails-caabed6c76eea0db99949f34c234ef1b2657392a.tar.bz2
rails-caabed6c76eea0db99949f34c234ef1b2657392a.zip
Don't call after_commit when creating through an association and save fails, fixes #5802
Diffstat (limited to 'activerecord/test/cases/transaction_callbacks_test.rb')
-rw-r--r--activerecord/test/cases/transaction_callbacks_test.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb
index 869892e33f..fd5651b4e0 100644
--- a/activerecord/test/cases/transaction_callbacks_test.rb
+++ b/activerecord/test/cases/transaction_callbacks_test.rb
@@ -5,9 +5,29 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
fixtures :topics
+ class ReplyWithCallbacks < ActiveRecord::Base
+ self.table_name = :topics
+
+ belongs_to :topic, foreign_key: "parent_id"
+
+ validates_presence_of :content
+
+ after_commit :do_after_commit, on: :create
+
+ def history
+ @history ||= []
+ end
+
+ def do_after_commit
+ history << :commit_on_create
+ end
+ end
+
class TopicWithCallbacks < ActiveRecord::Base
self.table_name = :topics
+ has_many :replies, class_name: "ReplyWithCallbacks", foreign_key: "parent_id"
+
after_commit{|record| record.send(:do_after_commit, nil)}
after_commit(:on => :create){|record| record.send(:do_after_commit, :create)}
after_commit(:on => :update){|record| record.send(:do_after_commit, :update)}
@@ -93,6 +113,13 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
assert_equal [:commit_on_create], @new_record.history
end
+ def test_only_call_after_commit_on_create_after_transaction_commits_for_new_record_if_create_succeeds_creating_through_association
+ topic = TopicWithCallbacks.create!(:title => "New topic", :written_on => Date.today)
+ reply = topic.replies.create
+
+ assert_equal [], reply.history
+ end
+
def test_call_after_rollback_after_transaction_rollsback
@first.after_commit_block{|r| r.history << :after_commit}
@first.after_rollback_block{|r| r.history << :after_rollback}