mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Cheaper attribute reads and respond_to?. Add underscore-prefixed method aliased to the attribute name so it can be overridden but still called internally.
This commit is contained in:
@@ -48,13 +48,13 @@ module ActiveRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def respond_to?(*args)
|
def respond_to?(*args)
|
||||||
self.class.define_attribute_methods
|
self.class.define_attribute_methods unless self.class.attribute_methods_generated?
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def attribute_method?(attr_name)
|
def attribute_method?(attr_name)
|
||||||
attr_name == 'id' || attributes.include?(attr_name)
|
attr_name == 'id' || @attributes.include?(attr_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -70,13 +70,19 @@ module ActiveRecord
|
|||||||
if cache_attribute?(attr_name)
|
if cache_attribute?(attr_name)
|
||||||
access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
|
access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
|
||||||
end
|
end
|
||||||
generated_attribute_methods.module_eval("def #{symbol}; #{access_code}; end", __FILE__, __LINE__)
|
generated_attribute_methods.module_eval("def _#{symbol}; #{access_code}; end; alias #{symbol} _#{symbol}", __FILE__, __LINE__)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
|
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
|
||||||
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
|
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
|
||||||
def read_attribute(attr_name)
|
def read_attribute(attr_name)
|
||||||
|
send "_#{attr_name}"
|
||||||
|
rescue NoMethodError
|
||||||
|
_read_attribute attr_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def _read_attribute(attr_name)
|
||||||
attr_name = attr_name.to_s
|
attr_name = attr_name.to_s
|
||||||
attr_name = self.class.primary_key if attr_name == 'id'
|
attr_name = self.class.primary_key if attr_name == 'id'
|
||||||
if !(value = @attributes[attr_name]).nil?
|
if !(value = @attributes[attr_name]).nil?
|
||||||
@@ -89,8 +95,6 @@ module ActiveRecord
|
|||||||
else
|
else
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -19,12 +19,13 @@ module ActiveRecord
|
|||||||
def define_method_attribute(attr_name)
|
def define_method_attribute(attr_name)
|
||||||
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
||||||
method_body, line = <<-EOV, __LINE__ + 1
|
method_body, line = <<-EOV, __LINE__ + 1
|
||||||
def #{attr_name}(reload = false)
|
def _#{attr_name}(reload = false)
|
||||||
cached = @attributes_cache['#{attr_name}']
|
cached = @attributes_cache['#{attr_name}']
|
||||||
return cached if cached && !reload
|
return cached if cached && !reload
|
||||||
time = read_attribute('#{attr_name}')
|
time = _read_attribute('#{attr_name}')
|
||||||
@attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time
|
@attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time
|
||||||
end
|
end
|
||||||
|
alias #{attr_name} _#{attr_name}
|
||||||
EOV
|
EOV
|
||||||
generated_attribute_methods.module_eval(method_body, __FILE__, line)
|
generated_attribute_methods.module_eval(method_body, __FILE__, line)
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user