Module: Polyamorous::JoinAssociationExtensions

Includes:
SwappingReflectionClass
Defined in:
lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SwappingReflectionClass

#swapping_reflection_klass

Class Method Details

.prepended(base) ⇒ Object



4
5
6
# File 'lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb', line 4

def self.prepended(base)
  base.class_eval { attr_reader :join_type }
end

Instance Method Details

#initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb', line 8

def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
  @join_type = join_type
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
    swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
      super(reflection, children)
      self.reflection.options[:polymorphic] = true
    end
  else
    super(reflection, children)
  end
end

#join_constraints_with_tables(foreign_table, foreign_klass, join_type, alias_tracker, tables) ⇒ Object

Same as #join_constraints, but instead of constructing tables from the given block, uses the ones passed



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb', line 22

def join_constraints_with_tables(foreign_table, foreign_klass, join_type, alias_tracker, tables)
  joins = []
  chain = []

  reflection.chain.each.with_index do |reflection, i|
    table = tables[i]

    @table ||= table
    chain << [reflection, table]
  end

  # The chain starts with the target table, but we want to end with it here (makes
  # more sense in this context), so we reverse
  chain.reverse_each do |reflection, table|
    klass = reflection.klass

    join_scope = reflection.join_scope(table, foreign_table, foreign_klass)

    unless join_scope.references_values.empty?
      join_dependency = join_scope.construct_join_dependency(
        join_scope.eager_load_values | join_scope.includes_values, Arel::Nodes::OuterJoin
      )
      join_scope.joins!(join_dependency)
    end

    arel = join_scope.arel(alias_tracker.aliases)
    nodes = arel.constraints.first

    if nodes.is_a?(Arel::Nodes::And)
      others = nodes.children.extract! do |node|
        !Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
      end
    end

    joins << table.create_join(table, table.create_on(nodes), join_type)

    if others && !others.empty?
      joins.concat arel.join_sources
      append_constraints(joins.last, others)
    end

    # The current table in this iteration becomes the foreign table in the next
    foreign_table, foreign_klass = table, klass
  end

  joins
end