From 23bcc6578e28355cc7258c744bcb26c0c80524ef Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 27 Jun 2017 18:47:42 +0900 Subject: Move constructing polymorphic type to `join_scope` in `Reflection` --- .../associations/join_dependency/join_association.rb | 12 ++---------- activerecord/lib/active_record/reflection.rb | 11 +++++++---- activerecord/lib/active_record/scoping/named.rb | 3 +-- 3 files changed, 10 insertions(+), 16 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 95f16c622e..70520b8e85 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -40,21 +40,13 @@ module ActiveRecord constraint = build_constraint(klass, table, key, foreign_table, foreign_key) - rel = reflection.join_scope(table) + rel = reflection.join_scope(table, foreign_klass) - if rel && !rel.arel.constraints.empty? + if rel.arel.constraints.any? binds += rel.bound_attributes constraint = constraint.and rel.arel.constraints end - if reflection.type - value = foreign_klass.base_class.name - column = klass.columns_hash[reflection.type.to_s] - - binds << Relation::QueryAttribute.new(column.name, value, klass.type_for_attribute(column.name)) - constraint = constraint.and klass.arel_attribute(reflection.type, table).eq(Arel::Nodes::BindParam.new) - end - joins << table.create_join(table, table.create_on(constraint), join_type) # The current table in this iteration becomes the foreign table in the next diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index c6aa2ed720..4e3359bb4e 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -185,12 +185,16 @@ module ActiveRecord end deprecate :scope_chain - def join_scope(table) + def join_scope(table, foreign_klass) predicate_builder = predicate_builder(table) scope_chain_items = join_scopes(table, predicate_builder) klass_scope = klass_join_scope(table, predicate_builder) - scope_chain_items.inject(klass_scope || scope_chain_items.shift, &:merge!) + if type + klass_scope.where!(type => foreign_klass.base_class.name) + end + + scope_chain_items.inject(klass_scope, &:merge!) end def join_scopes(table, predicate_builder) # :nodoc: @@ -207,8 +211,7 @@ module ActiveRecord scope.joins_values = scope.left_outer_joins_values = [].freeze } else - relation = build_scope(table, predicate_builder) - klass.send(:build_default_scope, relation) + klass.default_scoped(build_scope(table, predicate_builder)) end end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index a61fdd6454..388f471bf5 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -29,8 +29,7 @@ module ActiveRecord end end - def default_scoped # :nodoc: - scope = relation + def default_scoped(scope = relation) # :nodoc: build_default_scope(scope) || scope end -- cgit v1.2.3