Class: FacetFilter

Inherits:
Struct
  • Object
show all
Defined in:
public/app/models/facet_filter.rb

Overview

container for handling facet, filter information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_types, fields = [], values = []) ⇒ FacetFilter

Returns a new instance of FacetFilter.



6
7
8
9
10
11
12
13
# File 'public/app/models/facet_filter.rb', line 6

def initialize(default_types, fields = [], values=[])
  self.default_types = default_types || []
  self.fields = Array.new(fields || [])
  self.values = Array.new(values || [])
  self.facet_types = default_types
  Rails.logger.debug("Default: #{default_types} fields: #{fields} facet_types: #{self.facet_types}")
  self.facet_set_arr = []
end

Instance Attribute Details

#default_typesObject

Returns the value of attribute default_types

Returns:

  • (Object)

    the current value of default_types



2
3
4
# File 'public/app/models/facet_filter.rb', line 2

def default_types
  @default_types
end

#facet_set_arrObject

Returns the value of attribute facet_set_arr

Returns:

  • (Object)

    the current value of facet_set_arr



2
3
4
# File 'public/app/models/facet_filter.rb', line 2

def facet_set_arr
  @facet_set_arr
end

#facet_typesObject

Returns the value of attribute facet_types

Returns:

  • (Object)

    the current value of facet_types



2
3
4
# File 'public/app/models/facet_filter.rb', line 2

def facet_types
  @facet_types
end

#fieldsObject

Returns the value of attribute fields

Returns:

  • (Object)

    the current value of fields



2
3
4
# File 'public/app/models/facet_filter.rb', line 2

def fields
  @fields
end

#valuesObject

Returns the value of attribute values

Returns:

  • (Object)

    the current value of values



2
3
4
# File 'public/app/models/facet_filter.rb', line 2

def values
  @values
end

Instance Method Details

#get_facet_typesObject

an array of strings for asking for filtering



16
17
18
# File 'public/app/models/facet_filter.rb', line 16

def get_facet_types
  self.facet_types
end

#get_filter_hash(url = nil) ⇒ Object

returns a hash of arrays of hashes representing the selected filters and values, with filter field as the key in the top-level hash, the values being arrays of selections within that filter, each of which is then a hash where pt as the printable field label, pv as the printable value, v as the value of the filter



36
37
38
39
40
41
42
43
44
45
46
# File 'public/app/models/facet_filter.rb', line 36

def get_filter_hash(url = nil)
  fh = {}
  self.fields.zip(self.values) do |k, v|
    pt = I18n.t("search_results.filter.#{k}")
    pv = get_pretty_facet_value(k, v.sub(/"(.*)"/, '\1'))
    uri = (url)? url.sub("&filter_fields[]=#{k}&filter_values[]=#{CGI.escape(v)}", "") : ''
    fh[k] ||= []
    fh[k].push({'v' => v, 'pv' => pv, 'pt' => pt, 'uri' => uri })
  end
  fh
end

#get_filter_queryObject

returns an AdvancedQueryBuilder with the filters worked in.



21
22
23
24
25
# File 'public/app/models/facet_filter.rb', line 21

def get_filter_query
  builder = AdvancedQueryBuilder.new
  self.fields.zip(self.values) {|field, value| builder.and(field, value) }
  builder
end

#get_filter_url_paramsObject



27
28
29
30
31
# File 'public/app/models/facet_filter.rb', line 27

def get_filter_url_params
  param = ""
  self.fields.zip(values) {|field, value| param += "&filter_fields[]=#{field}&filter_values[]=#{CGI.escape(value)}" }
  param
end