Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb,
lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb,
lib/polyamorous/activerecord_5.2.1_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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb', line 7

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(outer_joins, join_type) ⇒ Object

Replaces ActiveRecord::Associations::JoinDependency#join_constraints

This internal method was changed in Rails 5.0 by commit github.com/rails/rails/commit/e038975 which added left_outer_joins (see #make_polyamorous_left_outer_joins below) and added passing an additional argument, ‘join_type`, to #join_constraints.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb', line 40

def join_constraints(outer_joins, join_type)
  joins = join_root.children.flat_map { |child|
    if join_type == Arel::Nodes::OuterJoin
      make_polyamorous_left_outer_joins join_root, child
    else
      make_polyamorous_inner_joins join_root, child
    end
  }

  joins.concat outer_joins.flat_map { |oj|
    if join_root.match? oj.join_root
      walk(join_root, oj.join_root)
    else
      oj.join_root.children.flat_map { |child|
        make_outer_joins(oj.join_root, child)
      }
    end
  }
end