Sha256: c55fc523202279d133eb2e995b2bf2a55f29ccb9e9861285ddabd5f44e2d8427

Contents?: true

Size: 1.76 KB

Versions: 7

Compression:

Stored size: 1.76 KB

Contents

class ThinkingSphinx::ActiveRecord::FilteredReflection <
  ActiveRecord::Reflection::AssociationReflection

  class Filter
    attr_reader :reflection, :class_name

    delegate :foreign_type, :active_record, :to => :reflection

    def initialize(reflection, class_name)
      @reflection, @class_name = reflection, class_name
      @options = reflection.options.clone
    end

    def options
      @options.delete :polymorphic
      @options[:class_name]    = class_name
      @options[:foreign_key] ||= "#{reflection.name}_id"
      @options[:foreign_type]  = reflection.foreign_type

      return @options if reflection.respond_to?(:scope)

      case @options[:conditions]
      when nil
        @options[:conditions] = condition
      when Array
        @options[:conditions] << condition
      when Hash
        @options[:conditions].merge!(reflection.foreign_type => @options[:class_name])
      else
        @options[:conditions] << " AND #{condition}"
      end

      @options
    end

    def scope
      lambda { |association|
        reflection = association.reflection
        where(
          association.parent.aliased_table_name.to_sym =>
          {reflection.foreign_type => reflection.class_name}
        )
      }
    end

    private

    def condition
      "::ts_join_alias::.#{quoted_foreign_type} = '#{class_name}'"
    end

    def quoted_foreign_type
      active_record.connection.quote_column_name foreign_type
    end
  end

  def self.clone_with_filter(reflection, name, class_name)
    filter = Filter.new(reflection, class_name)

    if reflection.respond_to?(:scope)
      new reflection.macro, name, filter.scope, filter.options,
        reflection.active_record
    else
      new reflection.macro, name, filter.options, reflection.active_record
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thinking-sphinx-3.1.1 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.1.0 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.0.6 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.0.5 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.0.4 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.0.3 lib/thinking_sphinx/active_record/filtered_reflection.rb
thinking-sphinx-3.0.2 lib/thinking_sphinx/active_record/filtered_reflection.rb