Skip to content

Commit

Permalink
speeding up object instantiation by eliminating instance_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 30, 2010
1 parent ef8ce78 commit 505b532
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -884,13 +884,9 @@ def relation #:nodoc:
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
def instantiate(record)
find_sti_class(record[inheritance_column]).allocate.instance_eval do
@attributes, @attributes_cache, @previously_changed, @changed_attributes = record, {}, {}, {}
@new_record = @readonly = @destroyed = @marked_for_destruction = false
_run_find_callbacks
_run_initialize_callbacks
self
end
model = find_sti_class(record[inheritance_column]).allocate
model.init_with('attributes' => record)
model
end

def find_sti_class(type_name)
Expand Down Expand Up @@ -1416,6 +1412,24 @@ def initialize_copy(other)
populate_with_current_scope_attributes
end

# Initialize an empty model object from +coder+. +coder+ must contain
# the attributes necessary for initializing an empty model object. For
# example:
#
# class Post < ActiveRecord::Base
# end
#
# post = Post.allocate
# post.init_with('attributes' => { 'title' => 'hello world' })
# post.title # => 'hello world'
def init_with(coder)
@attributes = coder['attributes']
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
@new_record = @readonly = @destroyed = @marked_for_destruction = false
_run_find_callbacks
_run_initialize_callbacks
end

# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.
Expand Down

0 comments on commit 505b532

Please sign in to comment.