Module: Rooftop::AlgoliaSearch::PostIndexing

Defined in:
lib/rooftop/algolia_search/post_indexing.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/rooftop/algolia_search/post_indexing.rb', line 5

def self.included(base)
  base.extend ClassMethods

  base.send :setup_index_name!

  base.send :after_save, :reindex!
  base.send :after_destroy, :deindex!

end

Instance Method Details

#deindex!Object



92
93
94
# File 'lib/rooftop/algolia_search/post_indexing.rb', line 92

def deindex!
  self.class.deindex_entity(self)
end

#reindex!Object Also known as: index!

Reindex an instance



81
82
83
84
85
86
87
88
# File 'lib/rooftop/algolia_search/post_indexing.rb', line 81

def reindex!
  # only reindex things which are published
  if status == 'publish'
    self.class.reindex_entity(self)
  else
    deindex!
  end
end

#to_search_index_parametersObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rooftop/algolia_search/post_indexing.rb', line 96

def to_search_index_parameters
  self.class.search_fields.inject({}) do |hash, fields|
    hash[:objectID] = self.id

    if fields.last.is_a? Proc
      hash[fields.first] = fields.last.call(self)
    else
      fields.each do |field|
        begin
          hash[field] = self.send(field)
        rescue NoMethodError
          if self.has_field?(field)
            hash[field] = self.fields.send(field)
          end
        end
      end
    end

    hash
  end
end