From 7edb10fe682d5fa9802b2b5355b1073444042fc4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 14 Mar 2005 00:04:14 +0000 Subject: Added that all types of after_find/after_initialized callbacks are triggered if the explicit implementation is present, not only the explicit implementation itself git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@898 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/callbacks.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 2e0bfd3e03..f92e38a0d0 100755 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -151,8 +151,8 @@ module ActiveRecord # # Because after_find and after_initialize is called for each object instantiated found by a finder, such as Base.find_all, we've had # to implement a simple performance constraint (50% more speed on a simple test case). Unlike all the other callbacks, after_find and - # after_initialize can only be declared using an explicit implementation. So using the inheritable callback queue for after_find and - # after_initialize won't work. + # after_initialize will only be run if an explicit implementation is defined (def after_find). In that case, all of the + # callback types will be called. # # == Cancelling callbacks # @@ -209,8 +209,19 @@ module ActiveRecord module ClassMethods #:nodoc: def instantiate_with_callbacks(record) object = instantiate_without_callbacks(record) - object.send(:invoke_and_notify, :after_find) - object.send(:invoke_and_notify, :after_initialize) + + if object.send(:respond_to_without_attributes?, :after_find) + object.send(:callback, :after_find) + else + object.send(:invoke_and_notify, :after_find) + end + + if object.send(:respond_to_without_attributes?, :after_initialize) + object.send(:callback, :after_initialize) + else + object.send(:invoke_and_notify, :after_initialize) + end + object end end @@ -223,7 +234,7 @@ module ActiveRecord def initialize_with_callbacks(attributes = nil) #:nodoc: initialize_without_callbacks(attributes) result = yield self if block_given? - invoke_and_notify(:after_initialize) + respond_to_without_attributes?(:after_initialize) ? callback(:after_initialize) : invoke_and_notify(:after_initialize) result end -- cgit v1.2.3