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
Public Class Methods
@param [ActionController::Parameters] params @param [Blacklight::Config] blacklight_config
@param [ApplicationController] controller used for the routing helpers
# File lib/blacklight/search_state.rb, line 23 def initialize(params, blacklight_config, controller = nil) @params = self.class.normalize_params(params) @blacklight_config = blacklight_config @controller = controller end
# File lib/blacklight/search_state.rb, line 29 def self.normalize_params(untrusted_params = {}) params = untrusted_params if params.respond_to?(:to_unsafe_h) # This is the typical (not-ActionView::TestCase) code path. params = params.to_unsafe_h # In Rails 5 to_unsafe_h returns a HashWithIndifferentAccess, in Rails 4 it returns Hash params = params.with_indifferent_access if params.instance_of? Hash elsif params.is_a? Hash # This is an ActionView::TestCase workaround for Rails 4.2. params = params.dup.with_indifferent_access else params = params.dup.to_h.with_indifferent_access end # Normalize facet parameters mangled by facebook if params[:f].is_a?(Hash) && params[:f].values.any? { |x| x.is_a?(Hash) } params[:f] = params[:f].transform_values do |value| value.is_a?(Hash) ? value.values : value end end params end
Public Instance Methods
adds the value and/or field to params Does NOT remove request keys and otherwise ensure that the hash is suitable for a redirect. See add_facet_params_and_redirect
# File lib/blacklight/search_state.rb, line 154 def add_facet_params(field, item) filter(field).add(item).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.
# File lib/blacklight/search_state.rb, line 166 def add_facet_params_and_redirect(field, item) new_params = Deprecation.silence(self.class) do add_facet_params(field, item) end # 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
# File lib/blacklight/search_state.rb, line 93 def clause_params params[:clause] || {} end
# File lib/blacklight/search_state.rb, line 242 def facet_page [params[facet_request_keys[:page]].to_i, 1].max end
# File lib/blacklight/search_state.rb, line 250 def facet_prefix params[facet_request_keys[:prefix]] end
# File lib/blacklight/search_state.rb, line 246 def facet_sort params[facet_request_keys[:sort]] end
# File lib/blacklight/search_state.rb, line 142 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
# File lib/blacklight/search_state.rb, line 97 def filter_params params[:f] || {} end
# File lib/blacklight/search_state.rb, line 134 def filters @filters ||= blacklight_config.facet_fields.each_value.map do |value| f = filter(value) f if f.any? end.compact end
# File lib/blacklight/search_state.rb, line 83 def has_constraints? Deprecation.silence(Blacklight::SearchState) do !(query_param.blank? && filter_params.blank? && filters.blank? && clause_params.blank?) end end
# File lib/blacklight/search_state.rb, line 190 def has_facet?(config, value: nil) if value filter(config).include?(value) else filter(config).any? end end
# File lib/blacklight/search_state.rb, line 64 def method_missing(method_name, *arguments, &block) if @params.respond_to?(method_name) Deprecation.warn(self.class, "Calling `#{method_name}` on Blacklight::SearchState " \ 'is deprecated and will be removed in Blacklight 8. Call #to_h first if you ' \ ' need to use hash methods (or, preferably, use your own SearchState implementation)') @params.public_send(method_name, *arguments, &block) else super end end
# File lib/blacklight/search_state.rb, line 218 def page [params[:page].to_i, 1].max 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
# File lib/blacklight/search_state.rb, line 203 def params_for_search(params_to_merge = {}) # params hash we'll return my_params = params.dup.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
# File lib/blacklight/search_state.rb, line 222 def per_page params[:rows].presence&.to_i || params[:per_page].presence&.to_i || blacklight_config.default_per_page end
# File lib/blacklight/search_state.rb, line 89 def query_param params[:q] end
copies the current params (or whatever is passed in as the 3rd arg) removes the field value from params removes the field if there are no more values in params[field] removes additional params (page, id, etc..) @param [String] field @param [String] item
# File lib/blacklight/search_state.rb, line 185 def remove_facet_params(field, item) filter(field).remove(item).params end
# File lib/blacklight/search_state.rb, line 128 def remove_query_params p = reset_search_params p.delete(:q) p end
@return [Blacklight::SearchState]
# File lib/blacklight/search_state.rb, line 103 def reset(params = nil) self.class.new(params || ActionController::Parameters.new, blacklight_config, controller) end
@return [Blacklight::SearchState]
# File lib/blacklight/search_state.rb, line 108 def reset_search(additional_params = {}) reset(reset_search_params.merge(additional_params)) end
# File lib/blacklight/search_state.rb, line 75 def respond_to_missing?(method_name, include_private = false) @params.respond_to?(method_name, include_private) || super end
# File lib/blacklight/search_state.rb, line 238 def search_field blacklight_config.search_fields[search_field_key] end
# File lib/blacklight/search_state.rb, line 228 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
# File lib/blacklight/search_state.rb, line 54 def to_hash @params.deep_dup end
# File lib/blacklight/search_state.rb, line 59 def to_unsafe_h Deprecation.warn(self.class, 'Use SearchState#to_h instead of SearchState#to_unsafe_h') to_hash end
Extension point for downstream applications to provide more interesting routing to documents
# File lib/blacklight/search_state.rb, line 116 def url_for_document(doc, options = {}) if respond_to?(:blacklight_config) && blacklight_config.view_config(:show).route && (!doc.respond_to?(:to_model) || doc.to_model.is_a?(SolrDocument)) route = blacklight_config.view_config(:show).route.merge(action: :show, id: doc).merge(options) route[:controller] = params[:controller] if route[:controller] == :current route else doc end end
Private Instance Methods
# File lib/blacklight/search_state.rb, line 264 def facet_request_keys blacklight_config.facet_paginator_class.request_keys end
Reset any search parameters that store search context and need to be reset when e.g. constraints change @return [ActionController::Parameters]
# File lib/blacklight/search_state.rb, line 272 def reset_search_params Parameters.sanitize(params).except(:page, :counter) end
# File lib/blacklight/search_state.rb, line 256 def search_field_key params[:search_field] end
# File lib/blacklight/search_state.rb, line 260 def sort_field_key params[:sort] end