diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2017-03-03 11:15:58 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2017-03-03 11:29:43 -0800 |
commit | a5651eb522bd656f21f2f2da57d6326a6bd28372 (patch) | |
tree | f92679a2d4494846ec1107ff57ecfdc1d6cfd5f9 | |
parent | 76be78c59cd75eaafb31719745b43df4743fd33d (diff) | |
download | rails-a5651eb522bd656f21f2f2da57d6326a6bd28372.tar.gz rails-a5651eb522bd656f21f2f2da57d6326a6bd28372.tar.bz2 rails-a5651eb522bd656f21f2f2da57d6326a6bd28372.zip |
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.
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency/join_association.rb | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 22 |
2 files changed, 23 insertions, 8 deletions
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 |