module ThinkingSphinx::ActiveRecord

Core additions to ActiveRecord models - define_index for creating indexes for models. If you want to interrogate the index objects created for the model, you can use the class-level accessor :sphinx_indexes.

Attributes

sphinx_index_blocks[RW]
sphinx_types[RW]
excerpts[RW]
matching_fields[RW]
sphinx_attributes[RW]

Public Class Methods

clear_primary_key_for_sphinx() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 51
def clear_primary_key_for_sphinx
  @primary_key_for_sphinx = nil
end
custom_primary_key_for_sphinx?() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 44
def custom_primary_key_for_sphinx?
  (
    superclass.respond_to?(:custom_primary_key_for_sphinx?) &&
    superclass.custom_primary_key_for_sphinx?
  ) || !@sphinx_primary_key_attribute.nil?
end
included(base) click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 16
def self.included(base)
  base.class_eval do
    if defined?(class_attribute)
      class_attribute :sphinx_indexes, :sphinx_facets
    else
      class_inheritable_array :sphinx_indexes, :sphinx_facets
    end

    extend ThinkingSphinx::ActiveRecord::ClassMethods

    class << self
      attr_accessor :sphinx_index_blocks, :sphinx_types

      def set_sphinx_primary_key(attribute)
        @sphinx_primary_key_attribute = attribute
      end

      def primary_key_for_sphinx
        @primary_key_for_sphinx ||= begin
          if custom_primary_key_for_sphinx?
            @sphinx_primary_key_attribute ||
            superclass.primary_key_for_sphinx
          else
            primary_key || 'id'
          end
        end
      end

      def custom_primary_key_for_sphinx?
        (
          superclass.respond_to?(:custom_primary_key_for_sphinx?) &&
          superclass.custom_primary_key_for_sphinx?
        ) || !@sphinx_primary_key_attribute.nil?
      end

      def clear_primary_key_for_sphinx
        @primary_key_for_sphinx = nil
      end

      def sphinx_index_options
        sphinx_indexes.last.options
      end

      def set_sphinx_types(types)
        @sphinx_types = types
      end

      # Generate a unique CRC value for the model's name, to use to
      # determine which Sphinx documents belong to which AR records.
      #
      # Really only written for internal use - but hey, if it's useful to
      # you in some other way, awesome.
      #
      def to_crc32
        self.name.to_crc32
      end

      def to_crc32s
        (descendants << self).collect { |klass| klass.to_crc32 }
      end

      def sphinx_database_adapter
        ThinkingSphinx::AbstractAdapter.detect(self)
      end

      def sphinx_name
        self.name.underscore.tr(':/\', '_')
      end

      private

      def defined_indexes?
        @defined_indexes
      end

      def defined_indexes=(value)
        @defined_indexes = value
      end

      def sphinx_delta?
        self.sphinx_indexes.any? { |index| index.delta? }
      end
    end
  end

  if ThinkingSphinx.rails_3_1?
    assoc_mixin = ThinkingSphinx::ActiveRecord::CollectionProxy
    ::ActiveRecord::Associations::CollectionProxy.send(:include, assoc_mixin)
  else
    assoc_mixin = ThinkingSphinx::ActiveRecord::HasManyAssociation
    ::ActiveRecord::Associations::HasManyAssociation.send(:include, assoc_mixin)
    ::ActiveRecord::Associations::HasManyThroughAssociation.send(:include, assoc_mixin)
  end
end
primary_key_for_sphinx() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 33
def primary_key_for_sphinx
  @primary_key_for_sphinx ||= begin
    if custom_primary_key_for_sphinx?
      @sphinx_primary_key_attribute ||
      superclass.primary_key_for_sphinx
    else
      primary_key || 'id'
    end
  end
end
set_sphinx_primary_key(attribute) click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 29
def set_sphinx_primary_key(attribute)
  @sphinx_primary_key_attribute = attribute
end
set_sphinx_types(types) click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 59
def set_sphinx_types(types)
  @sphinx_types = types
end
sphinx_database_adapter() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 77
def sphinx_database_adapter
  ThinkingSphinx::AbstractAdapter.detect(self)
end
sphinx_index_options() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 55
def sphinx_index_options
  sphinx_indexes.last.options
end
sphinx_name() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 81
def sphinx_name
  self.name.underscore.tr(':/\', '_')
end
to_crc32() click to toggle source

Generate a unique CRC value for the model's name, to use to determine which Sphinx documents belong to which AR records.

Really only written for internal use - but hey, if it's useful to you in some other way, awesome.

# File lib/thinking_sphinx/active_record.rb, line 69
def to_crc32
  self.name.to_crc32
end
to_crc32s() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 73
def to_crc32s
  (descendants << self).collect { |klass| klass.to_crc32 }
end

Private Class Methods

defined_indexes=(value) click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 91
def defined_indexes=(value)
  @defined_indexes = value
end
defined_indexes?() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 87
def defined_indexes?
  @defined_indexes
end
sphinx_delta?() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 95
def sphinx_delta?
  self.sphinx_indexes.any? { |index| index.delta? }
end

Public Instance Methods

primary_key_for_sphinx() click to toggle source

Returns the unique integer id for the object. This method uses the attribute hash to get around ActiveRecord always mapping the id method to whatever the real primary key is (which may be a unique string hash).

@return [Integer] Unique record id for the purposes of Sphinx.

# File lib/thinking_sphinx/active_record.rb, line 368
def primary_key_for_sphinx
  read_attribute(self.class.primary_key_for_sphinx)
end
sphinx_document_id() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 372
def sphinx_document_id
  primary_key_for_sphinx * ThinkingSphinx.context.indexed_models.size +
    self.class.sphinx_offset
end
toggle_deleted() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 348
def toggle_deleted
  return unless ThinkingSphinx.updates_enabled?

  self.class.core_index_names.each do |index_name|
    self.class.delete_in_index index_name, self.sphinx_document_id
  end
  self.class.delta_index_names.each do |index_name|
    self.class.delete_in_index index_name, self.sphinx_document_id
  end if self.class.delta_indexed_by_sphinx? && toggled_delta?

rescue ::ThinkingSphinx::ConnectionError
  # nothing
end

Private Instance Methods

define_indexes() click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 383
def define_indexes
  self.class.define_indexes
end
sphinx_index_name(suffix) click to toggle source
# File lib/thinking_sphinx/active_record.rb, line 379
def sphinx_index_name(suffix)
  "#{self.class.source_of_sphinx_index.name.underscore.tr(':/\\', '_')}_#{suffix}"
end