Browse files

speeding up clone_attributes, changing readonly to be initialized in …

…def initialize

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
1 parent 115230e commit 95bd56e3cc028971059ac4ec0dfe3e29012ef35d @tenderlove tenderlove committed with wycats Mar 14, 2010
Showing with 9 additions and 8 deletions.
  1. +9 −8 activerecord/lib/active_record/base.rb
View
17 activerecord/lib/active_record/base.rb
@@ -1507,6 +1507,7 @@ def initialize(attributes = nil)
@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 @@ def initialize_copy(other)
# 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 @@ def becomes(klass)
# 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 @@ def frozen?
# 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 @@ def inspect
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 @@ def object_from_yaml(string)
# 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)

0 comments on commit 95bd56e

Please sign in to comment.