Permalink
Browse files

Performance: absorb instantiate and initialize_with_callbacks into th…

…e Base methods.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7380 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 35ade47 commit 55efae2387b0542e6f943f8a3d50290c7b06ae80 @jeremy jeremy committed Aug 31, 2007
Showing with 17 additions and 34 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +12 −1 activerecord/lib/active_record/base.rb
  3. +3 −33 activerecord/lib/active_record/callbacks.rb
View
@@ -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]
@@ -1095,6 +1095,15 @@ def instantiate(record)
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 @@ def initialize(attributes = nil)
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
@@ -177,16 +177,10 @@ module Callbacks
)
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 @@ def self.#{method}(*callbacks, &block)
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 <tt>Base.find</tt>.
#def after_find() end
# Is called after the object has been instantiated by a call to <tt>Base.new</tt>.
#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_ <tt>Base.save</tt> (regardless of whether it's a +create+ or +update+ save).
def before_save() end

0 comments on commit 55efae2

Please sign in to comment.