Sha256: 1cae74ea8c3e537f4a7d0411cc20be553a2f218bf9097256feff264802a69071

Contents?: true

Size: 1.37 KB

Versions: 9

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

class ThinkingSphinx::ActiveRecord::SQLSource::Template
  attr_reader :source

  def initialize(source)
    @source = source
  end

  def apply
    add_field class_column, :sphinx_internal_class_name

    add_attribute primary_key,  :sphinx_internal_id,    nil
    add_attribute class_column, :sphinx_internal_class, :string, :facet => true
    add_attribute '0',          :sphinx_deleted,        :integer
  end

  private

  def add_attribute(column, name, type, options = {})
    source.add_attribute ThinkingSphinx::ActiveRecord::Attribute.new(
      source.model, ThinkingSphinx::ActiveRecord::Column.new(column),
      options.merge(:as => name, :type => type)
    )
  end

  def add_field(column, name, options = {})
    source.add_field ThinkingSphinx::ActiveRecord::Field.new(
      source.model, ThinkingSphinx::ActiveRecord::Column.new(column),
      options.merge(:as => name)
    )
  end

  def class_column
    if inheriting?
      adapter = source.adapter
      quoted_column = "#{adapter.quoted_table_name}.#{adapter.quote(model.inheritance_column)}"
      source.adapter.convert_blank quoted_column, "'#{model.sti_name}'"
    else
      "'#{model.name}'"
    end
  end

  def inheriting?
    model.column_names.include?(model.inheritance_column)
  end

  def model
    source.model
  end

  def primary_key
    source.options[:primary_key].to_sym
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/active_record/sql_source/template.rb
thinking-sphinx-5.0.0 lib/thinking_sphinx/active_record/sql_source/template.rb