class Blacklight::SearchState
This class encapsulates the search state as represented by the query parameters namely: :f, :q, :page, :per_page and, :sort
Attributes
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers
Public Class Methods
Source
# File lib/blacklight/search_state.rb, line 21 def initialize(params, blacklight_config, controller = nil) @blacklight_config = blacklight_config @controller = controller @params = Blacklight::Parameters.new(params, self).permit_search_params.to_h.with_indifferent_access end
@param [ActionController::Parameters] params @param [Blacklight::Config] blacklight_config
@param [ApplicationController] controller used for the routing helpers
Public Instance Methods
Source
# File lib/blacklight/search_state.rb, line 105 def add_facet_params_and_redirect(field, item) new_params = filter(field).add(item).to_h # Delete any request params from facet-specific action, needed # to redir to index action properly. request_keys = blacklight_config.facet_paginator_class.request_keys new_params.extract!(*request_keys.values) new_params end
Used in catalog/facet action, facets.rb view, for a click on a facet value. Add on the facet params to existing search constraints. Remove any paginator-specific request params, or other request params that should be removed for a ‘fresh’ display. Change the action to ‘index’ to send them back to catalog/index with their new facet choice.
Source
# File lib/blacklight/search_state.rb, line 40 def clause_params params[:clause] || {} end
Source
# File lib/blacklight/search_state.rb, line 159 def facet_page [params[facet_request_keys[:page]].to_i, 1].max end
Source
# File lib/blacklight/search_state.rb, line 167 def facet_prefix params[facet_request_keys[:prefix]] end
Source
# File lib/blacklight/search_state.rb, line 163 def facet_sort params[facet_request_keys[:sort]] end
Source
# File lib/blacklight/search_state.rb, line 90 def filter(field_key_or_field) field = field_key_or_field if field_key_or_field.is_a? Blacklight::Configuration::Field field ||= blacklight_config.facet_fields[field_key_or_field] field ||= Blacklight::Configuration::NullField.new(key: field_key_or_field) (field.filter_class || FilterField).new(field, self) end
@return [FilterField]
Source
# File lib/blacklight/search_state.rb, line 81 def filter_fields blacklight_config.facet_fields.each_value.map { |value| filter(value) } end
Source
# File lib/blacklight/search_state.rb, line 85 def filters @filters ||= filter_fields.select(&:any?) end
Source
# File lib/blacklight/search_state.rb, line 32 def has_constraints? !(query_param.blank? && filters.blank? && clause_params.blank?) end
Source
# File lib/blacklight/search_state.rb, line 135 def page [params[:page].to_i, 1].max end
Source
# File lib/blacklight/search_state.rb, line 120 def params_for_search(params_to_merge = {}) # params hash we'll return my_params = to_h.merge(self.class.new(params_to_merge, blacklight_config, controller)) if block_given? yield my_params end if my_params[:page] && (my_params[:per_page] != params[:per_page] || my_params[:sort] != params[:sort]) my_params[:page] = 1 end Parameters.sanitize(my_params) end
Merge the source params with the params_to_merge hash @param [Hash] params_to_merge to merge into above @return [ActionController::Parameters] the current search parameters after being sanitized by Blacklight::Parameters.sanitize
@yield [params] The merged parameters hash before being sanitized
Source
# File lib/blacklight/search_state.rb, line 139 def per_page params[:rows].presence&.to_i || params[:per_page].presence&.to_i || blacklight_config.default_per_page end
Source
# File lib/blacklight/search_state.rb, line 75 def remove_query_params p = reset_search_params p.delete(:q) p end
Source
# File lib/blacklight/search_state.rb, line 45 def reset(params = nil) self.class.new(params || {}, blacklight_config, controller) end
@return [Blacklight::SearchState]
Source
# File lib/blacklight/search_state.rb, line 50 def reset_search(additional_params = {}) reset(reset_search_params.merge(additional_params)) end
@return [Blacklight::SearchState]
Source
# File lib/blacklight/search_state.rb, line 69 def routable?(doc) return false unless respond_to?(:blacklight_config) && blacklight_config.view_config(:show).route doc.is_a? routable_model_for(blacklight_config) end
To build a show route, we must have a blacklight_config
that has configured show views, and the doc must appropriate to the config @return [Boolean]
Source
# File lib/blacklight/search_state.rb, line 155 def search_field blacklight_config.search_fields[search_field_key] end
Source
# File lib/blacklight/search_state.rb, line 145 def sort_field if sort_field_key.blank? # no sort param provided, use default blacklight_config.default_sort_field else # check for sort field key blacklight_config.sort_fields[sort_field_key] end end
Source
# File lib/blacklight/search_state.rb, line 27 def to_hash params.deep_dup end
Source
# File lib/blacklight/search_state.rb, line 58 def url_for_document(doc, options = {}) return doc unless routable?(doc) route = blacklight_config.view_config(:show).route.merge(action: :show, id: doc).merge(options) route[:controller] = params[:controller] if route[:controller] == :current route end
Extension point for downstream applications to provide more interesting routing to documents
Private Instance Methods
Source
# File lib/blacklight/search_state.rb, line 185 def facet_request_keys blacklight_config.facet_paginator_class.request_keys end
Source
# File lib/blacklight/search_state.rb, line 193 def reset_search_params Parameters.sanitize(to_h).except(:page, :counter) end
Reset any search parameters that store search context and need to be reset when e.g. constraints change @return [ActionController::Parameters]
Source
# File lib/blacklight/search_state.rb, line 173 def routable_model_for(blacklight_config) blacklight_config.document_model || ::SolrDocument end
Source
# File lib/blacklight/search_state.rb, line 177 def search_field_key params[:search_field] end
Source
# File lib/blacklight/search_state.rb, line 181 def sort_field_key params[:sort] end