Module: SunspotCell::Setup

Defined in:
lib/sunspot_cell/setup.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
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
# File 'lib/sunspot_cell/setup.rb', line 4

def self.included(base)
  base.class_eval do
    alias :sunspot_initialize :initialize unless method_defined?(:sunspot_initialize)
    def initialize(clazz)
      @attachment_field_factories, @attachment_field_factories_cache = *Array.new(8) { Hash.new }
      sunspot_initialize(clazz)
    end

    alias :sunspot_all_field_factories :all_field_factories unless method_defined?(:sunspot_all_field_factories)
    def all_field_factories
      all_field_factories = sunspot_all_field_factories
      all_field_factories.concat(attachment_field_factories)
      all_field_factories
    end
    
    # Add field_factories for fulltext search on attachments
    #
    # ==== Parameters
    #
    def add_attachment_field_factory(name, options = {}, &block)
      stored = options[:stored]
      field_factory = Sunspot::FieldFactory::Static.new(name, Sunspot::Type::AttachmentType.instance, options, &block)
      @attachment_field_factories[name] = field_factory
      @attachment_field_factories_cache[field_factory.name] = field_factory
      if stored
        @attachment_field_factories_cache[field_factory.name] << field_factory
      end
    end

    def text_fields(field_name)
      text_field =
        if field_factory = @text_field_factories_cache[field_name.to_sym]
          field_factory.build
        else
          if field_factory = @attachment_field_factories_cache[field_name.to_sym]
            field_factory.build
          else
            raise(
              UnrecognizedFieldError,
              "No text field configured for #{@class_name} with name '#{field_name}'"
            )
          end
        end
      [text_field]
    end

    #
    # Return all text fields
    #
    def all_text_fields
      text_fields = text_field_factories.map { |text_field_factory| text_field_factory.build }
      text_fields.concat(all_attachment_fields)
    end

    def all_attachment_fields
      attachment_field_factories.map { |field_factory| field_factory.build }
    end

    # Get the text field_factories associated with this setup as well as all inherited
    # attachment field_factories
    #
    # ==== Returns
    #
    # Array:: Collection of all text field_factories associated with this setup
    #
    def attachment_field_factories
      collection_from_inheritable_hash(:attachment_field_factories)
    end
  end
end