提交 c539cc00 编写于 作者: R Rafael Mendonça França

Merge pull request #19448 from tgxworld/fix_activesupport_callbacks_clash_on_run

Fix AS::Callbacks raising an error when `:run` callback is defined.
......@@ -401,7 +401,7 @@ def validate!(context = nil)
protected
def run_validations! #:nodoc:
_run_validate_callbacks
run_callbacks :validate
errors.empty?
end
......
......@@ -109,7 +109,7 @@ def after_validation(*args, &block)
# 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 @@ def delete_records(records, method)
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 ClassMethods
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 @@ def checkin(conn)
synchronize do
owner = conn.owner
conn._run_checkin_callbacks do
conn.run_callbacks :checkin do
conn.expire
end
......@@ -449,7 +449,7 @@ def checkout_new_connection
end
def checkout_and_verify(c)
c._run_checkout_callbacks do
c.run_callbacks :checkout do
c.verify!
end
c
......
......@@ -302,7 +302,7 @@ def initialize(attributes = nil)
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 @@ def init_with(coder)
self.class.define_attribute_methods
_run_find_callbacks
_run_initialize_callbacks
run_callbacks :find
run_callbacks :initialize
self
end
......@@ -366,7 +366,7 @@ def initialize_dup(other) # :nodoc:
@attributes = @attributes.dup
@attributes.reset(self.class.primary_key)
_run_initialize_callbacks
run_callbacks(:initialize)
@new_record = true
@destroyed = false
......
......@@ -319,8 +319,8 @@ def rollback_active_record_state!
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 @@ def before_committed! # :nodoc:
# 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 @@ def committed!(should_run_callbacks: true) #:nodoc:
# 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 Callbacks
# save
# end
def run_callbacks(kind, &block)
send "_run_#{kind}_callbacks", &block
end
private
callbacks = send("_#{kind}_callbacks")
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 @@ def _run_callbacks(callbacks, &block)
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 @@ def define_callbacks(*names)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册