Class: Blacklight::SearchState
Overview
This class encapsulates the search state as represented by the query parameters namely: :f, :q, :page, :per_page and, :sort
Defined Under Namespace
Classes: FilterField
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.
-
#clause_params ⇒ Object
-
#facet_page ⇒ Object
-
#facet_prefix ⇒ Object
-
#facet_sort ⇒ Object
-
#filter(field_key_or_field) ⇒ Object
-
#filter_fields ⇒ Object
-
#filter_params ⇒ Object
-
#filters ⇒ Object
-
#has_constraints? ⇒ Boolean
-
#has_facet?(config, value: nil) ⇒ Boolean
-
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
constructor
A new instance of SearchState.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
-
#page ⇒ Object
-
#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters
Merge the source params with the params_to_merge hash.
-
#per_page ⇒ Object
-
#query_param ⇒ Object
-
#remove_facet_params(field, item) ⇒ Object
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..).
-
#remove_query_params ⇒ Object
-
#reset(params = nil) ⇒ Blacklight::SearchState
-
#reset_search(additional_params = {}) ⇒ Blacklight::SearchState
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#search_field ⇒ Object
-
#sort_field ⇒ Object
-
#to_hash ⇒ Object
(also: #to_h)
-
#to_unsafe_h ⇒ Object
-
#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.
25
26
27
28
29
|
# File 'lib/blacklight/search_state.rb', line 25
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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/blacklight/search_state.rb', line 41
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
|
Instance Attribute Details
#blacklight_config ⇒ Object
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
13
14
15
|
# File 'lib/blacklight/search_state.rb', line 13
def blacklight_config
@blacklight_config
end
|
#controller ⇒ Object
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers
18
19
20
|
# File 'lib/blacklight/search_state.rb', line 18
def controller
@controller
end
|
#params ⇒ Object
Returns the value of attribute params.
14
15
16
|
# File 'lib/blacklight/search_state.rb', line 14
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
131
132
133
|
# File 'lib/blacklight/search_state.rb', line 131
def add_facet_params(field, item)
filter(field).add(item).params
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.
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/blacklight/search_state.rb', line 143
def add_facet_params_and_redirect(field, item)
new_params = Deprecation.silence(self.class) do
add_facet_params(field, item).to_h.with_indifferent_access
end
request_keys = blacklight_config.facet_paginator_class.request_keys
new_params.(*request_keys.values)
new_params
end
|
#clause_params ⇒ Object
70
71
72
|
# File 'lib/blacklight/search_state.rb', line 70
def clause_params
params[:clause] || {}
end
|
#facet_page ⇒ Object
219
220
221
|
# File 'lib/blacklight/search_state.rb', line 219
def facet_page
[params[facet_request_keys[:page]].to_i, 1].max
end
|
#facet_prefix ⇒ Object
227
228
229
|
# File 'lib/blacklight/search_state.rb', line 227
def facet_prefix
params[facet_request_keys[:prefix]]
end
|
#facet_sort ⇒ Object
223
224
225
|
# File 'lib/blacklight/search_state.rb', line 223
def facet_sort
params[facet_request_keys[:sort]]
end
|
#filter(field_key_or_field) ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/blacklight/search_state.rb', line 119
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
|
#filter_fields ⇒ Object
111
112
113
|
# File 'lib/blacklight/search_state.rb', line 111
def filter_fields
blacklight_config.facet_fields.each_value.map { |value| filter(value) }
end
|
#filter_params ⇒ Object
74
75
76
|
# File 'lib/blacklight/search_state.rb', line 74
def filter_params
params[:f] || {}
end
|
#filters ⇒ Object
115
116
117
|
# File 'lib/blacklight/search_state.rb', line 115
def filters
@filters ||= filter_fields.select(&:any?)
end
|
#has_constraints? ⇒ Boolean
60
61
62
63
64
|
# File 'lib/blacklight/search_state.rb', line 60
def has_constraints?
Deprecation.silence(Blacklight::SearchState) do
!(query_param.blank? && filter_params.blank? && filters.blank? && clause_params.blank?)
end
end
|
#has_facet?(config, value: nil) ⇒ Boolean
167
168
169
170
171
172
173
|
# File 'lib/blacklight/search_state.rb', line 167
def has_facet?(config, value: nil)
if value
filter(config).include?(value)
else
filter(config).any?
end
end
|
#page ⇒ Object
195
196
197
|
# File 'lib/blacklight/search_state.rb', line 195
def page
[params[:page].to_i, 1].max
end
|
#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters
Merge the source params with the params_to_merge hash
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/blacklight/search_state.rb', line 180
def params_for_search(params_to_merge = {})
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
|
#per_page ⇒ Object
199
200
201
202
203
|
# File 'lib/blacklight/search_state.rb', line 199
def per_page
params[:rows].presence&.to_i ||
params[:per_page].presence&.to_i ||
blacklight_config.default_per_page
end
|
#query_param ⇒ Object
66
67
68
|
# File 'lib/blacklight/search_state.rb', line 66
def query_param
params[:q]
end
|
#remove_facet_params(field, item) ⇒ Object
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..)
162
163
164
|
# File 'lib/blacklight/search_state.rb', line 162
def remove_facet_params(field, item)
filter(field).remove(item).params
end
|
#remove_query_params ⇒ Object
105
106
107
108
109
|
# File 'lib/blacklight/search_state.rb', line 105
def remove_query_params
p = reset_search_params
p.delete(:q)
p
end
|
80
81
82
|
# File 'lib/blacklight/search_state.rb', line 80
def reset(params = nil)
self.class.new(params || {}, blacklight_config, controller)
end
|
85
86
87
|
# File 'lib/blacklight/search_state.rb', line 85
def reset_search(additional_params = {})
reset(reset_search_params.merge(additional_params))
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
52
53
54
|
# File 'lib/blacklight/search_state.rb', line 52
def respond_to_missing?(method_name, include_private = false)
@params.respond_to?(method_name, include_private) || super
end
|
#search_field ⇒ Object
215
216
217
|
# File 'lib/blacklight/search_state.rb', line 215
def search_field
blacklight_config.search_fields[search_field_key]
end
|
#sort_field ⇒ Object
205
206
207
208
209
210
211
212
213
|
# File 'lib/blacklight/search_state.rb', line 205
def sort_field
if sort_field_key.blank?
blacklight_config.default_sort_field
else
blacklight_config.sort_fields[sort_field_key]
end
end
|
#to_hash ⇒ Object
Also known as:
to_h
31
32
33
|
# File 'lib/blacklight/search_state.rb', line 31
def to_hash
params.deep_dup
end
|
#to_unsafe_h ⇒ Object
36
37
38
39
|
# File 'lib/blacklight/search_state.rb', line 36
def to_unsafe_h
Deprecation.warn(self.class, 'Use SearchState#to_h instead of SearchState#to_unsafe_h')
to_hash
end
|
#url_for_document(doc, options = {}) ⇒ Object
Extension point for downstream applications to provide more interesting routing to documents
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/blacklight/search_state.rb', line 93
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
|