Class: Geoblacklight::BboxFilterField

Inherits:
Blacklight::SearchState::FilterField
  • Object
show all
Defined in:
app/models/concerns/geoblacklight/bbox_filter_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BboxFilterField

Returns a new instance of BboxFilterField.



8
9
10
11
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 8

def initialize(*args)
  super
  @filters_key = :bbox
end

Instance Attribute Details

#filters_keyObject

this accessor is unnecessary after Blacklight 7.25.0



6
7
8
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 6

def filters_key
  @filters_key
end

Instance Method Details

#add(item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • a (String, #value)

    filter item to add to the url

Returns:

  • (Blacklight::SearchState)

    new state



15
16
17
18
19
20
21
22
23
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 15

def add(item)
  new_state = search_state.reset_search
  params = new_state.params
  value = as_url_parameter(item)

  params[filters_key] = value.to_param

  new_state.reset(params)
end

#include?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



49
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 49

delegate :include?, to: :values

#needs_normalization?(value_params) ⇒ Boolean

normal filter fields demangle when they encounter a hash, which they assume to be a number-indexed map

Returns:

  • (Boolean)

Since:

  • Blacklight v7.25.0



53
54
55
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 53

def needs_normalization?(value_params)
  value_params.is_a?(Hash) && value_params.keys.map(&:to_s).all? { |k| k =~ /^\d+$/ }
end

#normalize(value_params) ⇒ Object

value should be the first value from a mangled hash, otherwise return the value as-is

Since:

  • Blacklight v7.25.0



60
61
62
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 60

def normalize(value_params)
  needs_normalization?(value_params) ? value_params.values : value_params
end

#remove(_item) ⇒ Blacklight::SearchState

Returns new state.

Parameters:

  • a (String, #value)

    filter to remove from the url

Returns:

  • (Blacklight::SearchState)

    new state



27
28
29
30
31
32
33
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 27

def remove(_item)
  new_state = search_state.reset_search
  params = new_state.params

  params.delete(filters_key)
  new_state.reset(params)
end

#values(except: []) ⇒ Array

Returns an array of applied filters.

Returns:

  • (Array)

    an array of applied filters



36
37
38
39
40
41
42
43
44
45
# File 'app/models/concerns/geoblacklight/bbox_filter_field.rb', line 36

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

  [Geoblacklight::BoundingBox.from_rectangle(params[filters_key])]
rescue Geoblacklight::Exceptions::WrongBoundingBoxFormat => e
  Rails.logger.warn(e)

  []
end