diff options
author | Arthur Neves <arthurnn@gmail.com> | 2015-02-12 23:06:44 -0500 |
---|---|---|
committer | Arthur Neves <arthurnn@gmail.com> | 2015-02-24 19:13:56 -0500 |
commit | 4a1bb9d0ce985fd105f930078a733601b29ef8a4 (patch) | |
tree | 3d6ac1c3e44f3646ecd0833e13ef0f6acf39ea3f /activerecord/test/cases | |
parent | 62133326df3c7edff67a2e57ae32c95bf6e8a818 (diff) | |
download | rails-4a1bb9d0ce985fd105f930078a733601b29ef8a4.tar.gz rails-4a1bb9d0ce985fd105f930078a733601b29ef8a4.tar.bz2 rails-4a1bb9d0ce985fd105f930078a733601b29ef8a4.zip |
Add transaction callbacks that wont enroll to the transaction.
Add after_commit_without_transaction_enrollment and
after_rollback_without_transaction_enrollment private callbacks so we
can create after_commit and after_rollback callbacks without having the
records automatic enrolled in the transaction.
[fixes #18904]
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/transaction_callbacks_test.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index f185cda263..60b0fd4112 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -400,3 +400,62 @@ class CallbacksOnMultipleActionsTest < ActiveRecord::TestCase assert_equal [:update_and_destroy, :create_and_destroy], topic.history end end + + +class TransactionEnrollmentCallbacksTest < ActiveRecord::TestCase + + class TopicWithoutTransactionalEnrollmentCallbacks < ActiveRecord::Base + self.table_name = :topics + + after_commit_without_transaction_enrollment { |r| r.history << :commit } + after_rollback_without_transaction_enrollment { |r| r.history << :rollback } + + def history + @history ||= [] + end + end + + def setup + @topic = TopicWithoutTransactionalEnrollmentCallbacks.create! + end + + def test_commit_dont_enroll_transaction + @topic.transaction do + @topic.content = 'foo' + @topic.save! + end + assert @topic.history.empty? + end + + def test_commit_enrollment_transaction_when_call_add + @topic.transaction do + 2.times do + @topic.content = 'foo' + @topic.save! + end + @topic.class.connection.add_transaction_record(@topic) + end + assert_equal [:commit], @topic.history + end + + def test_rollback_dont_enroll_transaction + @topic.transaction do + @topic.content = 'foo' + @topic.save! + raise ActiveRecord::Rollback + end + assert @topic.history.empty? + end + + def test_rollback_enrollment_transaction_when_call_add + @topic.transaction do + 2.times do + @topic.content = 'foo' + @topic.save! + end + @topic.class.connection.add_transaction_record(@topic) + raise ActiveRecord::Rollback + end + assert_equal [:rollback], @topic.history + end +end |