1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

make a singleton for AssociationScope

AssociationScope no longer maintains state, so we're safe to keep a
singleton and save on GC time
This commit is contained in:
Aaron Patterson 2014-02-14 14:23:04 -08:00
parent 8e6ef92fd9
commit 213b2fbf40
3 changed files with 8 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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