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

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.



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

def initialize(config, search_state)
  @config = config
  @search_state = search_state
end

Instance Attribute Details

#configObject (readonly)



10
11
12
# File 'lib/blacklight/search_state/filter_field.rb', line 10

def config
  @config
end

#search_stateObject (readonly)

Parameters:



13
14
15
# File 'lib/blacklight/search_state/filter_field.rb', line 13

def search_state
  @search_state
end

Instance Method Details

#add(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • a (String, #value)

    filter item to add to the url

Returns:



27
28
29
30
31
32
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
# File 'lib/blacklight/search_state/filter_field.rb', line 27

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

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

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

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

  param = :f_inclusive 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

#include?(item) ⇒ Boolean

Returns whether the provided filter is currently applied/selected.

Parameters:

  • a (String, #value)

    filter to remove from the url

Returns:

  • (Boolean)

    whether the provided filter is currently applied/selected



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/blacklight/search_state/filter_field.rb', line 125

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

  value = as_url_parameter(item)
  params = search_state.params

  if value.is_a?(Array)
    (params.dig(:f_inclusive, key) || []).to_set == value.to_set
  elsif value == Blacklight::SearchState::FilterField::MISSING
    (params.dig(:f, "-#{key}") || []).include?(Blacklight::Engine.config.blacklight.facet_missing_param)
  else
    (params.dig(:f, key) || []).include?(value)
  end
end

#keyString, Symbol

Returns:

  • (String, Symbol)


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

delegate :key, to: :config

#remove(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • a (String, #value)

    filter to remove from the url

Returns:



71
72
73
74
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
107
108
109
110
# File 'lib/blacklight/search_state/filter_field.rb', line 71

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

  url_key = config.key
  params = new_state.params

  param = :f
  value = as_url_parameter(item)

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

  param = :f_inclusive 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]
  # collection should be an array, because we link to ?f[key][]=value,
  # however, Facebook (and maybe some other PHP tools) tranform that parameters
  # into ?f[key][0]=value, which Rails interprets as a Hash.
  if collection.is_a? Hash
    Deprecation.warn(self, 'Normalizing parameters in FilterField#remove is deprecated')
    collection = collection.values
  end

  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

#valuesArray

Returns an array of applied filters.

Returns:

  • (Array)

    an array of applied filters



113
114
115
116
117
118
119
120
# File 'lib/blacklight/search_state/filter_field.rb', line 113

def values
  params = search_state.params
  f = Array(params.dig(:f, key))
  f_inclusive = [params.dig(:f_inclusive, key)] if params.dig(:f_inclusive, key).present?
  f_missing = [Blacklight::SearchState::FilterField::MISSING] if params.dig(:f, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param }

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