Class: Blacklight::SearchState::PivotFilterField

Inherits:
FilterField
  • Object
show all
Defined in:
lib/blacklight/search_state/pivot_filter_field.rb

Overview

Modeling access to filter query parameters

Defined Under Namespace

Classes: PivotValue, QueryBuilder

Constant Summary collapse

STOP_VALUE =
[:stop].freeze

Constants inherited from FilterField

FilterField::MISSING

Instance Attribute Summary

Attributes inherited from FilterField

#config, #filters_key, #inclusive_filters_key, #inclusive_param, #param, #search_state

Instance Method Summary collapse

Methods inherited from FilterField

#each_value, #initialize, #key, #permitted_params

Constructor Details

This class inherits a constructor from Blacklight::SearchState::FilterField

Instance Method Details

#add(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • item (String, #value)

    a filter item to add to the url

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blacklight/search_state/pivot_filter_field.rb', line 14

def add(item)
  item = wrap_item(item)
  new_state = search_state.reset_search
  return new_state if include?(item)

  pivot_values = pivot_fq(item).merge(pivot[0].to_sym => as_url_parameter(item))
  pivot_values.inject(new_state) do |memo_state, entry|
    url_key, value = entry
    field_facade = FilterField.new(null_field(url_key), memo_state)
    next memo_state if value.nil? || field_facade.include?(value)

    field_facade.add(value)
  end
end

#include?(item) ⇒ Boolean

Returns whether the provided filter is currently applied/selected.

Parameters:

  • item (String, #value #fq)

    a filter may represent in the url

Returns:

  • (Boolean)

    whether the provided filter is currently applied/selected



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blacklight/search_state/pivot_filter_field.rb', line 68

def include?(item)
  return false unless pivot.is_a?(Array) && pivot.present?

  params = search_state.params
  item = wrap_item(item)
  pivot_values = pivot_fq(item).merge(pivot[0].to_sym => as_url_parameter(item))
  pivot_values.inject(true) do |m, entry|
    k, v = entry
    m && params.dig(:f, k)&.include?(as_url_parameter(v))
  end
end

#pivotArray

Returns:

  • (Array)


10
# File 'lib/blacklight/search_state/pivot_filter_field.rb', line 10

delegate :pivot, to: :config

#remove(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • item (String, #value)

    a filter to remove from the url

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blacklight/search_state/pivot_filter_field.rb', line 31

def remove(item)
  item = wrap_item(item)
  new_state = search_state.reset_search
  pivot_values = pivot_fq(item).merge(pivot[0].to_sym => as_url_parameter(item))
  pivot_values.inject(new_state) do |memo_state, entry|
    url_key, value = entry
    next memo_state if value.nil?

    FilterField.new(null_field(url_key), memo_state).remove(value)
  end
end

#values(except: []) ⇒ Array

Matrix the values of the pivoted fields rubocop:disable Lint/UnusedMethodArgument

Returns:

  • (Array)

    an array of applied filters



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/blacklight/search_state/pivot_filter_field.rb', line 46

def values(except: [])
  return nil unless pivot.is_a?(Array) && pivot.present?

  params = search_state.params
  # values should have at most one terminal blank pivot
  pivot_values = pivot.map { |k| Array(params.dig(:f, k)) || STOP_VALUE }
  pivot_values = pivot_values[0..(pivot_values.index(STOP_VALUE) || -1)]
  # put an explicit nil in for the matrix
  pivot_values[-1] = [nil] if pivot_values.last == STOP_VALUE
  top_level_values = pivot_values.shift
  return [] if top_level_values.first.blank?

  pivot_values.each { |pivot_value| pivot_value[0] ||= nil }
  matrix_values = top_level_values.product(*pivot_values)
  matrix_values.map do |vals|
    PivotValue.new(value: vals.shift, fq: pivot[1..].map(&:to_sym).zip(vals).to_h)
  end
end