module Polyamorous::JoinAssociationExtensions

Public Class Methods

included(base) click to toggle source
# File lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb, line 5
def self.included(base)
  base.class_eval do
    alias_method_chain :initialize, :polymorphism
    alias_method :equality_without_polymorphism, :==
    alias_method :==, :equality_with_polymorphism
    if base.method_defined?(:active_record)
      alias_method :base_klass, :active_record
    end

    if ActiveRecord::VERSION::STRING =~ /^3\.0\./
      alias_method_chain :association_join, :polymorphism
    else
      alias_method_chain :build_constraint, :polymorphism
    end
  end
end
new(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) click to toggle source
Calls superclass method
# File lib/polyamorous/activerecord_5.0_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
prepended(base) click to toggle source
# File lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb, line 6
def self.prepended(base)
  base.class_eval { attr_reader :join_type }
end

Public Instance Methods

==(other) click to toggle source

Reference github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1

# File lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb, line 27
def ==(other)
  base_klass == other.base_klass
end
association_join_with_polymorphism() click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb, line 29
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 ) click to toggle source
# File lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb, line 39
def build_constraint_with_polymorphism(
  reflection, table, key, foreign_table, foreign_key
)
  if reflection.options[:polymorphic]
    build_constraint_without_polymorphism(
      reflection, table, key, foreign_table, foreign_key
    )
    .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
  else
    build_constraint_without_polymorphism(
      reflection, table, key, foreign_table, foreign_key
    )
  end
end
equality_with_polymorphism(other) click to toggle source
# 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_with_polymorphism( reflection, join_dependency, parent = nil, polymorphic_class = nil ) click to toggle source
# File lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb, line 22
def initialize_with_polymorphism(
  reflection, join_dependency, parent = nil, polymorphic_class = nil
)
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
    swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
      initialize_without_polymorphism(reflection, join_dependency, parent)
      self.reflection.options[:polymorphic] = true
    end
  else
    initialize_without_polymorphism(reflection, join_dependency, parent)
  end
end