Class: Blacklight::SearchState::FilterField

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

Overview

Modeling access to filter query parameters

Direct Known Subclasses

PivotFilterField

Constant Summary collapse

MISSING =
{ missing: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, search_state) ⇒ FilterField

Returns a new instance of FilterField.



24
25
26
27
28
29
# File 'lib/blacklight/search_state/filter_field.rb', line 24

def initialize(config, search_state)
  @config = config
  @search_state = search_state
  @filters_key = :f
  @inclusive_filters_key = :f_inclusive
end

Instance Attribute Details

#configBlacklight::Configuration::FacetField



17
18
19
# File 'lib/blacklight/search_state/filter_field.rb', line 17

def config
  @config
end

#filters_keyObject (readonly)

Returns the value of attribute filters_key.



17
18
19
# File 'lib/blacklight/search_state/filter_field.rb', line 17

def filters_key
  @filters_key
end

#inclusive_filters_keyObject (readonly)

Returns the value of attribute inclusive_filters_key.



17
18
19
# File 'lib/blacklight/search_state/filter_field.rb', line 17

def inclusive_filters_key
  @inclusive_filters_key
end

#inclusive_paramString, Symbol

Returns:

  • (String, Symbol)


17
# File 'lib/blacklight/search_state/filter_field.rb', line 17

attr_reader :config, :search_state, :filters_key, :inclusive_filters_key

#paramString, Symbol

Returns:

  • (String, Symbol)


17
# File 'lib/blacklight/search_state/filter_field.rb', line 17

attr_reader :config, :search_state, :filters_key, :inclusive_filters_key

#search_stateBlacklight::SearchState



17
# File 'lib/blacklight/search_state/filter_field.rb', line 17

attr_reader :config, :search_state, :filters_key, :inclusive_filters_key

Instance Method Details

#add(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • item (String, #value)

    a filter item to add to the url

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blacklight/search_state/filter_field.rb', line 33

def add(item)
  new_state = search_state.reset_search

  if item.try(:missing)
    # if this is a 'missing' facet value, the :fq is only for backwards compatibility
  elsif item.respond_to?(:fq)
    Array(item.fq).each do |f, v|
      new_state = new_state.filter(f).add(v)
    end
  end

  return new_state.filter(item.field).add(item) if item.respond_to?(:field) && item.field != key

  url_key = key
  params = new_state.params
  param = filters_key
  value = as_url_parameter(item)

  if value == Blacklight::SearchState::FilterField::MISSING
    url_key = "-#{key}"
    value = Blacklight::Engine.config.blacklight.facet_missing_param
  end

  param = inclusive_filters_key if value.is_a?(Array)

  # value could be a string
  params[param] = (params[param] || {}).dup

  if value.is_a? Array
    params[param][url_key] = value
  elsif config.single
    params[param][url_key] = [value]
  else
    params[param][url_key] = Array(params[param][url_key] || []).dup
    params[param][url_key].push(value)
  end

  new_state.reset(params)
end

#each_value(except: [], &block) ⇒ Object

Appease rubocop rules by implementing #each_value



123
124
125
# File 'lib/blacklight/search_state/filter_field.rb', line 123

def each_value(except: [], &block)
  values(except: except).each(&block)
end

#include?(item) ⇒ Boolean

Returns whether the provided filter is currently applied/selected.

Parameters:

  • item (String, #value)

    a filter to remove from the url

Returns:

  • (Boolean)

    whether the provided filter is currently applied/selected



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/blacklight/search_state/filter_field.rb', line 129

def include?(item)
  return search_state.filter(item.field).selected?(item) if item.respond_to?(:field) && item.field != key

  value = as_url_parameter(item)
  params = search_state.params

  case value
  when Array
    (params.dig(inclusive_filters_key, key) || []).to_set == value.to_set
  when Blacklight::SearchState::FilterField::MISSING
    (params.dig(filters_key, "-#{key}") || []).include?(Blacklight::Engine.config.blacklight.facet_missing_param)
  else
    (params.dig(filters_key, key) || []).include?(value)
  end
end

#keyString, Symbol

Returns:

  • (String, Symbol)


20
# File 'lib/blacklight/search_state/filter_field.rb', line 20

delegate :key, to: :config

#permitted_paramsObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/blacklight/search_state/filter_field.rb', line 145

def permitted_params
  if config.pivot
    {
      filters_key => config.pivot.each_with_object({}) { |key, filter| filter.merge(key => [], "-#{key}" => []) },
      inclusive_filters_key => config.pivot.each_with_object({}) { |key, filter| filter.merge(key => []) }
    }
  else
    {
      filters_key => { config.key => [], "-#{config.key}" => [] },
      inclusive_filters_key => { config.key => [] }
    }
  end
end

#remove(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • item (String, #value)

    a filter to remove from the url

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/blacklight/search_state/filter_field.rb', line 75

def remove(item)
  new_state = search_state.reset_search

  return new_state.filter(item.field).remove(item) if item.respond_to?(:field) && item.field != key

  url_key = config.key
  params = new_state.params

  param = filters_key
  value = as_url_parameter(item)

  if value == Blacklight::SearchState::FilterField::MISSING
    url_key = "-#{key}"
    value = Blacklight::Engine.config.blacklight.facet_missing_param
  end

  param = inclusive_filters_key if value.is_a?(Array)

  # need to dup the facet values too,
  # if the values aren't dup'd, then the values
  # from the session will get remove in the show view...
  params[param] = (params[param] || {}).dup
  params[param][url_key] = (params[param][url_key] || []).dup

  collection = params[param][url_key]

  params[param][url_key] = collection - Array(value)
  params[param].delete(url_key) if params[param][url_key].empty?
  params.delete(param) if params[param].empty?

  new_state.reset(params)
end

#values(except: []) ⇒ Array

Returns an array of applied filters.

Returns:

  • (Array)

    an array of applied filters



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/blacklight/search_state/filter_field.rb', line 109

def values(except: [])
  params = search_state.params
  return [] if params.blank?

  f = except.include?(:filters) ? [] : [params.dig(filters_key, key)].flatten.compact
  f_inclusive = [params.dig(:f_inclusive, key)] unless params.dig(inclusive_filters_key, key).blank? || except.include?(:inclusive_filters)
  f_missing = [Blacklight::SearchState::FilterField::MISSING] if params.dig(filters_key, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param }
  f_missing = [] if except.include?(:missing)

  f + (f_inclusive || []) + (f_missing || [])
end