mirror of
https://github.com/github/rails.git
synced 2026-01-29 00:08:15 -05:00
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
This commit is contained in:
@@ -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 @@ 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
|
||||
|
||||
@@ -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 <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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user