class ThinkingSphinx::ActiveRecord::Associations

Constants

JoinDependency

Attributes

model[R]

Public Class Methods

new(model) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 6
def initialize(model)
  @model = model
  @joins = ActiveSupport::OrderedHash.new
end

Public Instance Methods

add_join_to(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 11
def add_join_to(stack)
  join_for(stack)
end
aggregate_for?(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 15
def aggregate_for?(stack)
  return false if stack.empty?

  joins_for(stack).compact.any? { |join|
    [:has_many, :has_and_belongs_to_many].include?(
      join.reflection.macro
    )
  }
end
alias_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 25
def alias_for(stack)
  return model.quoted_table_name if stack.empty?

  join_for(stack).aliased_table_name
end
join_values() click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 31
def join_values
  @joins.values.compact
end
model_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 35
def model_for(stack)
  return model if stack.empty?

  join = join_for(stack)
  join.nil? ? nil : join.reflection.klass
end

Private Instance Methods

base() click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 44
def base
  @base ||= JoinDependency.new model, [], []
end
join_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 48
def join_for(stack)
  @joins[stack] ||= begin
    reflection = reflection_for stack
    reflection.nil? ? nil : JoinDependency::JoinAssociation.new(
      reflection, base, parent_join_for(stack)
    ).tap { |join|
      join.join_type = Arel::OuterJoin

      rewrite_conditions_for join
    }
  end
end
joins_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 61
def joins_for(stack)
  if stack.length == 1
    [join_for(stack)]
  else
    [joins_for(stack[0..-2]), join_for(stack)].flatten
  end
end
parent_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 69
def parent_for(stack)
  stack.length == 1 ? base : join_for(stack[0..-2])
end
parent_join_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 73
def parent_join_for(stack)
  stack.length == 1 ? base.join_base : parent_for(stack)
end
reflection_for(stack) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 77
def reflection_for(stack)
  parent = parent_for(stack)
  klass  = parent.respond_to?(:base_klass) ? parent.base_klass :
    parent.active_record
  klass.reflections[stack.last]
end
rewrite_conditions_for(join) click to toggle source
# File lib/thinking_sphinx/active_record/associations.rb, line 84
def rewrite_conditions_for(join)
  if join.respond_to?(:scope_chain)
    conditions = Array(join.scope_chain).flatten
  else
    conditions = Array(join.conditions).flatten
  end

  conditions.each do |condition|
    next unless condition.is_a?(String)

    condition.gsub! /::ts_join_alias::/,
      model.connection.quote_table_name(join.parent.aliased_table_name)
  end
end