Skip to content

Commit

Permalink
Move #url_for_document logic into Blacklight::Path, and make it avail…
Browse files Browse the repository at this point in the history
…able to the controller
  • Loading branch information
cbeer committed Oct 20, 2015
1 parent e1d0e56 commit 8d45f8c
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def action_documents
end

def action_success_redirect_path
catalog_path(params[:id])
blacklight_path.url_for_document(blacklight_config.document_model.new(id: params[:id]))
end

##
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/concerns/blacklight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Blacklight::Controller
helper_method :has_user_authentication_provider?
helper_method :blacklight_config
helper_method :search_action_url, :search_action_path, :search_facet_url
helper_method :blacklight_path


# This callback runs when a user first logs in
Expand All @@ -45,6 +46,11 @@ def blacklight_config

protected

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

# Default route to the search action (used e.g. in global partials). Override this method
# in a controller or in your ApplicationController to introduce custom logic for choosing
# which action the search form should use
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/blacklight/facets_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def path_for_facet(facet_field, item)
if facet_config.url_method
send(facet_config.url_method, facet_field, item)
else
search_action_path(current_path.add_facet_params_and_redirect(facet_field, item))
search_action_path(blacklight_path.add_facet_params_and_redirect(facet_field, item))
end
end

##
# Standard display of a SELECTED facet value (e.g. without a link and with a remove button)
# @params (see #render_facet_value)
def render_selected_facet_value(facet_field, item)
remove_href = search_action_path(current_path.remove_facet_params(facet_field, item))
remove_href = search_action_path(blacklight_path.remove_facet_params(facet_field, item))
content_tag(:span, class: "facet-label") do
content_tag(:span, facet_display_value(facet_field, item), class: "selected") +
# remove link
Expand Down
20 changes: 3 additions & 17 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
##
# URL helper methods
module Blacklight::UrlHelperBehavior

# @return [Blacklight::Path] a memoized instance of the parameter state.
def current_path
@current_path ||= Blacklight::Path.new(params, 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) 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(options)
route[:controller] = controller_name if route[:controller] == :current
route
else
doc
end
def url_for_document(doc, options = {})
blacklight_path.url_for_document(doc, options)
end

# link_to_document(doc, 'VIEW', :counter => 3)
Expand Down Expand Up @@ -154,7 +140,7 @@ def link_to_previous_search(params)
# @param [Blacklight::SolrResponse::Group]
# @return [Hash]
def add_group_facet_params_and_redirect group
current_path.add_facet_params_and_redirect(group.field, group.key, params)
blacklight_path.add_facet_params_and_redirect(group.field, group.key, params)
end

# A URL to refworks export, with an embedded callback URL to this app.
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_per_page_widget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</button>
<ul class="dropdown-menu" role="menu">
<%- per_page_options_for_select.each do |(label, count)| %>
<li><%= link_to(label, url_for(current_path.params_for_search(per_page: count))) %></li>
<li><%= link_to(label, url_for(blacklight_path.params_for_search(per_page: count))) %></li>
<%- end -%>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_search_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_tag search_action_url, method: :get, class: 'search-query-form clearfix navbar-form', role: 'search' do %>
<%= render_hash_as_hidden_fields(current_path.params_for_search.except(:q, :search_field, :qt, :page, :utf8)) %>
<%= render_hash_as_hidden_fields(blacklight_path.params_for_search.except(:q, :search_field, :qt, :page, :utf8)) %>
<div class="input-group">
<% if search_fields.length > 1 %>
<span class="input-group-addon for-search-field">
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_sort_widget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ul class="dropdown-menu" role="menu">
<%- active_sort_fields.each do |sort_key, field_config| %>
<li><%= link_to(field_config.label, url_for(current_path.params_for_search(sort: sort_key))) %></li>
<li><%= link_to(field_config.label, url_for(blacklight_path.params_for_search(sort: sort_key))) %></li>
<%- end -%>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_zero_results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<%- if params[:q] and params[:search_field] and params[:search_field] != blacklight_config.default_search_field.try(:key) -%>
<li><%= t 'blacklight.search.zero_results.search_fields', :search_fields => search_field_label(params) %> -
<%= link_to t('blacklight.search.zero_results.search_everything', field: blacklight_config.default_search_field.label), url_for(current_path.params_for_search(search_field: blacklight_config.default_search_field.key)) %>
<%= link_to t('blacklight.search.zero_results.search_everything', field: blacklight_config.default_search_field.label), url_for(blacklight_path.params_for_search(search_field: blacklight_config.default_search_field.key)) %>
</li>
<%- end %>

Expand Down
12 changes: 12 additions & 0 deletions lib/blacklight/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ def initialize(params, blacklight_config)
@blacklight_config = blacklight_config
end

def url_for_document(doc, options = {})
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(options)
route[:controller] = params[:controller] if route[:controller] == :current
route
else
doc
end
end

# adds the value and/or field to params[:f]
# Does NOT remove request keys and otherwise ensure that the hash
# is suitable for a redirect. See
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/blacklight/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Blacklight::Base do
let(:controller) { (Class.new(ApplicationController) { include Blacklight::Base }).new }
subject { controller}

describe "#blacklight_path" do
let(:params) { double }
before { allow(controller).to receive_messages(params: params) }
subject { controller.send(:blacklight_path) }

it "creates a path object" do
expect(subject).to be_kind_of Blacklight::Path
expect(subject.params).to be params
end
end
end
3 changes: 2 additions & 1 deletion spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ def export_as_mock
before do
CatalogController.add_show_tools_partial(:like, callback: :perform_like, validator: :validate_like_params)
allow(controller).to receive(:perform_like)
allow(controller).to receive(:catalog_path).and_return('catalog/1')
allow(controller).to receive(:solr_document_url).and_return('catalog/1')
allow(controller).to receive(:action_documents).and_return([SolrDocument.new])
Rails.application.routes.draw do
get 'catalog/like', as: :catalog_like
end
Expand Down
15 changes: 2 additions & 13 deletions spec/helpers/facets_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@
allow(helper).to receive(:blacklight_config).and_return blacklight_config
end

describe "#current_path" do
let(:params) { double }
before { allow(helper).to receive_messages(params: params) }
subject { helper.current_path }

it "creates a path object" do
expect(subject).to be_kind_of Blacklight::Path
expect(subject.params).to be params
end
end

describe "has_facet_values?" do
it "should be true if there are any facets to display" do

Expand Down Expand Up @@ -312,11 +301,11 @@

describe "render_facet_value" do
let(:item) { double(:value => 'A', :hits => 10) }
let(:current_path) { double(add_facet_params_and_redirect: { controller: 'catalog' }) }
let(:blacklight_path) { double(add_facet_params_and_redirect: { controller: 'catalog' }) }
before do
allow(helper).to receive(:facet_configuration_for_field).with('simple_field').and_return(double(:query => nil, :date => nil, :helper_method => nil, :single => false, :url_method => nil))
allow(helper).to receive(:facet_display_value).and_return('Z')
allow(helper).to receive(:current_path).and_return(current_path)
allow(helper).to receive(:blacklight_path).and_return(blacklight_path)
allow(helper).to receive(:search_action_path) do |*args|
catalog_index_path *args
end
Expand Down
2 changes: 2 additions & 0 deletions spec/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
before do
allow(helper).to receive_messages(controller: controller_class)
allow(helper).to receive_messages(controller_name: controller_class.controller_name)
allow(helper).to receive_messages(params: {})
end

it "should be a polymorphic routing-ready object" do
Expand All @@ -49,6 +50,7 @@
let(:controller_class) { ::AlternateController.new }
before do
helper.blacklight_config.show.route = { controller: :current }
allow(helper).to receive(:params).and_return(controller: 'alternate')
end
it "should support the :current controller configuration" do
expect(helper.url_for_document(doc)).to eq({controller: 'alternate', action: :show, id: doc})
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@

config.include Devise::TestHelpers, type: :controller
config.infer_spec_type_from_file_location!

config.include(ControllerLevelHelpers, type: :helper)
config.before(:each, type: :helper) { initialize_controller_helpers(helper) }

config.include(ControllerLevelHelpers, type: :view)
config.before(:each, type: :view) { initialize_controller_helpers(view) }
end
13 changes: 13 additions & 0 deletions spec/support/controller_level_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ControllerLevelHelpers
module ControllerViewHelpers
include Blacklight::Facet

def blacklight_path
@blacklight_path ||= Blacklight::Path.new(params, blacklight_config)
end
end

def initialize_controller_helpers(helper)
helper.extend ControllerViewHelpers
end
end

0 comments on commit 8d45f8c

Please sign in to comment.