Module: ThinkingSphinx::ActiveRecord

Defined in:
lib/thinking_sphinx/active_record.rb,
lib/thinking_sphinx/active_record/delta.rb,
lib/thinking_sphinx/active_record/scopes.rb,
lib/thinking_sphinx/active_record/log_subscriber.rb,
lib/thinking_sphinx/active_record/collection_proxy.rb,
lib/thinking_sphinx/active_record/attribute_updates.rb,
lib/thinking_sphinx/active_record/has_many_association.rb,
lib/thinking_sphinx/active_record/collection_proxy_with_scopes.rb,
lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb

Overview

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.

Defined Under Namespace

Modules: AttributeUpdates, ClassMethods, CollectionProxy, CollectionProxyWithScopes, Delta, HasManyAssociation, HasManyAssociationWithScopes, Scopes Classes: LogSubscriber

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#excerptsObject

Returns the value of attribute excerpts.



343
344
345
# File 'lib/thinking_sphinx/active_record.rb', line 343

def excerpts
  @excerpts
end

#matching_fieldsObject

Returns the value of attribute matching_fields.



345
346
347
# File 'lib/thinking_sphinx/active_record.rb', line 345

def matching_fields
  @matching_fields
end

#sphinx_attributesObject

Returns the value of attribute sphinx_attributes.



344
345
346
# File 'lib/thinking_sphinx/active_record.rb', line 344

def sphinx_attributes
  @sphinx_attributes
end

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

Instance Method Details

#primary_key_for_sphinxInteger

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).

Returns:

  • (Integer)

    Unique record id for the purposes of Sphinx.



367
368
369
# File 'lib/thinking_sphinx/active_record.rb', line 367

def primary_key_for_sphinx
  read_attribute(self.class.primary_key_for_sphinx)
end

#sphinx_document_idObject



371
372
373
374
# File 'lib/thinking_sphinx/active_record.rb', line 371

def sphinx_document_id
  primary_key_for_sphinx * ThinkingSphinx.context.indexed_models.size +
    self.class.sphinx_offset
end

#toggle_deletedObject



347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/thinking_sphinx/active_record.rb', line 347

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