Class: BlacklightRangeLimit::FilterField
- Inherits:
-
Blacklight::SearchState::FilterField
- Object
- Blacklight::SearchState::FilterField
- BlacklightRangeLimit::FilterField
- Defined in:
- app/presenters/blacklight_range_limit/filter_field.rb
Overview
Modeling access to filter query parameters
Instance Attribute Summary collapse
-
#filters_key ⇒ Object
this accessor is unnecessary after Blacklight 7.25.0.
Instance Method Summary collapse
-
#add(item) ⇒ Blacklight::SearchState
New state.
-
#include? ⇒ Boolean
Whether the provided filter is currently applied/selected.
-
#initialize(config, search_state) ⇒ FilterField
constructor
A new instance of FilterField.
-
#permitted_params ⇒ Object
normal filter fields demangle when they encounter a hash, which they assume to be a number-indexed map this filter should allow (expect) hashes if the keys include ‘begin’ or ‘end’.
-
#remove(item) ⇒ Blacklight::SearchState
New state.
-
#values(except: []) ⇒ Array
An array of applied filters.
Constructor Details
#initialize(config, search_state) ⇒ FilterField
Returns a new instance of FilterField.
9 10 11 12 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 9 def initialize(config, search_state) super @filters_key = :range end |
Instance Attribute Details
#filters_key ⇒ Object
this accessor is unnecessary after Blacklight 7.25.0
7 8 9 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 7 def filters_key @filters_key end |
Instance Method Details
#add(item) ⇒ Blacklight::SearchState
Returns new state.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 16 def add(item) new_state = search_state.reset_search params = new_state.params value = as_url_parameter(item) if value.is_a? Range param_key = filters_key params[param_key] = (params[param_key] || {}).dup params[param_key][config.key] = { begin: value.begin, end: value.end } new_state.reset(params) else super end end |
#include? ⇒ Boolean
Returns whether the provided filter is currently applied/selected.
77 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 77 delegate :include?, to: :values |
#permitted_params ⇒ Object
normal filter fields demangle when they encounter a hash, which they assume to be a number-indexed map this filter should allow (expect) hashes if the keys include ‘begin’ or ‘end’
82 83 84 85 86 87 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 82 def permitted_params { filters_key => { config.key => [:begin, :end], "-#{config.key}" => [] }, inclusive_filters_key => { config.key => [:begin, :end] } } end |
#remove(item) ⇒ Blacklight::SearchState
Returns new state.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 34 def remove(item) new_state = search_state.reset_search params = new_state.params value = as_url_parameter(item) if value.is_a? Range param_key = filters_key params[param_key] = (params[param_key] || {}).dup params[param_key]&.delete(config.key) new_state.reset(params) else super end end |
#values(except: []) ⇒ Array
Returns an array of applied filters.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/presenters/blacklight_range_limit/filter_field.rb', line 50 def values(except: []) params = search_state.params param_key = filters_key range = if !params.try(:dig, param_key).respond_to?(:dig) # bad data, not a hash at all, correct it. Yes, it's bad form to mutate # params here, but we found no better solution -- this only necessary in BL # prior to 8.x, not sure why, but this branch can be omitted in BL 8. params.delete(param_key) nil elsif params.dig(param_key, config.key).is_a? Range params.dig(param_key, config.key) elsif params.dig(param_key, config.key).is_a? Hash b_bound = params.dig(param_key, config.key, :begin).presence e_bound = params.dig(param_key, config.key, :end).presence Range.new(b_bound&.to_i, e_bound&.to_i) if b_bound || e_bound end f = except.include?(:filters) ? [] : [range].compact f_missing = [] if except.include?(:missing) f_missing ||= [Blacklight::SearchState::FilterField::MISSING] if params.dig(filters_key, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param } f + (f_missing || []) end |