mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
use a hash for caching aggregations rather than ivars
This commit is contained in:
@@ -4,9 +4,7 @@ module ActiveRecord
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def clear_aggregation_cache #:nodoc:
|
||||
self.class.reflect_on_all_aggregations.to_a.each do |assoc|
|
||||
instance_variable_set "@#{assoc.name}", nil
|
||||
end if self.persisted?
|
||||
@aggregation_cache.clear if persisted?
|
||||
end
|
||||
|
||||
# Active Record implements aggregation through a macro-like class method called +composed_of+
|
||||
@@ -224,11 +222,7 @@ module ActiveRecord
|
||||
def reader_method(name, class_name, mapping, allow_nil, constructor)
|
||||
module_eval do
|
||||
define_method(name) do
|
||||
unless instance_variable_defined?("@#{name}")
|
||||
instance_variable_set("@#{name}", nil)
|
||||
end
|
||||
|
||||
if (instance_variable_get("@#{name}").nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
|
||||
if (@aggregation_cache[name].nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
|
||||
attrs = mapping.collect {|pair| read_attribute(pair.first)}
|
||||
object = case constructor
|
||||
when Symbol
|
||||
@@ -238,9 +232,9 @@ module ActiveRecord
|
||||
else
|
||||
raise ArgumentError, 'Constructor must be a symbol denoting the constructor method to call or a Proc to be invoked.'
|
||||
end
|
||||
instance_variable_set("@#{name}", object)
|
||||
@aggregation_cache[name] = object
|
||||
end
|
||||
instance_variable_get("@#{name}")
|
||||
@aggregation_cache[name]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -251,7 +245,7 @@ module ActiveRecord
|
||||
define_method("#{name}=") do |part|
|
||||
if part.nil? && allow_nil
|
||||
mapping.each { |pair| self[pair.first] = nil }
|
||||
instance_variable_set("@#{name}", nil)
|
||||
@aggregation_cache[name] = nil
|
||||
else
|
||||
unless part.is_a?(class_name.constantize) || converter.nil?
|
||||
part = case converter
|
||||
@@ -265,7 +259,7 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
mapping.each { |pair| self[pair.first] = part.send(pair.last) }
|
||||
instance_variable_set("@#{name}", part.freeze)
|
||||
@aggregation_cache[name] = part.freeze
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1369,6 +1369,7 @@ MSG
|
||||
def initialize(attributes = nil)
|
||||
@attributes = attributes_from_column_definition
|
||||
@association_cache = {}
|
||||
@aggregation_cache = {}
|
||||
@attributes_cache = {}
|
||||
@new_record = true
|
||||
@readonly = false
|
||||
@@ -1417,6 +1418,7 @@ MSG
|
||||
@attributes = coder['attributes']
|
||||
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
|
||||
@association_cache = {}
|
||||
@aggregation_cache = {}
|
||||
@readonly = @destroyed = @marked_for_destruction = false
|
||||
@new_record = false
|
||||
_run_find_callbacks
|
||||
@@ -1628,8 +1630,7 @@ MSG
|
||||
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
|
||||
end
|
||||
|
||||
clear_aggregation_cache
|
||||
|
||||
@aggregation_cache = {}
|
||||
@association_cache = {}
|
||||
@attributes_cache = {}
|
||||
@new_record = true
|
||||
|
||||
Reference in New Issue
Block a user