Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb,
lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#build(associations, base_klass) ⇒ Object

Replaces ActiveRecord::Associations::JoinDependency#build


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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, alias_tracker) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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