Sha256: f8da675809bbc0fef1b282f3a21fbd219cc2ed1c5d05395b7075d8ed043aba2e

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 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 if sourced_by_query? || !group?

    columns_with_table
  end

  def to_select
    return nil if sourced_by_query?

    "#{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 = concatenate clause
    if aggregate?
      clause = adapter.group_concatenate(clause, aggregate_separator)
    end

    clause
  end

  def column_exists?(column)
    model = associations.model_for(column.__stack)
    model && model.column_names.include?(column.__name.to_s)
  end

  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

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

  def concatenating?
    property.columns.length > 1
  end

  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

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

  def sourced_by_query?
    property.source_type.to_s[/query/]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.5 lib/thinking_sphinx/active_record/property_sql_presenter.rb