Module: Polyamorous::JoinAssociationExtensions

Includes:
SwappingReflectionClass
Defined in:
lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb,
lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb,
lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb,
lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SwappingReflectionClass

#swapping_reflection_klass

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb', line 5

def self.included(base)
  base.class_eval do
    attr_reader :join_type
    alias_method_chain :initialize, :polymorphism
    alias_method_chain :build_constraint, :polymorphism
  end
end

.prepended(base) ⇒ Object



6
7
8
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb', line 6

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

Instance Method Details

#==(other) ⇒ Object

Reference: github.com/rails/rails/commit/9b15db5 NOTE: Not sure we still need it?



26
27
28
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb', line 26

def ==(other)
  base_klass == other.base_klass
end

#association_join_with_polymorphismObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb', line 54

def association_join_with_polymorphism
  return @join if @Join
  @join = association_join_without_polymorphism
  if reflection.macro == :belongs_to && reflection.options[:polymorphic]
    aliased_table = Arel::Table.new(
      table_name,
      as: @aliased_table_name,
      engine: arel_engine,
      columns: klass.columns
    )
    parent_table = Arel::Table.new(
      parent.table_name,
      as: parent.aliased_table_name,
      engine: arel_engine,
      columns: parent.base_klass.columns
    )
    @join << parent_table[reflection.options[:foreign_type]]
             .eq(reflection.klass.name)
  end
  @join
end

#build_constraint(klass, table, key, foreign_table, foreign_key) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb', line 30

def build_constraint(klass, table, key, foreign_table, foreign_key)
  if reflection.polymorphic?
    super(klass, table, key, foreign_table, foreign_key)
      .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
  else
    super(klass, table, key, foreign_table, foreign_key)
  end
end

#build_constraint_with_polymorphism(reflection, table, key, foreign_table, foreign_key) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb', line 31

def build_constraint_with_polymorphism(
  klass, table, key, foreign_table, foreign_key
)
  if reflection.polymorphic?
    build_constraint_without_polymorphism(
      klass, table, key, foreign_table, foreign_key
    )
    .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
  else
    build_constraint_without_polymorphism(
      klass, table, key, foreign_table, foreign_key
    )
  end
end

#equality_with_polymorphism(other) ⇒ Object



35
36
37
# File 'lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb', line 35

def equality_with_polymorphism(other)
  equality_without_polymorphism(other) && base_klass == other.base_klass
end

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



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb', line 10

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

#initialize_with_polymorphism(reflection, join_dependency, parent = nil, polymorphic_class = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb', line 13

def initialize_with_polymorphism(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|
      initialize_without_polymorphism(reflection, children)
      self.reflection.options[:polymorphic] = true
    end
  else
    initialize_without_polymorphism(reflection, children)
  end
end