class Blacklight::SearchState::FilterField
Modeling access to filter query parameters
Attributes
config[R]
@param [Blacklight::Configuration::FacetField] config
search_state[R]
@param [Blacklight::SearchState] search_state
Public Class Methods
new(config, search_state)
click to toggle source
@param [Blacklight::Configuration::FacetField] config @param [Blacklight::SearchState] search_state
# File lib/blacklight/search_state/filter_field.rb, line 18 def initialize(config, search_state) @config = config @search_state = search_state end
Public Instance Methods
add(item)
click to toggle source
@param [String,#value] a filter item to add to the url @return [Blacklight::SearchState] new state
# File lib/blacklight/search_state/filter_field.rb, line 25 def add(item) new_state = search_state.reset_search if 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 params = new_state.params param = :f value = as_url_parameter(item) 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][key] = value elsif config.single params[param][key] = [value] else params[param][key] = Array(params[param][key] || []).dup params[param][key].push(value) end new_state.reset(params) end
include?(item)
click to toggle source
@param [String,#value] a filter to remove from the url @return [Boolean] whether the provided filter is currently applied/selected
# File lib/blacklight/search_state/filter_field.rb, line 114 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 else (params.dig(:f, key) || []).include?(value) end end
remove(item)
click to toggle source
@param [String,#value] a filter to remove from the url @return [Blacklight::SearchState] new state
# File lib/blacklight/search_state/filter_field.rb, line 60 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 params = new_state.params param = :f value = as_url_parameter(item) 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][key] = (params[param][key] || []).dup collection = params[param][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][key] = collection - Array(value) params[param].delete(key) if params[param][key].empty? params.delete(param) if params[param].empty? # Handle missing field queries. missing = I18n.t("blacklight.search.facets.missing") if (item.respond_to?(:fq) && item.fq == "-#{key}:[* TO *]") || item == missing params[param].delete("-#{key}:") params[param].delete(key) if params[param][key] == [""] end new_state.reset(params) end
values()
click to toggle source
@return [Array] an array of applied filters
# File lib/blacklight/search_state/filter_field.rb, line 103 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 + (f_inclusive || []) end
Private Instance Methods
as_url_parameter(item)
click to toggle source
TODO: this code is duplicated in Blacklight::FacetsHelperBehavior
# File lib/blacklight/search_state/filter_field.rb, line 132 def as_url_parameter(item) if item.respond_to? :value item.value else item end end