Skip to content

Commit

Permalink
Remove join_keys and get_join_keys
Browse files Browse the repository at this point in the history
Use `join_primary_key` and `join_foreign_key` directly.
  • Loading branch information
kamipo committed Jun 2, 2020
1 parent e9b2dde commit fc0090b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
14 changes: 6 additions & 8 deletions activerecord/lib/active_record/associations/association_scope.rb
Expand Up @@ -56,13 +56,12 @@ def join(table, constraint)
end

def last_chain_scope(scope, reflection, owner)
join_keys = reflection.join_keys
key = join_keys.key
foreign_key = join_keys.foreign_key
primary_key = reflection.join_primary_key
foreign_key = reflection.join_foreign_key

table = reflection.aliased_table
value = transform_value(owner[foreign_key])
scope = apply_scope(scope, table, key, value)
scope = apply_scope(scope, table, primary_key, value)

if reflection.type
polymorphic_type = transform_value(owner.class.polymorphic_name)
Expand All @@ -77,13 +76,12 @@ def transform_value(value)
end

def next_chain_scope(scope, reflection, next_reflection)
join_keys = reflection.join_keys
key = join_keys.key
foreign_key = join_keys.foreign_key
primary_key = reflection.join_primary_key
foreign_key = reflection.join_foreign_key

table = reflection.aliased_table
foreign_table = next_reflection.aliased_table
constraint = table[key].eq(foreign_table[foreign_key])
constraint = table[primary_key].eq(foreign_table[foreign_key])

if reflection.type
value = transform_value(next_reflection.klass.polymorphic_name)
Expand Down
33 changes: 17 additions & 16 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -167,10 +167,6 @@ def class_name

JoinKeys = Struct.new(:key, :foreign_key) # :nodoc:

def join_keys
@join_keys ||= get_join_keys(klass)
end

# Returns a list of scopes that should be applied for this Reflection
# object when querying the database.
def scopes
Expand All @@ -188,10 +184,10 @@ def join_scope(table, foreign_table, foreign_klass)

scope_chain_items.inject(klass_scope, &:merge!)

key = join_keys.key
foreign_key = join_keys.foreign_key
primary_key = join_primary_key
foreign_key = join_foreign_key

klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
klass_scope.where!(table[primary_key].eq(foreign_table[foreign_key]))

if klass.finder_needs_type_condition?
klass_scope.where!(klass.send(:type_condition, table))
Expand Down Expand Up @@ -287,10 +283,6 @@ def chain
collect_join_chain
end

def get_join_keys(association_klass)
JoinKeys.new(join_primary_key(association_klass), join_foreign_key)
end

def build_scope(table, predicate_builder = predicate_builder(table), klass = self.klass)
Relation.create(
klass,
Expand All @@ -299,7 +291,7 @@ def build_scope(table, predicate_builder = predicate_builder(table), klass = sel
)
end

def join_primary_key(*)
def join_primary_key(klass = nil)
foreign_key
end

Expand Down Expand Up @@ -754,8 +746,8 @@ def collection?
# Holds all the metadata about a :through association as it was specified
# in the Active Record class.
class ThroughReflection < AbstractReflection #:nodoc:
delegate :foreign_key, :foreign_type, :association_foreign_key, :join_id_for,
:active_record_primary_key, :type, :get_join_keys, to: :source_reflection
delegate :foreign_key, :foreign_type, :association_foreign_key, :join_id_for, :type,
:active_record_primary_key, :join_foreign_key, to: :source_reflection

def initialize(delegate_reflection)
@delegate_reflection = delegate_reflection
Expand Down Expand Up @@ -875,6 +867,10 @@ def association_primary_key(klass = nil)
actual_source_reflection.options[:primary_key] || primary_key(klass || self.klass)
end

def join_primary_key(klass = self.klass)
source_reflection.join_primary_key(klass)
end

# Gets an array of possible <tt>:through</tt> source reflection names in both singular and plural form.
#
# class Post < ActiveRecord::Base
Expand Down Expand Up @@ -1008,7 +1004,8 @@ def derive_class_name
end

class PolymorphicReflection < AbstractReflection # :nodoc:
delegate :klass, :scope, :plural_name, :type, :get_join_keys, :scope_for, to: :@reflection
delegate :klass, :scope, :plural_name, :type, :join_primary_key, :join_foreign_key,
:scope_for, to: :@reflection

def initialize(reflection, previous_reflection)
@reflection = reflection
Expand All @@ -1033,7 +1030,7 @@ def source_type_scope
end

class RuntimeReflection < AbstractReflection # :nodoc:
delegate :scope, :type, :constraints, :get_join_keys, to: :@reflection
delegate :scope, :type, :constraints, :join_foreign_key, to: :@reflection

def initialize(reflection, association)
@reflection = reflection
Expand All @@ -1048,6 +1045,10 @@ def aliased_table
@aliased_table ||= Arel::Table.new(table_name, type_caster: klass.type_caster)
end

def join_primary_key(klass = self.klass)
@reflection.join_primary_key(klass)
end

def all_includes; yield; end
end
end
Expand Down

0 comments on commit fc0090b

Please sign in to comment.