class ThinkingSphinx::ActiveRecord::PropertyQuery

Attributes

property[R]
source[R]
type[R]

Public Class Methods

new(property, source, type = nil) click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 2
def initialize(property, source, type = nil)
  @property, @source, @type = property, source, type
end

Public Instance Methods

to_s() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 6
def to_s
  identifier = [type, property.name].compact.join(' ')

  "#{identifier} from #{source_type}; #{queries.join('; ')}"
end

Private Instance Methods

base_association() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 27
def base_association
  reflections.first
end
base_association_class() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 31
def base_association_class
  base_association.klass
end
column() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 36
def column
  @column ||= property.columns.first
end
extend_reflection(reflection) click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 40
def extend_reflection(reflection)
  return [reflection] unless reflection.through_reflection

  [reflection.through_reflection, reflection.source_reflection]
end
joins() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 59
def joins
  @joins ||= begin
    remainder = reflections.collect(&:name)[1..-1]
    return nil             if remainder.empty?
    return remainder.first if remainder.length == 1

    remainder[0..-2].reverse.inject(remainder.last) { |value, key|
      {key => value}
    }
  end
end
offset() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 71
def offset
  "* #{ThinkingSphinx::Configuration.instance.indices.count} + #{source.offset}"
end
queries() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 14
def queries
  queries    = []
  if column.string?
    queries << column.__name.strip.gsub(/\n/, "\\\n")
  else
    queries << to_sql
    queries << range_sql if ranged?
  end
  queries
end
quote_column(column) click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 87
def quote_column(column)
  ActiveRecord::Base.connection.quote_column_name(column)
end
quote_with_table(table, column) click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 83
def quote_with_table(table, column)
  "#{quote_column(table)}.#{quote_column(column)}"
end
quoted_foreign_key() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 75
def quoted_foreign_key
  quote_with_table(base_association_class.table_name, base_association.foreign_key)
end
quoted_primary_key() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 79
def quoted_primary_key
  quote_with_table(reflections.last.klass.table_name, column.__name)
end
range_sql() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 95
def range_sql
  base_association_class_unscoped.select(
    "MIN(#{quoted_foreign_key}), MAX(#{quoted_foreign_key})"
  ).to_sql
end
ranged?() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 91
def ranged?
  property.source_type == :ranged_query
end
reflections() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 46
def reflections
  @reflections ||= begin
    base = source.model

    column.__stack.collect { |key|
      reflection = base.reflections[key]
      base = reflection.klass

      extend_reflection reflection
    }.flatten
  end
end
source_type() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 101
def source_type
  property.source_type.to_s.dasherize
end
to_sql() click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 105
def to_sql
  raise "Could not determine SQL for MVA" if reflections.empty?

  relation = base_association_class_unscoped.select("#{quoted_foreign_key} #{offset} AS #{quote_column('id')}, #{quoted_primary_key} AS #{quote_column(property.name)}"
  )
  relation = relation.joins(joins) if joins.present?
  relation = relation.where("#{quoted_foreign_key} BETWEEN $start AND $end") if ranged?
  relation = relation.order("#{quoted_foreign_key} ASC") if type.nil?

  relation.to_sql
end