Module: SunspotCell::Indexer

Defined in:
lib/sunspot_cell/indexer.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
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
# File 'lib/sunspot_cell/indexer.rb', line 4

def self.included(base)
  base.class_eval do

    def add_documents(documents)
      documents_arr = Sunspot::Util.Array(documents)
      docs_attach = []
      docs_no_attach = []
      documents_arr.each do |document|
        if document.contains_attachment?
          docs_attach << document
        else
          docs_no_attach << document
        end
      end

      begin
        if !docs_no_attach.empty?
          @connection.add(docs_no_attach)
        end
        if !docs_attach.empty?
          Sunspot::Util.Array(docs_attach).each do |document|
            document.add(@connection)
          end
        end
      rescue Exception => e
        @batch = nil
        raise e
      end
    end


    def document_for(model)
      Sunspot::RichDocument.new(
        :id => Sunspot::Adapters::InstanceAdapter.adapt(model).index_id,
        :type => Sunspot::Util.superclasses_for(model.class).map { |clazz| clazz.name }
      )
    end

  end
end