Module: Rooftop::AlgoliaSearch::FacetHelper

Defined in:
app/helpers/rooftop/algolia_search/facet_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_facet_query(hash, opts = {}) ⇒ Object



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
# File 'app/helpers/rooftop/algolia_search/facet_helper.rb', line 5

def build_facet_query(hash,opts = {})
#   takes a hash like this:
#   {
#     foo: ['bar', 'baz'],
#     qux: ['boo']
#
#   }

  if opts[:or]
    # returns this
    # ["foo:bar", "foo:baz", "qux:boo"]
    result = []
    hash.each do |k,v|
      if v.is_a?(Array)
        v.each {|val| result << "#{k}:#{val}"}
      else
        result << "#{k}:#{v}"
      end
    end
    return [result]
  else
    # returns this
    # [["foo:bar", "foo:baz"],"qux:boo"]
    hash.collect do |k,v|
      if v.is_a?(Array)
        v.collect {|val| "#{k}:#{val}"}
      else
        "#{k}:#{v}"
      end
    end

  end

end