Skip to content

Commit

Permalink
Allow the SearchState implementation to be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 6, 2017
1 parent 01de647 commit 343dc88
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
7 changes: 6 additions & 1 deletion app/controllers/concerns/blacklight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ module Blacklight::Controller
helper_method :search_action_url, :search_action_path, :search_facet_url, :search_facet_path
helper_method :search_state

# Specify which class to use for the search state. You can subclass SearchState if you
# want to override any of the methods (e.g. SearchState#url_for_document)
class_attribute :search_state_class
self.search_state_class = Blacklight::SearchState

# This callback runs when a user first logs in

define_callbacks :logging_in_user
Expand Down Expand Up @@ -64,7 +69,7 @@ def render_saved_searches?

# @return [Blacklight::SearchState] a memoized instance of the parameter state.
def search_state
@search_state ||= Blacklight::SearchState.new(params, blacklight_config)
@search_state ||= search_state_class.new(params, blacklight_config)
end

# Default route to the search action (used e.g. in global partials). Override this method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def remove_constraint_url(localized_params)
# @return [String]
def render_constraints_filters(localized_params = params)
return "".html_safe unless localized_params[:f]
path = Blacklight::SearchState.new(localized_params, blacklight_config)
path = controller.search_state_class.new(localized_params, blacklight_config)
content = []
localized_params[:f].each_pair do |facet, values|
content << render_filter_element(facet, values, path)
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
##
# URL helper methods
module Blacklight::UrlHelperBehavior
##
# Extension point for downstream applications
# to provide more interesting routing to
# documents
def url_for_document(doc, options = {})
search_state.url_for_document(doc, options)
end
Expand Down
8 changes: 6 additions & 2 deletions lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def to_hash
alias to_h to_hash

def reset
Blacklight::SearchState.new(ActionController::Parameters.new, blacklight_config)
self.class.new(ActionController::Parameters.new, blacklight_config)
end

##
# Extension point for downstream applications
# to provide more interesting routing to
# documents
def url_for_document(doc, options = {})
if respond_to?(:blacklight_config) &&
blacklight_config.show.route &&
Expand Down Expand Up @@ -116,7 +120,7 @@ def remove_facet_params(field, item)
# @yield [params] The merged parameters hash before being sanitized
def params_for_search(params_to_merge = {})
# params hash we'll return
my_params = params.dup.merge(Blacklight::SearchState.new(params_to_merge, blacklight_config))
my_params = params.dup.merge(self.class.new(params_to_merge, blacklight_config))

if block_given?
yield my_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
let(:params) { ActionController::Parameters.new f: { 'type' => [''] } }
before do
allow(helper).to receive(:blacklight_config).and_return(config)
allow(controller).to receive(:search_state_class).and_return(Blacklight::SearchState)
end
subject { helper.render_constraints_filters(params) }

Expand Down

0 comments on commit 343dc88

Please sign in to comment.