mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20887 from tgxworld/ar_callbacks
Revert "Revert "Reduce allocations when running AR callbacks.""
This commit is contained in:
commit
64c1264419
8 changed files with 32 additions and 23 deletions
|
@ -404,7 +404,7 @@ module ActiveModel
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def run_validations! #:nodoc:
|
def run_validations! #:nodoc:
|
||||||
run_callbacks :validate
|
_run_validate_callbacks
|
||||||
errors.empty?
|
errors.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -109,7 +109,7 @@ module ActiveModel
|
||||||
|
|
||||||
# Overwrite run validations to include callbacks.
|
# Overwrite run validations to include callbacks.
|
||||||
def run_validations! #:nodoc:
|
def run_validations! #:nodoc:
|
||||||
run_callbacks(:validation) { super }
|
_run_validation_callbacks { super }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -133,7 +133,7 @@ module ActiveRecord
|
||||||
if scope.klass.primary_key
|
if scope.klass.primary_key
|
||||||
count = scope.destroy_all.length
|
count = scope.destroy_all.length
|
||||||
else
|
else
|
||||||
scope.each { |record| record.run_callbacks :destroy }
|
scope.each(&:_run_destroy_callbacks)
|
||||||
|
|
||||||
arel = scope.arel
|
arel = scope.arel
|
||||||
|
|
||||||
|
|
|
@ -289,24 +289,25 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy #:nodoc:
|
def destroy #:nodoc:
|
||||||
run_callbacks(:destroy) { super }
|
_run_destroy_callbacks { super }
|
||||||
end
|
end
|
||||||
|
|
||||||
def touch(*) #:nodoc:
|
def touch(*) #:nodoc:
|
||||||
run_callbacks(:touch) { super }
|
_run_touch_callbacks { super }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_or_update(*) #:nodoc:
|
def create_or_update(*) #:nodoc:
|
||||||
run_callbacks(:save) { super }
|
_run_save_callbacks { super }
|
||||||
end
|
end
|
||||||
|
|
||||||
def _create_record #:nodoc:
|
def _create_record #:nodoc:
|
||||||
run_callbacks(:create) { super }
|
_run_create_callbacks { super }
|
||||||
end
|
end
|
||||||
|
|
||||||
def _update_record(*) #:nodoc:
|
def _update_record(*) #:nodoc:
|
||||||
run_callbacks(:update) { super }
|
_run_update_callbacks { super }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -508,7 +508,7 @@ module ActiveRecord
|
||||||
synchronize do
|
synchronize do
|
||||||
remove_connection_from_thread_cache conn
|
remove_connection_from_thread_cache conn
|
||||||
|
|
||||||
conn.run_callbacks :checkin do
|
conn._run_checkin_callbacks do
|
||||||
conn.expire
|
conn.expire
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -764,7 +764,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def checkout_and_verify(c)
|
def checkout_and_verify(c)
|
||||||
c.run_callbacks :checkout do
|
c._run_checkout_callbacks do
|
||||||
c.verify!
|
c.verify!
|
||||||
end
|
end
|
||||||
c
|
c
|
||||||
|
|
|
@ -303,7 +303,7 @@ module ActiveRecord
|
||||||
assign_attributes(attributes) if attributes
|
assign_attributes(attributes) if attributes
|
||||||
|
|
||||||
yield self if block_given?
|
yield self if block_given?
|
||||||
run_callbacks :initialize
|
_run_initialize_callbacks
|
||||||
end
|
end
|
||||||
|
|
||||||
# Initialize an empty model object from +coder+. +coder+ should be
|
# Initialize an empty model object from +coder+. +coder+ should be
|
||||||
|
@ -330,8 +330,8 @@ module ActiveRecord
|
||||||
|
|
||||||
self.class.define_attribute_methods
|
self.class.define_attribute_methods
|
||||||
|
|
||||||
run_callbacks :find
|
_run_find_callbacks
|
||||||
run_callbacks :initialize
|
_run_initialize_callbacks
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
@ -367,7 +367,7 @@ module ActiveRecord
|
||||||
@attributes = @attributes.dup
|
@attributes = @attributes.dup
|
||||||
@attributes.reset(self.class.primary_key)
|
@attributes.reset(self.class.primary_key)
|
||||||
|
|
||||||
run_callbacks(:initialize)
|
_run_initialize_callbacks
|
||||||
|
|
||||||
@new_record = true
|
@new_record = true
|
||||||
@destroyed = false
|
@destroyed = false
|
||||||
|
|
|
@ -319,8 +319,8 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def before_committed! # :nodoc:
|
def before_committed! # :nodoc:
|
||||||
run_callbacks :before_commit_without_transaction_enrollment
|
_run_before_commit_without_transaction_enrollment_callbacks
|
||||||
run_callbacks :before_commit
|
_run_before_commit_callbacks
|
||||||
end
|
end
|
||||||
|
|
||||||
# Call the +after_commit+ callbacks.
|
# Call the +after_commit+ callbacks.
|
||||||
|
@ -329,8 +329,8 @@ module ActiveRecord
|
||||||
# but call it after the commit of a destroyed object.
|
# but call it after the commit of a destroyed object.
|
||||||
def committed!(should_run_callbacks: true) #:nodoc:
|
def committed!(should_run_callbacks: true) #:nodoc:
|
||||||
if should_run_callbacks && destroyed? || persisted?
|
if should_run_callbacks && destroyed? || persisted?
|
||||||
run_callbacks :commit_without_transaction_enrollment
|
_run_commit_without_transaction_enrollment_callbacks
|
||||||
run_callbacks :commit
|
_run_commit_callbacks
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
force_clear_transaction_record_state
|
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.
|
# 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:
|
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
|
||||||
if should_run_callbacks
|
if should_run_callbacks
|
||||||
run_callbacks :rollback
|
_run_rollback_callbacks
|
||||||
run_callbacks :rollback_without_transaction_enrollment
|
_run_rollback_without_transaction_enrollment_callbacks
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
restore_transaction_record_state(force_restore_state)
|
restore_transaction_record_state(force_restore_state)
|
||||||
|
|
|
@ -80,8 +80,12 @@ module ActiveSupport
|
||||||
# save
|
# save
|
||||||
# end
|
# end
|
||||||
def run_callbacks(kind, &block)
|
def run_callbacks(kind, &block)
|
||||||
callbacks = send("_#{kind}_callbacks")
|
send "_run_#{kind}_callbacks", &block
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def __run_callbacks__(callbacks, &block)
|
||||||
if callbacks.empty?
|
if callbacks.empty?
|
||||||
yield if block_given?
|
yield if block_given?
|
||||||
else
|
else
|
||||||
|
@ -91,8 +95,6 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# A hook invoked every time a before callback is halted.
|
# A hook invoked every time a before callback is halted.
|
||||||
# This can be overridden in AS::Callback implementors in order
|
# This can be overridden in AS::Callback implementors in order
|
||||||
# to provide better debugging/logging.
|
# to provide better debugging/logging.
|
||||||
|
@ -806,6 +808,12 @@ module ActiveSupport
|
||||||
names.each do |name|
|
names.each do |name|
|
||||||
class_attribute "_#{name}_callbacks"
|
class_attribute "_#{name}_callbacks"
|
||||||
set_callbacks name, CallbackChain.new(name, options)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue