class ThinkingSphinx::ActiveRecord::PropertySQLPresenter

Attributes

adapter[R]
associations[R]
property[R]

Public Class Methods

new(property, adapter, associations) click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 4
def initialize(property, adapter, associations)
  @property, @adapter, @associations = property, adapter, associations
end

Public Instance Methods

to_group() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 8
def to_group
  return nil if sourced_by_query? || !group?

  columns_with_table
end
to_select() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 14
def to_select
  return nil if sourced_by_query?

  "#{casted_column_with_table} AS #{adapter.quote property.name}"
end

Private Instance Methods

aggregate?() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 24
def aggregate?
  property.columns.any? { |column|
    associations.aggregate_for?(column.__stack)
  }
end
aggregate_separator() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 30
def aggregate_separator
  multi? ? ',' : ' '
end
cast_to_timestamp(clause) click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 34
def cast_to_timestamp(clause)
  clause.split(', ').collect { |part|
    adapter.cast_to_timestamp part
  }.join(', ')
end
casted_column_with_table() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 40
def casted_column_with_table
  clause = columns_with_table
  clause = cast_to_timestamp clause if property.type == :timestamp
  clause = concatenate clause
  if aggregate?
    clause = adapter.group_concatenate(clause, aggregate_separator)
  end

  clause
end
column_exists?(column) click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 51
def column_exists?(column)
  model = associations.model_for(column.__stack)
  model && model.column_names.include?(column.__name.to_s)
end
column_with_table(column) click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 56
def column_with_table(column)
  return column.__name if column.string?
  return nil unless column_exists?(column)

  "#{associations.alias_for(column.__stack)}.#{adapter.quote column.__name}"
end
columns_with_table() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 63
def columns_with_table
  property.columns.collect { |column|
    column_with_table(column)
  }.compact.join(', ')
end
concatenate(clause) click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 73
def concatenate(clause)
  return clause unless concatenating?

  if property.type.nil?
    adapter.concatenate clause, ' '
  else
    clause = clause.split(', ').collect { |part|
      adapter.cast_to_string part
    }.join(', ')
    adapter.concatenate clause, ','
  end
end
concatenating?() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 69
def concatenating?
  property.columns.length > 1
end
group?() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 86
def group?
  !(aggregate? || property.columns.any?(&:string?))
end
sourced_by_query?() click to toggle source
# File lib/thinking_sphinx/active_record/property_sql_presenter.rb, line 90
def sourced_by_query?
  property.source_type.to_s[/query/]
end