Class: Blacklight::SearchState
- Inherits:
-
Object
- Object
- Blacklight::SearchState
- Defined in:
- lib/blacklight/search_state.rb
Overview
This class encapsulates the search state as represented by the query parameters namely: :f, :q, :page, :per_page and, :sort
Instance Attribute Summary collapse
-
#blacklight_config ⇒ Object
readonly
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
-
#controller ⇒ Object
readonly
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#add_facet_params(field, item) ⇒ Object
adds the value and/or field to params Does NOT remove request keys and otherwise ensure that the hash is suitable for a redirect.
-
#add_facet_params_and_redirect(field, item) ⇒ Object
Used in catalog/facet action, facets.rb view, for a click on a facet value.
-
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
constructor
A new instance of SearchState.
-
#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters
Merge the source params with the params_to_merge hash.
- #remove_facet_params(field, item) ⇒ Object
- #reset(params = nil) ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
-
#url_for_document(doc, options = {}) ⇒ Object
Extension point for downstream applications to provide more interesting routing to documents.
Constructor Details
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
Returns a new instance of SearchState.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/blacklight/search_state.rb', line 18 def initialize(params, blacklight_config, controller = nil) 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 @blacklight_config = blacklight_config @controller = controller end |
Instance Attribute Details
#blacklight_config ⇒ Object (readonly)
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
6 7 8 |
# File 'lib/blacklight/search_state.rb', line 6 def blacklight_config @blacklight_config end |
#controller ⇒ Object (readonly)
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers
11 12 13 |
# File 'lib/blacklight/search_state.rb', line 11 def controller @controller end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/blacklight/search_state.rb', line 7 def params @params end |
Instance Method Details
#add_facet_params(field, item) ⇒ Object
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
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/blacklight/search_state.rb', line 64 def add_facet_params(field, item) p = reset_search_params add_facet_param(p, field, item) if item and item.respond_to?(:fq) and item.fq Array(item.fq).each do |f, v| add_facet_param(p, f, v) end end p end |
#add_facet_params_and_redirect(field, item) ⇒ Object
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.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/blacklight/search_state.rb', line 85 def add_facet_params_and_redirect(field, item) new_params = add_facet_params(field, item) # 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 |
#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters
Merge the source params with the params_to_merge hash
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/blacklight/search_state.rb', line 135 def params_for_search(params_to_merge={}, &block) # 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] and (my_params[:per_page] != params[:per_page] or my_params[:sort] != params[:sort] ) my_params[:page] = 1 end Parameters.sanitize(my_params) end |
#remove_facet_params(field, item) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/blacklight/search_state.rb', line 100 def remove_facet_params(field, item) if item.respond_to? :field field = item.field end facet_config = facet_configuration_for_field(field) url_field = facet_config.key value = facet_value_for_facet_item(item) p = reset_search_params # 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... p[:f] = (p[:f] || {}).dup p[:f][url_field] = (p[:f][url_field] || []).dup collection = p[:f][url_field] # 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 collection = collection.values end p[:f][url_field] = collection - [value] p[:f].delete(url_field) if p[:f][url_field].empty? p.delete(:f) if p[:f].empty? p end |
#reset(params = nil) ⇒ Object
40 41 42 |
# File 'lib/blacklight/search_state.rb', line 40 def reset(params = nil) self.class.new(params || ActionController::Parameters.new, blacklight_config, controller) end |
#to_hash ⇒ Object Also known as: to_h
35 36 37 |
# File 'lib/blacklight/search_state.rb', line 35 def to_hash @params end |
#url_for_document(doc, options = {}) ⇒ Object
Extension point for downstream applications to provide more interesting routing to documents
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/blacklight/search_state.rb', line 48 def url_for_document(doc, = {}) if respond_to?(:blacklight_config) and blacklight_config.show.route and (!doc.respond_to?(:to_model) or doc.to_model.is_a? SolrDocument) route = blacklight_config.show.route.merge(action: :show, id: doc).merge() route[:controller] = params[:controller] if route[:controller] == :current route else doc end end |