From 55efae2387b0542e6f943f8a3d50290c7b06ae80 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 31 Aug 2007 01:56:39 +0000 Subject: Performance: absorb instantiate and initialize_with_callbacks into the Base methods. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7380 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 13 ++++++++++- activerecord/lib/active_record/callbacks.rb | 36 +++-------------------------- 3 files changed, 17 insertions(+), 34 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index ddbb25e9de..9660c9641f 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Performance: absorb instantiate and initialize_with_callbacks into the Base methods. [Jeremy Kemper] + * Fixed that eager loading queries and with_scope should respect the :group option [DHH] * Improve performance and functionality of the postgresql adapter. Closes #8049 [roderickvd] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 44cbbc97cc..b922b869bd 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1095,6 +1095,15 @@ module ActiveRecord #:nodoc: object.instance_variable_set("@attributes", record) object.instance_variable_set("@attributes_cache", Hash.new) + + if object.respond_to_without_attributes?(:after_find) + object.send(:callback, :after_find) + end + + if object.respond_to_without_attributes?(:after_initialize) + object.send(:callback, :after_initialize) + end + object end @@ -1649,7 +1658,9 @@ module ActiveRecord #:nodoc: ensure_proper_type self.attributes = attributes unless attributes.nil? self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) - yield self if block_given? + result = yield self if block_given? + callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) + result end # A model instance's primary key is always available as model.id diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 83c54680b5..631eb911fc 100755 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -177,16 +177,10 @@ module ActiveRecord ) def self.included(base) #:nodoc: - base.extend(ClassMethods) - base.class_eval do - class << self - include Observable - alias_method_chain :instantiate, :callbacks - end + base.extend Observable - [:initialize, :create_or_update, :valid?, :create, :update, :destroy].each do |method| - alias_method_chain method, :callbacks - end + [:create_or_update, :valid?, :create, :update, :destroy].each do |method| + base.send :alias_method_chain, method, :callbacks end CALLBACKS.each do |method| @@ -199,36 +193,12 @@ module ActiveRecord end end - module ClassMethods #:nodoc: - def instantiate_with_callbacks(record) - object = instantiate_without_callbacks(record) - - if object.respond_to_without_attributes?(:after_find) - object.send(:callback, :after_find) - end - - if object.respond_to_without_attributes?(:after_initialize) - object.send(:callback, :after_initialize) - end - - object - end - end - # Is called when the object was instantiated by one of the finders, like Base.find. #def after_find() end # Is called after the object has been instantiated by a call to Base.new. #def after_initialize() end - def initialize_with_callbacks(attributes = nil) #:nodoc: - initialize_without_callbacks(attributes) - result = yield self if block_given? - callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) - result - end - private :initialize_with_callbacks - # Is called _before_ Base.save (regardless of whether it's a +create+ or +update+ save). def before_save() end -- cgit v1.2.3