mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Prioritize checking :on
actions before :if
for transaction callbacks
This commit is contained in:
parent
c1d57d74ac
commit
c478c74c18
2 changed files with 41 additions and 1 deletions
|
@ -283,7 +283,7 @@ module ActiveRecord
|
|||
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 @@ 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
|
||||
|
|
Loading…
Reference in a new issue