mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #19448 from tgxworld/fix_activesupport_callbacks_clash_on_run
Fix AS::Callbacks raising an error when `:run` callback is defined.
This commit is contained in:
commit
c539cc0061
8 changed files with 24 additions and 33 deletions
|
@ -401,7 +401,7 @@ module ActiveModel
|
|||
protected
|
||||
|
||||
def run_validations! #:nodoc:
|
||||
_run_validate_callbacks
|
||||
run_callbacks :validate
|
||||
errors.empty?
|
||||
end
|
||||
|
||||
|
|
|
@ -109,7 +109,7 @@ module ActiveModel
|
|||
|
||||
# Overwrite run validations to include callbacks.
|
||||
def run_validations! #:nodoc:
|
||||
_run_validation_callbacks { super }
|
||||
run_callbacks(:validation) { super }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -135,7 +135,7 @@ module ActiveRecord
|
|||
if scope.klass.primary_key
|
||||
count = scope.destroy_all.length
|
||||
else
|
||||
scope.each(&:_run_destroy_callbacks)
|
||||
scope.each { |record| record.run_callbacks :destroy }
|
||||
|
||||
arel = scope.arel
|
||||
|
||||
|
|
|
@ -289,25 +289,24 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def destroy #:nodoc:
|
||||
_run_destroy_callbacks { super }
|
||||
run_callbacks(:destroy) { super }
|
||||
end
|
||||
|
||||
def touch(*) #:nodoc:
|
||||
_run_touch_callbacks { super }
|
||||
run_callbacks(:touch) { super }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_or_update(*) #:nodoc:
|
||||
_run_save_callbacks { super }
|
||||
run_callbacks(:save) { super }
|
||||
end
|
||||
|
||||
def _create_record #:nodoc:
|
||||
_run_create_callbacks { super }
|
||||
run_callbacks(:create) { super }
|
||||
end
|
||||
|
||||
def _update_record(*) #:nodoc:
|
||||
_run_update_callbacks { super }
|
||||
run_callbacks(:update) { super }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -358,7 +358,7 @@ module ActiveRecord
|
|||
synchronize do
|
||||
owner = conn.owner
|
||||
|
||||
conn._run_checkin_callbacks do
|
||||
conn.run_callbacks :checkin do
|
||||
conn.expire
|
||||
end
|
||||
|
||||
|
@ -449,7 +449,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def checkout_and_verify(c)
|
||||
c._run_checkout_callbacks do
|
||||
c.run_callbacks :checkout do
|
||||
c.verify!
|
||||
end
|
||||
c
|
||||
|
|
|
@ -302,7 +302,7 @@ module ActiveRecord
|
|||
assign_attributes(attributes) if attributes
|
||||
|
||||
yield self if block_given?
|
||||
_run_initialize_callbacks
|
||||
run_callbacks :initialize
|
||||
end
|
||||
|
||||
# Initialize an empty model object from +coder+. +coder+ should be
|
||||
|
@ -329,8 +329,8 @@ module ActiveRecord
|
|||
|
||||
self.class.define_attribute_methods
|
||||
|
||||
_run_find_callbacks
|
||||
_run_initialize_callbacks
|
||||
run_callbacks :find
|
||||
run_callbacks :initialize
|
||||
|
||||
self
|
||||
end
|
||||
|
@ -366,7 +366,7 @@ module ActiveRecord
|
|||
@attributes = @attributes.dup
|
||||
@attributes.reset(self.class.primary_key)
|
||||
|
||||
_run_initialize_callbacks
|
||||
run_callbacks(:initialize)
|
||||
|
||||
@new_record = true
|
||||
@destroyed = false
|
||||
|
|
|
@ -319,8 +319,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def before_committed! # :nodoc:
|
||||
_run_before_commit_without_transaction_enrollment_callbacks
|
||||
_run_before_commit_callbacks
|
||||
run_callbacks :before_commit_without_transaction_enrollment
|
||||
run_callbacks :before_commit
|
||||
end
|
||||
|
||||
# Call the +after_commit+ callbacks.
|
||||
|
@ -329,8 +329,8 @@ module ActiveRecord
|
|||
# but call it after the commit of a destroyed object.
|
||||
def committed!(should_run_callbacks: true) #:nodoc:
|
||||
if should_run_callbacks && destroyed? || persisted?
|
||||
_run_commit_without_transaction_enrollment_callbacks
|
||||
_run_commit_callbacks
|
||||
run_callbacks :commit_without_transaction_enrollment
|
||||
run_callbacks :commit
|
||||
end
|
||||
ensure
|
||||
force_clear_transaction_record_state
|
||||
|
@ -340,8 +340,8 @@ module ActiveRecord
|
|||
# state should be rolled back to the beginning or just to the last savepoint.
|
||||
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
|
||||
if should_run_callbacks
|
||||
_run_rollback_without_transaction_enrollment_callbacks
|
||||
_run_rollback_callbacks
|
||||
run_callbacks :rollback
|
||||
run_callbacks :rollback_without_transaction_enrollment
|
||||
end
|
||||
ensure
|
||||
restore_transaction_record_state(force_restore_state)
|
||||
|
|
|
@ -80,14 +80,10 @@ module ActiveSupport
|
|||
# save
|
||||
# end
|
||||
def run_callbacks(kind, &block)
|
||||
send "_run_#{kind}_callbacks", &block
|
||||
end
|
||||
callbacks = send("_#{kind}_callbacks")
|
||||
|
||||
private
|
||||
|
||||
def _run_callbacks(callbacks, &block)
|
||||
if callbacks.empty?
|
||||
block.call if block
|
||||
yield if block_given?
|
||||
else
|
||||
runner = callbacks.compile
|
||||
e = Filters::Environment.new(self, false, nil, block)
|
||||
|
@ -95,6 +91,8 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A hook invoked every time a before callback is halted.
|
||||
# This can be overridden in AS::Callback implementors in order
|
||||
# to provide better debugging/logging.
|
||||
|
@ -800,12 +798,6 @@ module ActiveSupport
|
|||
names.each do |name|
|
||||
class_attribute "_#{name}_callbacks"
|
||||
set_callbacks name, CallbackChain.new(name, options)
|
||||
|
||||
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def _run_#{name}_callbacks(&block)
|
||||
_run_callbacks(_#{name}_callbacks, &block)
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue