Module: Elastify::ActiveRecordExtensions::ClassMethods

Defined in:
lib/elastify/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#elastify(type, index, &block) ⇒ Object



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
# File 'lib/elastify/active_record_extensions.rb', line 11

def elastify type, index, &block
    include Elastify::ActiveRecordExtensions::LocalMethods
    cattr_accessor :elastify_options, :elastify_model_block
    attr_accessor :elastify_serialized_document

    self.elastify_options = {
        base_url: Elastify.configs[:base_url],
        index: index,
        type: type,
        map: Elastify.mappings.symbolize_keys[type.to_sym]
    }

    self.elastify_options.each do |key, value|
        if value.blank? 
            raise ElastifyError, "You must specify the #{key} value"
        end
    end 

    if block_given?
        self.elastify_model_block = block
    else 
        if self.respond_to?(:to_elastify)
            self.elastify_model_block = Proc.new { |item| next item.to_elastify }
        elsif self.respond_to?(:to_serial)
            self.elastify_model_block = Proc.new { |item| next item.to_serial }
        else   
            self.elastify_model_block = Proc.new { |item| next item.serializable_hash }
        end
    end
end