class ThinkingSphinx::ActiveRecord::Attribute::Type

Constants

UPDATEABLE_TYPES

Attributes

attribute[R]
model[R]

Public Class Methods

new(attribute, model) click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 6
def initialize(attribute, model)
  @attribute, @model = attribute, model
end

Public Instance Methods

multi?() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 10
def multi?
  @multi ||= attribute.options[:multi] || multi_from_associations
end
timestamp?() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 14
def timestamp?
  type == :timestamp
end
type() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 18
def type
  @type ||= attribute.options[:type] || type_from_database
end
type=(value) click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 22
def type=(value)
  @type = attribute.options[:type] = value
end
updateable?() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 26
def updateable?
  UPDATEABLE_TYPES.include?(type) && single_column_reference?
end

Private Instance Methods

associations() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 34
def associations
  @associations ||= begin
    klass = model
    attribute.columns.first.__stack.collect { |name|
      association = klass.reflect_on_association(name)
      klass       = association.klass
      association
    }
  end
end
big_integer?() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 45
def big_integer?
  type_symbol == :integer && database_column.sql_type[/bigint/i]
end
column_name() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 49
def column_name
  attribute.columns.first.__name.to_s
end
database_column() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 53
def database_column
  @database_column ||= klass.columns.detect { |db_column|
    db_column.name == column_name
  }
end
klass() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 59
def klass
  @klass ||= associations.any? ? associations.last.klass : model
end
multi_from_associations() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 63
def multi_from_associations
  associations.any? { |association|
    [:has_many, :has_and_belongs_to_many].include?(association.macro)
  }
end
single_column_reference?() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 69
def single_column_reference?
  attribute.columns.length == 1               &&
  attribute.columns.first.__stack.length == 0 &&
  !attribute.columns.first.string?
end
type_from_database() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 75
  def type_from_database
    raise ThinkingSphinx::MissingColumnError,
      "Cannot determine the database type of column #{column_name}, as it does not exist" if database_column.nil?

    return :bigint if big_integer?

    case type_symbol
    when :datetime, :date
      :timestamp
    when :text
      :string
    when :decimal
      :float
    when :integer, :boolean, :timestamp, :float, :string, :bigint, :json
      type_symbol
    else
      raise ThinkingSphinx::UnknownAttributeType,
        <<-ERROR
Unable to determine an equivalent Sphinx attribute type from #{database_column.type.class.name} for attribute #{attribute.name}. You may want to manually set the type.

e.g.
  has my_column, :type => :integer
        ERROR
    end
  end
type_symbol() click to toggle source
# File lib/thinking_sphinx/active_record/attribute/type.rb, line 101
def type_symbol
  return database_column.type if database_column.type.is_a?(Symbol)

  database_column.type.class.name.demodulize.downcase.to_sym
end