diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-02-28 15:23:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 15:23:46 -0500 |
commit | 6a7bd6e2e4a4f408781fdfbbfb9508b5a872e9f6 (patch) | |
tree | a464a455e4f404df206e386e2014fee52f110a3b /activerecord/test | |
parent | a00abc150b35659165c12d135b373278e1b313a8 (diff) | |
parent | c478c74c183c480398eab822f4265e06a9501d36 (diff) | |
download | rails-6a7bd6e2e4a4f408781fdfbbfb9508b5a872e9f6.tar.gz rails-6a7bd6e2e4a4f408781fdfbbfb9508b5a872e9f6.tar.bz2 rails-6a7bd6e2e4a4f408781fdfbbfb9508b5a872e9f6.zip |
Merge pull request #28063 from Erol/prioritize-callback-on-action-before-if
Do not evaluate :if arguments when :on is not satisfied for transaction callbacks
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/transaction_callbacks_test.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index 391bbe8877..eaa4dd09a9 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -551,3 +551,43 @@ class TransactionEnrollmentCallbacksTest < ActiveRecord::TestCase assert_equal [:rollback], @topic.history end end + +class CallbacksOnActionAndConditionTest < ActiveRecord::TestCase + self.use_transactional_tests = false + + class TopicWithCallbacksOnActionAndCondition < ActiveRecord::Base + self.table_name = :topics + + after_commit(on: [:create, :update], if: :run_callback?) { |record| record.history << :create_or_update } + + def clear_history + @history = [] + end + + def history + @history ||= [] + end + + def run_callback? + self.history << :run_callback? + true + end + + attr_accessor :save_before_commit_history, :update_title + end + + def test_callback_on_action_with_condition + topic = TopicWithCallbacksOnActionAndCondition.new + topic.save + assert_equal [:run_callback?, :create_or_update], topic.history + + topic.clear_history + topic.approved = true + topic.save + assert_equal [:run_callback?, :create_or_update], topic.history + + topic.clear_history + topic.destroy + assert_equal [], topic.history + end +end |