提交 6a7bd6e2 编写于 作者: R Rafael França 提交者: GitHub

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
......@@ -283,7 +283,7 @@ def set_options_for_callbacks!(args, enforced_options = {})
fire_on = Array(options[:on])
assert_valid_transaction_action(fire_on)
options[:if] = Array(options[:if])
options[:if] << "transaction_include_any_action?(#{fire_on})"
options[:if].unshift("transaction_include_any_action?(#{fire_on})")
end
end
......
......@@ -551,3 +551,43 @@ def test_rollback_run_transactions_callbacks_with_explicit_enrollment
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册