class ThinkingSphinx::Middlewares::SphinxQL::Inner

Attributes

context[R]

Public Class Methods

new(context) click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 20
def initialize(context)
  @context = context
end

Public Instance Methods

call() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 24
def call
  context[:indices]  = indices
  context[:sphinxql] = statement
end

Private Instance Methods

class_condition() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 59
def class_condition
  "(#{classes_and_descendants_names.join('|')})"
end
class_condition_required?() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 63
def class_condition_required?
  classes.any? && !indices_match_classes?
end
classes() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 37
def classes
  options[:classes] || []
end
classes_and_descendants() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 41
def classes_and_descendants
  classes + descendants
end
classes_and_descendants_names() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 45
def classes_and_descendants_names
  classes_and_descendants.collect do |klass|
    name = klass.name
    name = %Q{"#{name}"} if name[/:/]
    name
  end
end
classes_with_inheritance_column() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 53
def classes_with_inheritance_column
  classes.select { |klass|
    klass.column_names.include?(klass.inheritance_column)
  }
end
constantize_inheritance_column(klass) click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 67
def constantize_inheritance_column(klass)
  klass.connection.select_values(inheritance_column_select(klass)).compact.each(&:constantize)
end
descendants() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 71
def descendants
  @descendants ||= options[:skip_sti] ? [] : descendants_from_tables
end
descendants_from_tables() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 75
def descendants_from_tables
  classes_with_inheritance_column.collect do |klass|
    constantize_inheritance_column(klass)
    klass.descendants
  end.flatten
end
exclusive_filters() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 95
def exclusive_filters
  @exclusive_filters ||= (options[:without] || {}).tap do |without|
    without[:sphinx_internal_id] = options[:without_ids] if options[:without_ids].present?
  end
end
extended_query() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 101
def extended_query
  conditions = options[:conditions] || {}
  if class_condition_required?
    conditions[:sphinx_internal_class_name] = class_condition
  end

  @extended_query ||= ThinkingSphinx::Search::Query.new(
    context.search.query, conditions, options[:star]
  ).to_s
end
group_attribute() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 112
def group_attribute
  options[:group_by].to_s if options[:group_by]
end
group_order_clause() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 116
def group_order_clause
  group_by = options[:order_group_by]
  group_by = "#{group_by} ASC" if group_by.is_a? Symbol
  group_by
end
inclusive_filters() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 122
def inclusive_filters
  (options[:with] || {}).merge({:sphinx_deleted => false})
end
index_names() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 126
def index_names
  indices.collect(&:name)
end
index_options() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 130
def index_options
  indices.first.options
end
indices() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 134
def indices
  @indices ||= begin
    set = ThinkingSphinx::IndexSet.new classes, options[:indices]
    raise ThinkingSphinx::NoIndicesError if set.empty?
    set
  end
end
indices_match_classes?() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 82
def indices_match_classes?
  indices.collect(&:reference).uniq.sort == classes.collect { |klass|
    klass.name.underscore.to_sym
  }.sort
end
inheritance_column_select(klass) click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 88
    def inheritance_column_select(klass)
      <<-SQL
SELECT DISTINCT #{klass.inheritance_column}
FROM #{klass.table_name}
SQL
    end
order_clause() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 142
def order_clause
  order_by = options[:order]
  order_by = "#{order_by} ASC" if order_by.is_a? Symbol
  order_by
end
select_options() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 148
def select_options
  @select_options ||= SELECT_OPTIONS.inject({}) do |hash, key|
    hash[key] = settings[key.to_s] if settings.key? key.to_s
    hash[key] = index_options[key] if index_options.key? key
    hash[key] = options[key]       if options.key? key
    hash
  end
end
statement() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 162
def statement
  Statement.new(self).to_riddle_query_select
end
values() click to toggle source
# File lib/thinking_sphinx/middlewares/sphinxql.rb, line 157
def values
  options[:select] ||= "*, #{ThinkingSphinx::SphinxQL.group_by}, #{ThinkingSphinx::SphinxQL.count}" if group_attribute.present?
  options[:select]
end