diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-02-14 14:23:04 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-02-14 14:23:04 -0800 |
commit | 213b2fbf40f6a1ce8381749bd5ba734f20bd4b21 (patch) | |
tree | bc432094b82a6695919922a6067c455ef1476f8e /activerecord | |
parent | 8e6ef92fd9ede38aa435afeaef6370ff65191a0b (diff) | |
download | rails-213b2fbf40f6a1ce8381749bd5ba734f20bd4b21.tar.gz rails-213b2fbf40f6a1ce8381749bd5ba734f20bd4b21.tar.bz2 rails-213b2fbf40f6a1ce8381749bd5ba734f20bd4b21.zip |
make a singleton for AssociationScope
AssociationScope no longer maintains state, so we're safe to keep a
singleton and save on GC time
Diffstat (limited to 'activerecord')
3 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 2dba33898c..4e46256862 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -94,7 +94,7 @@ module ActiveRecord # actually gets built. def association_scope if klass - @association_scope ||= AssociationScope.new.scope(self, klass.connection) + @association_scope ||= AssociationScope.scope(self, klass.connection) end end diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index cd2911e0a4..1bc998d20c 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -1,6 +1,12 @@ module ActiveRecord module Associations class AssociationScope #:nodoc: + INSTANCE = new + + def self.scope(association, connection) + INSTANCE.scope association, connection + end + def scope(association, connection) klass = association.klass reflection = association.reflection diff --git a/activerecord/test/cases/associations/association_scope_test.rb b/activerecord/test/cases/associations/association_scope_test.rb index f9793277e5..c78b036f53 100644 --- a/activerecord/test/cases/associations/association_scope_test.rb +++ b/activerecord/test/cases/associations/association_scope_test.rb @@ -6,8 +6,7 @@ module ActiveRecord module Associations class AssociationScopeTest < ActiveRecord::TestCase test 'does not duplicate conditions' do - association_scope = AssociationScope.new - scope = association_scope.scope(Author.new.association(:welcome_posts), + scope = AssociationScope.scope(Author.new.association(:welcome_posts), Author.connection) wheres = scope.where_values.map(&:right) assert_equal wheres.uniq, wheres |