Sha256: ad86e06c28e227e9d5f401041cfb967e657cfd889213d9f1df24ba417de1287f

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# active_record_5.0_ruby_2/join_association.rb

module Polyamorous
  module JoinAssociationExtensions
    include SwappingReflectionClass
    def self.prepended(base)
      base.class_eval { attr_reader :join_type }
    end

    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

    # Reference: https://github.com/rails/rails/commit/9b15db5
    # NOTE: Not sure we still need it?
    def ==(other)
      base_klass == other.base_klass
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyamorous-1.3.0 lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb