From a5651eb522bd656f21f2f2da57d6326a6bd28372 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 3 Mar 2017 11:15:58 -0800 Subject: Move join scopes on to the reflection object Scopes can only ever be *not* reflection objects when they are passed in to the Reflection constructor. Given this fact, we can eliminate is_a checks and an intermediate array object by just asking the reflection object for join scopes. --- .../join_dependency/join_association.rb | 9 +-------- activerecord/lib/active_record/reflection.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 935777d485..c28e3bb097 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -41,14 +41,7 @@ module ActiveRecord constraint = build_constraint(klass, table, key, foreign_table, foreign_key) predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table)) - scope_chain_items = reflection.scopes.map do |item| - if item.is_a?(Relation) - item - else - ActiveRecord::Relation.create(klass, table, predicate_builder) - .instance_exec(&item) - end - end + scope_chain_items = reflection.join_scopes(table, predicate_builder) klass_scope = if klass.current_scope diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 61a2279292..02aab25610 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -187,6 +187,15 @@ module ActiveRecord end deprecate :scope_chain + def join_scopes(table, predicate_builder) # :nodoc: + if scope + [ActiveRecord::Relation.create(klass, table, predicate_builder) + .instance_exec(&scope)] + else + [] + end + end + def constraints chain.map(&:scopes).flatten end @@ -806,6 +815,10 @@ module ActiveRecord source_reflection.scopes + super end + def join_scopes(table, predicate_builder) # :nodoc: + source_reflection.join_scopes(table, predicate_builder) + super + end + def source_type_scope through_reflection.klass.where(foreign_type => options[:source_type]) end @@ -990,6 +1003,15 @@ module ActiveRecord end end + def join_scopes(table, predicate_builder) # :nodoc: + scopes = @previous_reflection.join_scopes(table, predicate_builder) + super + if @previous_reflection.options[:source_type] + scopes + [@previous_reflection.source_type_scope] + else + scopes + end + end + def klass @reflection.klass end -- cgit v1.2.3