Skip to content

Commit

Permalink
Pass controller instance to SearchState
Browse files Browse the repository at this point in the history
This allows subclasses to access the url_helpers
  • Loading branch information
jcoyne committed May 2, 2017
1 parent 035a612 commit 279241e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 22 deletions.
15 changes: 11 additions & 4 deletions app/controllers/concerns/blacklight/controller.rb
Expand Up @@ -67,7 +67,15 @@ def render_saved_searches?

# @return [Blacklight::SearchState] a memoized instance of the parameter state.
def search_state
@search_state ||= search_state_class.new(params, blacklight_config)
@search_state ||= begin
if search_state_class.instance_method(:initialize).arity == -3
search_state_class.new(params, blacklight_config, self)
else
Deprecation.warn(search_state_class, "The constructor for #{search_state_class} now requires a third argument. " \
"Invoking it will 2 arguments is deprecated and will be removed in Blacklight 7.")
search_state_class.new(params, blacklight_config)
end
end
end

# Default route to the search action (used e.g. in global partials). Override this method
Expand Down Expand Up @@ -131,7 +139,7 @@ def discard_flash_if_xhr
#
def has_user_authentication_provider?
respond_to? :current_user
end
end

def require_user_authentication_provider
raise ActionController::RoutingError, 'Not Found' unless has_user_authentication_provider?
Expand Down Expand Up @@ -159,7 +167,7 @@ def transfer_guest_user_actions_to_current_user
end

##
# To handle failed authorization attempts, redirect the user to the
# To handle failed authorization attempts, redirect the user to the
# login form and persist the current request uri as a parameter
def access_denied
# send the user home if the access was previously denied by the same
Expand All @@ -171,5 +179,4 @@ def access_denied

redirect_to new_user_session_url(:referer => request.fullpath)
end

end
14 changes: 7 additions & 7 deletions app/helpers/blacklight/render_constraints_helper_behavior.rb
Expand Up @@ -64,14 +64,14 @@ def remove_constraint_url(localized_params)
# @param [Hash] localized_params query parameters
# @return [String]
def render_constraints_filters(localized_params = params)
return "".html_safe unless localized_params[:f]
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)
end
return "".html_safe unless localized_params[:f]
path = controller.search_state_class.new(localized_params, blacklight_config, controller)
content = []
localized_params[:f].each_pair do |facet,values|
content << render_filter_element(facet, values, path)
end

safe_join(content.flatten, "\n")
safe_join(content.flatten, "\n")
end

##
Expand Down
12 changes: 9 additions & 3 deletions lib/blacklight/search_state.rb
Expand Up @@ -6,11 +6,16 @@ class SearchState
attr_reader :blacklight_config # Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
attr_reader :params

# This method is never accessed in this class, but may be used by subclasses that need
# to access the url_helpers
attr_reader :controller

delegate :facet_configuration_for_field, to: :blacklight_config

# @param [ActionController::Parameters] params
# @param [Blacklight::Config] blacklight_config
def initialize(params, blacklight_config)
# @param [ApplicationController] controller used for the routing helpers
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
Expand All @@ -24,6 +29,7 @@ def initialize(params, blacklight_config)
end

@blacklight_config = blacklight_config
@controller = controller
end

def to_hash
Expand All @@ -32,7 +38,7 @@ def to_hash
alias to_h to_hash

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

##
Expand Down Expand Up @@ -120,7 +126,7 @@ def remove_facet_params(field, item)
# @yield [params] The merged parameters hash before being sanitized
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))
my_params = params.dup.merge(self.class.new(params_to_merge, blacklight_config, controller))

if block_given?
yield my_params
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/catalog_helper_spec.rb
Expand Up @@ -134,7 +134,7 @@ def render_grouped_response?
let(:my_engine) { double("Engine") }
let(:query_params) { { controller: 'catalog', action: 'index' } }
let(:config) { Blacklight::Configuration.new }
let(:search_state) { Blacklight::SearchState.new(query_params, config) }
let(:search_state) { Blacklight::SearchState.new(query_params, config, controller) }

it "calls url_for on the engine scope" do
allow(helper).to receive(:search_state).and_return search_state
Expand All @@ -152,7 +152,7 @@ def render_grouped_response?
let(:my_engine) { double("Engine") }
let(:query_params) { { controller: 'catalog', action: 'index' } }
let(:config) { Blacklight::Configuration.new }
let(:search_state) { Blacklight::SearchState.new(query_params, config) }
let(:search_state) { Blacklight::SearchState.new(query_params, config, controller) }

it "calls url_for on the engine scope" do
allow(helper).to receive(:search_state).and_return search_state
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/render_constraints_helper_spec.rb
Expand Up @@ -50,7 +50,7 @@
subject { helper.render_filter_element('type', ['journal'], path) }

let(:params) { ActionController::Parameters.new q: 'biz' }
let(:path) { Blacklight::SearchState.new(params, config) }
let(:path) { Blacklight::SearchState.new(params, config, controller) }

it "has a link relative to the current url" do
expect(subject).to have_link "Remove constraint Item Type: journal", href: "/catalog?q=biz"
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/blacklight/search_state_spec.rb
Expand Up @@ -9,7 +9,8 @@
end

let(:parameter_class) { ActionController::Parameters }
let(:search_state) { described_class.new(params, blacklight_config) }
let(:controller) { double }
let(:search_state) { described_class.new(params, blacklight_config, controller) }
let(:params) { parameter_class.new }

describe '#to_h' do
Expand Down
3 changes: 2 additions & 1 deletion spec/presenters/blacklight/link_alternate_presenter_spec.rb
Expand Up @@ -8,7 +8,8 @@
let(:config) { Blacklight::Configuration.new }
let(:parameter_class) { ActionController::Parameters }
let(:params) { parameter_class.new }
let(:search_state) { Blacklight::SearchState.new(params, config) }
let(:controller) { double }
let(:search_state) { Blacklight::SearchState.new(params, config, controller) }

let(:presenter) { described_class.new(view_context, document, options) }
before do
Expand Down
3 changes: 2 additions & 1 deletion spec/presenters/index_presenter_spec.rb
Expand Up @@ -9,7 +9,8 @@
let(:presenter) { described_class.new(document, request_context, config) }
let(:parameter_class) { ActionController::Parameters }
let(:params) { parameter_class.new }
let(:search_state) { Blacklight::SearchState.new(params, config) }
let(:controller) { double }
let(:search_state) { Blacklight::SearchState.new(params, config, controller) }

let(:document) do
SolrDocument.new(id: 1,
Expand Down
3 changes: 2 additions & 1 deletion spec/presenters/show_presenter_spec.rb
Expand Up @@ -9,7 +9,8 @@
let(:presenter) { described_class.new(document, request_context, config) }
let(:parameter_class) { ActionController::Parameters }
let(:params) { parameter_class.new }
let(:search_state) { Blacklight::SearchState.new(params, config) }
let(:controller) { double }
let(:search_state) { Blacklight::SearchState.new(params, config, controller) }

let(:document) do
SolrDocument.new(id: 1,
Expand Down
2 changes: 1 addition & 1 deletion spec/support/controller_level_helpers.rb
Expand Up @@ -4,7 +4,7 @@ module ControllerViewHelpers
include Blacklight::Facet

def search_state
@search_state ||= Blacklight::SearchState.new(params, blacklight_config)
@search_state ||= Blacklight::SearchState.new(params, blacklight_config, controller)
end

def blacklight_configuration_context
Expand Down

0 comments on commit 279241e

Please sign in to comment.