speeding up clone_attributes, changing readonly to be initialized in def initialize

Signed-off-by: wycats <wycats@gmail.com>
This commit is contained in:
Aaron Patterson
2010-03-14 14:17:59 -07:00
committed by wycats
parent 115230e619
commit 95bd56e3cc

View File

@@ -1507,6 +1507,7 @@ module ActiveRecord #:nodoc:
@attributes = attributes_from_column_definition
@attributes_cache = {}
@new_record = true
@readonly = false
ensure_proper_type
if scope = self.class.send(:current_scoped_methods)
@@ -1571,7 +1572,7 @@ module ActiveRecord #:nodoc:
# user_path(user) # => "/users/Phusion"
def to_param
# We can't use alias_method here, because method 'id' optimizes itself on the fly.
(id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes
id && id.to_s # Be sure to stringify the id for routes
end
# Returns a cache key that can be used to identify this record.
@@ -1696,7 +1697,7 @@ module ActiveRecord #:nodoc:
# This is especially useful for boolean flags on existing records. The regular +update_attribute+ method
# in Base is replaced with this when the validations module is mixed in, which it is by default.
def update_attribute(name, value)
send(name.to_s + '=', value)
send("#{name}=", value)
save(:validate => false)
end
@@ -1913,14 +1914,14 @@ module ActiveRecord #:nodoc:
# Returns duplicated record with unfreezed attributes.
def dup
obj = super
obj.instance_variable_set('@attributes', instance_variable_get('@attributes').dup)
obj.instance_variable_set('@attributes', @attributes.dup)
obj
end
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back
# attributes will be marked as read only since they cannot be saved.
def readonly?
defined?(@readonly) && @readonly == true
@readonly
end
# Marks this record as read only.
@@ -1940,10 +1941,10 @@ module ActiveRecord #:nodoc:
protected
def clone_attributes(reader_method = :read_attribute, attributes = {})
self.attribute_names.inject(attributes) do |attrs, name|
attrs[name] = clone_attribute_value(reader_method, name)
attrs
attribute_names.each do |name|
attributes[name] = clone_attribute_value(reader_method, name)
end
attributes
end
def clone_attribute_value(reader_method, attribute_name)
@@ -2246,4 +2247,4 @@ end
# TODO: Remove this and make it work with LAZY flag
require 'active_record/connection_adapters/abstract_adapter'
ActiveRecord.run_base_hooks(ActiveRecord::Base)
ActiveRecord.run_base_hooks(ActiveRecord::Base)