class Elasticsearch::FacetedSearch::FacetItem

Attributes

group[RW]
object[RW]

Public Class Methods

new(group, object) click to toggle source
# File lib/elasticsearch/faceted_search/facet_item.rb, line 19
def initialize(group, object)
  self.group = group
  self.object = OpenStruct.new(object)
end

Public Instance Methods

id() click to toggle source

delegate :operator_mappings, :operator, :key, :selected_values, :group_params, :type, to: :group delegate :term, :count, to: :object

# File lib/elasticsearch/faceted_search/facet_item.rb, line 15
def id
  object.id.to_s
end
params_for() click to toggle source
# File lib/elasticsearch/faceted_search/facet_item.rb, line 28
def params_for
  prefix = selected? ? :remove : :add

  case type
    when 'multivalue'     then send(:"#{prefix}_multivalue")
    when 'multivalue_and' then send(:"#{prefix}_multivalue", :and)
    when 'multivalue_or'  then send(:"#{prefix}_multivalue", :or)
    when 'exclusive_or'   then send(:"#{prefix}_singlevalue")
    # else                  raise UnknownSelectableType.new "Unknown selectable type #{selectable_type} for #{@type}"
  end
end
selected?() click to toggle source
# File lib/elasticsearch/faceted_search/facet_item.rb, line 24
def selected?
  @selected ||= Array(group_params).include?(id)
end

Private Instance Methods

add_multivalue(op=nil) click to toggle source

Adding a value to the parameters Example:

group_params = ['1','3','4']
id = 5

> {key => ‘1|3|4|5’}

# File lib/elasticsearch/faceted_search/facet_item.rb, line 77
def add_multivalue(op=nil)
  op = operator_mappings(op) || operator

  if params.blank?
    add_singlevalue
  else
    {
      key => (Array(params) + Array(id.to_s)).uniq
                               .join( op )
    }
  end
end
add_singlevalue() click to toggle source

Adding a single value to the parameters Example:

group_params = ['7']
id = 5

> {key => ‘5’}

# File lib/elasticsearch/faceted_search/facet_item.rb, line 95
def add_singlevalue
  { key => id.to_s }
end
matches?(v) click to toggle source
# File lib/elasticsearch/faceted_search/facet_item.rb, line 99
def matches?(v)
  v.to_s == id.to_s
end
params() click to toggle source
# File lib/elasticsearch/faceted_search/facet_item.rb, line 41
def params
  @params ||= group_params.dup
end
remove_multivalue(op=nil) click to toggle source

Removing a value from the parameters Example:

group_params = ['1','3','4']
id = 3

> {key => ‘1|4’}

# File lib/elasticsearch/faceted_search/facet_item.rb, line 50
def remove_multivalue(op=nil)
  op = operator_mappings(op) || operator
  p = params.reject{|v| matches?(v) }

  if p.blank?
    remove_singlevalue
  else
    {
      key => p.reject(&:blank?)
                   .join( op )
    }
  end
end
remove_singlevalue() click to toggle source

Remove a single value from params

# File lib/elasticsearch/faceted_search/facet_item.rb, line 66
def remove_singlevalue
  {
    key => nil
  }
end