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/attribute_updates.rb,
lib/thinking_sphinx/active_record/has_many_association.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, Delta, HasManyAssociation, Scopes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
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
# File 'lib/thinking_sphinx/active_record.rb', line 12

def self.included(base)
  base.class_eval do
    class_inheritable_array :sphinx_indexes, :sphinx_facets
    
    extend ThinkingSphinx::ActiveRecord::ClassMethods
    
    class << self
      attr_accessor :sphinx_index_blocks
      
      def set_sphinx_primary_key(attribute)
        @sphinx_primary_key_attribute = attribute
      end
      
      def primary_key_for_sphinx
        @sphinx_primary_key_attribute || primary_key
      end
      
      def sphinx_index_options
        sphinx_indexes.last.options
      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
        (subclasses << self).collect { |klass| klass.to_crc32 }
      end
      
      def sphinx_database_adapter
        @sphinx_database_adapter ||=
          ThinkingSphinx::AbstractAdapter.detect(self)
      end
      
      def sphinx_name
        self.name.underscore.tr(':/\\', '_')
      end
      
      #
      # The above method to_crc32s is dependant on the subclasses being loaded consistently
      # After a reset_subclasses is called (during a Dispatcher.cleanup_application in development)
      # Our subclasses will be lost but our context will not reload them for us.
      #
      # We reset the context which causes the subclasses to be reloaded next time the context is called.
      #
      def reset_subclasses_with_thinking_sphinx
        reset_subclasses_without_thinking_sphinx
        ThinkingSphinx.reset_context!
      end
      
      alias_method_chain :reset_subclasses, :thinking_sphinx
      
      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
  
  ::ActiveRecord::Associations::HasManyAssociation.send(
    :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  )
  ::ActiveRecord::Associations::HasManyThroughAssociation.send(
    :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  )
end

Instance Method Details

#in_both_indexes?Boolean

Returns:

  • (Boolean)


319
320
321
# File 'lib/thinking_sphinx/active_record.rb', line 319

def in_both_indexes?
  in_core_index? && in_delta_index?
end

#in_core_index?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/thinking_sphinx/active_record.rb', line 311

def in_core_index?
  in_index? "core"
end

#in_delta_index?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/thinking_sphinx/active_record.rb', line 315

def in_delta_index?
  in_index? "delta"
end

#in_index?(suffix) ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/thinking_sphinx/active_record.rb', line 307

def in_index?(suffix)
  self.class.search_for_id self.sphinx_document_id, sphinx_index_name(suffix)
end

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



329
330
331
# File 'lib/thinking_sphinx/active_record.rb', line 329

def primary_key_for_sphinx
  @primary_key_for_sphinx ||= read_attribute(self.class.primary_key_for_sphinx)
end

#sphinx_document_idObject



333
334
335
336
# File 'lib/thinking_sphinx/active_record.rb', line 333

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