Class: Babik::QuerySet::Join::AssociationJoiner

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/lib/join/association_joiner.rb

Overview

Class whose mission is to catch an association path and construct all the sequence of LEFT JOINS that lies behind this set of associations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(associations) ⇒ AssociationJoiner

Read an Array of associations an construct a list of joins.

Parameters:

  • associations (Array<ActiveRecord::Associations::Association>)

    Array of associations.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/babik/queryset/lib/join/association_joiner.rb', line 14

def initialize(associations)
  @left_joins_by_alias = {}
  last_table_alias = nil
  associations.each_with_index do |association, association_path_index|
    # Important, this variable will take the last joined table to chain the join, in the first loop, will take
    # the association origin table name.
    last_table_alias ||= association.active_record.table_name
    left_join = Babik::QuerySet::Join.new_from_association(association, association_path_index, last_table_alias)

    @left_joins_by_alias[left_join.target_alias] = left_join
    last_table_alias = left_join.target_alias
  end
  @target_alias = last_table_alias
end

Instance Attribute Details

#left_joins_by_aliasObject (readonly)

Returns the value of attribute left_joins_by_alias.



10
11
12
# File 'lib/babik/queryset/lib/join/association_joiner.rb', line 10

def left_joins_by_alias
  @left_joins_by_alias
end

#target_aliasObject (readonly)

Returns the value of attribute target_alias.



10
11
12
# File 'lib/babik/queryset/lib/join/association_joiner.rb', line 10

def target_alias
  @target_alias
end

Instance Method Details

#table_aliasString

Table alias will be another way of calling target alias,

i.e. the alias of the target table in the join,
i.e. the alias of the last table in the join,

Returns:

  • (String)

    Target table alias



33
34
35
# File 'lib/babik/queryset/lib/join/association_joiner.rb', line 33

def table_alias
  @target_alias
end