Sha256: 6d2270724cad773920913db065aa0881f91ed9cbaa42d0cf2cdf1d1f043b4c66

Contents?: true

Size: 878 Bytes

Versions: 2

Compression:

Stored size: 878 Bytes

Contents

# frozen_string_literal: true

# This overriding approach is only available in Rails 5.2+. This behaviour
# was preceded by AssociationReflection for Rails 4.1-5.1.
class ThinkingSphinx::ActiveRecord::Depolymorph::OverriddenReflection <
  ThinkingSphinx::ActiveRecord::Depolymorph::BaseReflection

  module JoinConstraint
    def build_join_constraint(table, foreign_table)
      super.and(
        foreign_table[options[:foreign_type]].eq(
          options[:class_name].constantize.base_class.name
        )
      )
    end
  end

  def self.overridden_classes
    @overridden_classes ||= {}
  end

  def call
    klass.new name, nil, options, reflection.active_record
  end

  private

  def klass
    self.class.overridden_classes[reflection.class] ||= begin
      subclass = Class.new reflection.class
      subclass.include JoinConstraint
      subclass
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinking-sphinx-4.1.0 lib/thinking_sphinx/active_record/depolymorph/overridden_reflection.rb
thinking-sphinx-4.0.0 lib/thinking_sphinx/active_record/depolymorph/overridden_reflection.rb