class ThinkingSphinx::ActiveRecord::SQLSource

Constants

OPTIONS

Attributes

associations[RW]
attributes[RW]
conditions[RW]
database_settings[R]
fields[RW]
groupings[RW]
model[R]
options[R]
polymorphs[RW]

Public Class Methods

new(model, options = {}) click to toggle source
Calls superclass method
# File lib/thinking_sphinx/active_record/sql_source.rb, line 13
def initialize(model, options = {})
  @model             = model
  @database_settings = model.connection.instance_variable_get(:@config).clone
  @options           = {
    :utf8? => (@database_settings[:encoding].to_s[/^utf8/])
  }.merge options

  @fields            = []
  @attributes        = []
  @associations      = []
  @conditions        = []
  @groupings         = []
  @polymorphs        = []

  Template.new(self).apply

  name = "#{options[:name] || model.name.downcase}_#{options[:position]}"

  super name, type

  apply_defaults!
end

Public Instance Methods

adapter() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 36
def adapter
  @adapter ||= DatabaseAdapters.adapter_for(@model)
end
delta?() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 44
def delta?
  options[:delta?]
end
delta_processor() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 40
def delta_processor
  options[:delta_processor].try(:new, adapter, @options[:delta_options] || {})
end
disable_range?() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 48
def disable_range?
  options[:disable_range?]
end
facets() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 52
def facets
  properties.select(&:facet?)
end
offset() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 56
def offset
  options[:offset]
end
primary_key() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 60
def primary_key
  options[:primary_key]
end
properties() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 64
def properties
  fields + attributes
end
render() click to toggle source
Calls superclass method
# File lib/thinking_sphinx/active_record/sql_source.rb, line 68
def render
  prepare_for_render unless @prepared

  super
end
set_database_settings(settings) click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 74
def set_database_settings(settings)
  @sql_host ||= settings[:host]     || 'localhost'
  @sql_user ||= settings[:username] || settings[:user] || ENV['USER']
  @sql_pass ||= settings[:password].to_s.gsub('#', '\#')
  @sql_db   ||= settings[:database]
  @sql_port ||= settings[:port]
  @sql_sock ||= settings[:socket]
  @mysql_ssl_cert ||= settings[:sslcert]
  @mysql_ssl_key  ||= settings[:sslkey]
  @mysql_ssl_ca   ||= settings[:sslca]
end
type() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 86
def type
  @type ||= case adapter
  when DatabaseAdapters::MySQLAdapter
    'mysql'
  when DatabaseAdapters::PostgreSQLAdapter
    'pgsql'
  else
    raise UnknownDatabaseAdapter, "Provided type: #{adapter.class.name}"
  end
end

Private Instance Methods

append_presenter_to_attribute_array() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 99
def append_presenter_to_attribute_array
  attributes.each do |attribute|
    presenter = Attribute::SphinxPresenter.new(attribute, self)

    attribute_array_for(presenter.collection_type) << presenter.declaration
  end
end
attribute_array_for(type) click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 107
def attribute_array_for(type)
  instance_variable_get "@sql_attr_#{type}".to_sym
end
build_sql_fields() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 115
def build_sql_fields
  fields.each do |field|
    @sql_field_string        << field.name if field.with_attribute?
    @sql_field_str2wordcount << field.name if field.wordcount?
    @sql_file_field          << field.name if field.file?

    @sql_joined_field << PropertyQuery.new(field, self).to_s if field.source_type
  end
end
build_sql_query() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 125
def build_sql_query
  @sql_query             = builder.sql_query
  @sql_query_range     ||= builder.sql_query_range
  @sql_query_pre        += builder.sql_query_pre
end
builder() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 111
def builder
  @builder ||= SQLBuilder.new self
end
config() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 131
def config
  ThinkingSphinx::Configuration.instance
end
prepare_for_render() click to toggle source
# File lib/thinking_sphinx/active_record/sql_source.rb, line 135
def prepare_for_render
  polymorphs.each &:morph!
  append_presenter_to_attribute_array

  set_database_settings database_settings
  build_sql_fields
  build_sql_query

  @prepared = true
end