aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorBen Woosley <ben.woosley@gmail.com>2014-09-20 09:03:03 -0700
committerBen Woosley <ben.woosley@gmail.com>2014-09-28 16:17:06 -0700
commit9d569585a20ddd9ddb3602921f2ccffc208998d8 (patch)
tree97f5ee9a38a958f3bf60c1f1f972b8549f9cedb5 /activerecord/lib/active_record/associations.rb
parent482e80e84440136763b47c0fba2ed65a7823f022 (diff)
downloadrails-9d569585a20ddd9ddb3602921f2ccffc208998d8.tar.gz
rails-9d569585a20ddd9ddb3602921f2ccffc208998d8.tar.bz2
rails-9d569585a20ddd9ddb3602921f2ccffc208998d8.zip
Isolate access to @associations_cache and @aggregations cache to the Associations and Aggregations modules, respectively.
This includes replacing the `association_cache` accessor with a more limited `association_cached?` accessor and making `clear_association_cache` and `clear_aggregation_cache` private.
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r--activerecord/lib/active_record/associations.rb36
1 files changed, 27 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 12ca3a48a9..5f80f485af 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -138,20 +138,14 @@ module ActiveRecord
autoload :AliasTracker, 'active_record/associations/alias_tracker'
end
- # Clears out the association cache.
- def clear_association_cache #:nodoc:
- @association_cache.clear if persisted?
- end
-
- # :nodoc:
- attr_reader :association_cache
-
# Returns the association instance for the given name, instantiating it if it doesn't already exist
def association(name) #:nodoc:
association = association_instance_get(name)
if association.nil?
- raise AssociationNotFoundError.new(self, name) unless reflection = self.class._reflect_on_association(name)
+ unless reflection = self.class._reflect_on_association(name)
+ raise AssociationNotFoundError.new(self, name)
+ end
association = reflection.association_class.new(self, reflection)
association_instance_set(name, association)
end
@@ -159,7 +153,31 @@ module ActiveRecord
association
end
+ def association_cached?(name) # :nodoc
+ @association_cache.key?(name)
+ end
+
+ def initialize_dup(*) # :nodoc:
+ @association_cache = {}
+ super
+ end
+
+ def reload(*) # :nodoc:
+ clear_association_cache
+ super
+ end
+
private
+ # Clears out the association cache.
+ def clear_association_cache # :nodoc:
+ @association_cache.clear if persisted?
+ end
+
+ def init_internals # :nodoc:
+ @association_cache = {}
+ super
+ end
+
# Returns the specified association instance if it responds to :loaded?, nil otherwise.
def association_instance_get(name)
@association_cache[name]