Sha256: 7a9fe6f362c696be5741bd41ffbae6a18350bd68c9174dc1eeee64a2e322b2e0

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

class ThinkingSphinx::ActiveRecord::PropertySQLPresenter
  attr_reader :property, :adapter, :associations

  def initialize(property, adapter, associations)
    @property, @adapter, @associations = property, adapter, associations
  end

  def to_group
    return nil unless group?

    columns_with_table
  end

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

  private

  def aggregate?
    property.columns.any? { |column|
      associations.aggregate_for?(column.__stack)
    }
  end

  def aggregate_separator
    (property.multi?) ? ',' : ' '
  end

  def casted_column_with_table
    clause = columns_with_table
    clause = adapter.cast_to_timestamp(clause) if property.type == :timestamp
    clause = adapter.concatenate(clause, ' ')  if concatenating?
    if aggregate?
      clause = adapter.group_concatenate(clause, aggregate_separator)
    end

    clause
  end

  def column_with_table(column)
    return column.__name if column.string?

    "#{associations.alias_for(column.__stack)}.#{adapter.quote column.__name}"
  end

  def columns_with_table
    property.columns.collect { |column|
      column_with_table(column)
    }.join(', ')
  end

  def concatenating?
    property.columns.length > 1
  end

  def group?
    !(aggregate? || property.columns.any?(&:string?))
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.0 lib/thinking_sphinx/active_record/property_sql_presenter.rb
thinking-sphinx-3.0.0.rc lib/thinking_sphinx/active_record/property_sql_presenter.rb
thinking-sphinx-3.0.0.pre lib/thinking_sphinx/active_record/property_sql_presenter.rb