1
0
Fork 0
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:
Erol Fornoles 2017-02-18 15:58:43 +08:00
parent c1d57d74ac
commit c478c74c18
No known key found for this signature in database
GPG key ID: DF0CCD8DE581AF8B
2 changed files with 41 additions and 1 deletions

View file

@ -283,7 +283,7 @@ module ActiveRecord
fire_on = Array(options[:on]) fire_on = Array(options[:on])
assert_valid_transaction_action(fire_on) assert_valid_transaction_action(fire_on)
options[:if] = Array(options[:if]) options[:if] = Array(options[:if])
options[:if] << "transaction_include_any_action?(#{fire_on})" options[:if].unshift("transaction_include_any_action?(#{fire_on})")
end end
end end

View file

@ -551,3 +551,43 @@ class TransactionEnrollmentCallbacksTest < ActiveRecord::TestCase
assert_equal [:rollback], @topic.history assert_equal [:rollback], @topic.history
end end
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