Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7380

Show
Ignore:
Timestamp:
08/31/07 01:56:39 (1 year ago)
Author:
bitsweat
Message:

Performance: absorb instantiate and initialize_with_callbacks into the Base methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r7355 r7380  
    11*SVN* 
     2 
     3* Performance: absorb instantiate and initialize_with_callbacks into the Base methods. [Jeremy Kemper] 
    24 
    35* Fixed that eager loading queries and with_scope should respect the :group option [DHH] 
  • trunk/activerecord/lib/active_record/base.rb

    r7355 r7380  
    10961096          object.instance_variable_set("@attributes", record) 
    10971097          object.instance_variable_set("@attributes_cache", Hash.new) 
     1098 
     1099          if object.respond_to_without_attributes?(:after_find) 
     1100            object.send(:callback, :after_find) 
     1101          end 
     1102 
     1103          if object.respond_to_without_attributes?(:after_initialize) 
     1104            object.send(:callback, :after_initialize) 
     1105          end 
     1106 
    10981107          object 
    10991108        end 
     
    16501659        self.attributes = attributes unless attributes.nil? 
    16511660        self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) 
    1652         yield self if block_given? 
     1661        result = yield self if block_given? 
     1662        callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) 
     1663        result 
    16531664      end 
    16541665 
  • trunk/activerecord/lib/active_record/callbacks.rb

    r7368 r7380  
    178178 
    179179    def self.included(base) #:nodoc: 
    180       base.extend(ClassMethods) 
    181       base.class_eval do 
    182         class << self 
    183           include Observable 
    184           alias_method_chain :instantiate, :callbacks 
    185         end 
    186  
    187         [:initialize, :create_or_update, :valid?, :create, :update, :destroy].each do |method| 
    188           alias_method_chain method, :callbacks 
    189         end 
     180      base.extend Observable 
     181 
     182      [:create_or_update, :valid?, :create, :update, :destroy].each do |method| 
     183        base.send :alias_method_chain, method, :callbacks 
    190184      end 
    191185 
     
    200194    end 
    201195 
    202     module ClassMethods #:nodoc: 
    203       def instantiate_with_callbacks(record) 
    204         object = instantiate_without_callbacks(record) 
    205  
    206         if object.respond_to_without_attributes?(:after_find) 
    207           object.send(:callback, :after_find) 
    208         end 
    209  
    210         if object.respond_to_without_attributes?(:after_initialize) 
    211           object.send(:callback, :after_initialize) 
    212         end 
    213  
    214         object 
    215       end 
    216     end 
    217  
    218196    # Is called when the object was instantiated by one of the finders, like <tt>Base.find</tt>. 
    219197    #def after_find() end 
     
    221199    # Is called after the object has been instantiated by a call to <tt>Base.new</tt>. 
    222200    #def after_initialize() end 
    223  
    224     def initialize_with_callbacks(attributes = nil) #:nodoc: 
    225       initialize_without_callbacks(attributes) 
    226       result = yield self if block_given? 
    227       callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) 
    228       result 
    229     end 
    230     private :initialize_with_callbacks 
    231201 
    232202    # Is called _before_ <tt>Base.save</tt> (regardless of whether it's a +create+ or +update+ save).