module Polyamorous::JoinDependencyExtensions

Public Instance Methods

build(associations, base_klass) click to toggle source

Replaces ActiveRecord::Associations::JoinDependency#build

# File lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb, line 4
def build(associations, base_klass)
  associations.map do |name, right|
    if name.is_a? Join
      reflection = find_reflection base_klass, name.name
      reflection.check_validity!
      reflection.check_eager_loadable!

      klass = if reflection.polymorphic?
        name.klass || base_klass
      else
        reflection.klass
      end
      JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
    else
      reflection = find_reflection base_klass, name
      reflection.check_validity!
      reflection.check_eager_loadable!

      if reflection.polymorphic?
        raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
      end
      JoinAssociation.new(reflection, build(right, reflection.klass))
    end
  end
end
join_constraints(joins_to_add, join_type, alias_tracker) click to toggle source
# File lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb, line 30
def join_constraints(joins_to_add, join_type, alias_tracker)
  @alias_tracker = alias_tracker

  construct_tables!(join_root)
  joins = make_join_constraints(join_root, join_type)

  joins.concat joins_to_add.flat_map { |oj|
    construct_tables!(oj.join_root)
    if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
      walk join_root, oj.join_root
    else
      make_join_constraints(oj.join_root, join_type)
    end
  }
end

Private Instance Methods

make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin) click to toggle source
# File lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb, line 47
def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
  foreign_table = parent.table
  foreign_klass = parent.base_klass
  join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin
  joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
  joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
end