Class: Workarea::Search::ProductRules

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/search/product_rules.rb

Constant Summary collapse

RULE_OPERATOR_MAP =
{
  'greater_than' => :gt,
  'greater_than_or_equal' => :gte,
  'less_than' => :lt,
  'less_than_or_equal' => :lte
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, ignore_category_ids: []) ⇒ ProductRules

Returns a new instance of ProductRules.



13
14
15
16
17
18
19
# File 'app/queries/workarea/search/product_rules.rb', line 13

def initialize(rules, ignore_category_ids: [])
  @rules = rules || []

  # Used to track which category rules are already included to avoid an
  # infinite recursion.
  @ignore_category_ids = ignore_category_ids
end

Instance Attribute Details

#ignore_category_idsObject (readonly)

Returns the value of attribute ignore_category_ids.



11
12
13
# File 'app/queries/workarea/search/product_rules.rb', line 11

def ignore_category_ids
  @ignore_category_ids
end

#rulesObject (readonly)

Returns the value of attribute rules.



11
12
13
# File 'app/queries/workarea/search/product_rules.rb', line 11

def rules
  @rules
end

Instance Method Details

#to_aArray<Hash>

Processes category rules into filters to be used by Elasticsearch.

Returns:

  • (Array<Hash>)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/queries/workarea/search/product_rules.rb', line 26

def to_a
  @filters ||= rules.inject([]) do |memo, rule|
    if rule.category?
      memo.push(*category_clauses_for(rule))
    elsif rule.search?
      memo.push(*search_clauses_for(rule))
    elsif rule.product_exclusion?
      memo.push(product_exclusion_clause_for(rule))
    elsif rule.equality?
      memo.push(*equality_clauses_for(rule))
    elsif rule.inequality?
      memo.push(bool: { must_not: equality_clauses_for(rule) })
    elsif rule.comparison?
      memo.push(*comparison_clauses_for(rule))
    end

    memo
  end.uniq
end