Skip to content

Commit

Permalink
Extract path concerns from the helpers to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 19, 2015
1 parent 9fd425e commit 1947978
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 406 deletions.
14 changes: 9 additions & 5 deletions app/helpers/blacklight/facets_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,24 @@ def render_facet_value(facet_field, item, options ={})
def path_for_facet(facet_field, item)
facet_config = facet_configuration_for_field(facet_field)
if facet_config.url_method
path = send(facet_config.url_method, facet_field, item)
send(facet_config.url_method, facet_field, item)
else
path = search_action_path(add_facet_params_and_redirect(facet_field, item))
search_action_path(current_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)
content_tag(:span, :class => "facet-label") do
content_tag(:span, facet_display_value(facet_field, item), :class => "selected") +
remove_href = search_action_path(current_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
link_to(content_tag(:span, '', :class => "glyphicon glyphicon-remove") + content_tag(:span, '[remove]', :class => 'sr-only'), search_action_path(remove_facet_params(facet_field, item, params)), :class=>"remove")
link_to(remove_href, class: "remove") do
content_tag(:span, '', class: "glyphicon glyphicon-remove") +
content_tag(:span, '[remove]', class: 'sr-only')
end
end + render_facet_count(item.hits, :classes => ["selected"])
end

Expand Down
19 changes: 9 additions & 10 deletions app/helpers/blacklight/render_constraints_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,33 @@ def render_constraints_query(localized_params = params)

##
# Render the facet constraints
#
# @param [Hash] query parameters
# @param [Hash] localized_params query parameters
# @return [String]
def render_constraints_filters(localized_params = params)
return "".html_safe unless localized_params[:f]
path = Blacklight::Path.new(localized_params, blacklight_config)
content = []
localized_params[:f].each_pair do |facet,values|
content << render_filter_element(facet, values, localized_params)
content << render_filter_element(facet, values, path)
end

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

##
# Render a single facet's constraint
#
# @param [String] facet field
# @param [Array<String>] selected facet values
# @param [Hash] query parameters
# @param [Array<String>] values selected facet values
# @param [Blacklight::Path] path query parameters
# @return [String]
def render_filter_element(facet, values, localized_params)
def render_filter_element(facet, values, path)
facet_config = facet_configuration_for_field(facet)

safe_join(values.map do |val|
next if val.blank? # skip empty string
render_constraint_element( facet_field_label(facet_config.key), facet_display_value(facet, val),
:remove => search_action_path(remove_facet_params(facet, val, localized_params)),
:classes => ["filter", "filter-" + facet.parameterize]
remove: search_action_path(path.remove_facet_params(facet, val)),
classes: ["filter", "filter-" + facet.parameterize]
)
end, "\n")
end
Expand Down
143 changes: 6 additions & 137 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# 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
Expand Down Expand Up @@ -144,150 +149,14 @@ def link_to_previous_search(params)
link_to(render_search_to_s(params), search_action_path(params))
end

# @overload params_for_search(source_params, params_to_merge)
# Merge the source params with the params_to_merge hash
# @param [Hash] Hash
# @param [Hash] Hash to merge into above
# @overload params_for_search(params_to_merge)
# Merge the current search parameters with the
# parameters provided.
# @param [Hash] Hash to merge into the parameters
# @overload params_for_search
# Returns the current search parameters after being sanitized by #sanitize_search_params
# @yield [params] The merged parameters hash before being sanitized
def params_for_search(*args, &block)

source_params, params_to_merge = case args.length
when 0
[params, {}]
when 1
[params, args.first]
when 2
[args.first, args.last]
else
raise ArgumentError.new "wrong number of arguments (#{args.length} for 0..2)"
end

# params hash we'll return
my_params = source_params.dup.merge(params_to_merge.dup)

if block_given?
yield my_params
end

if my_params[:page] and (my_params[:per_page] != source_params[:per_page] or my_params[:sort] != source_params[:sort] )
my_params[:page] = 1
end

sanitize_search_params(my_params)
end

##
# Sanitize the search parameters by removing unnecessary parameters
# from the provided parameters
# @param [Hash] Hash of parameters
def sanitize_search_params source_params

my_params = source_params.reject { |k,v| v.nil? }

my_params.except(:action, :controller, :id, :commit, :utf8)
end

##
# Reset any search parameters that store search context
# and need to be reset when e.g. constraints change
def reset_search_params source_params
sanitize_search_params(source_params).except(:page, :counter).with_indifferent_access
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
# add_facet_params_and_redirect
def add_facet_params(field, item, source_params = params)

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(source_params)
p[:f] = (p[:f] || {}).dup # the command above is not deep in rails3, !@#$!@#$
p[:f][url_field] = (p[:f][url_field] || []).dup

if facet_config.single and not p[:f][url_field].empty?
p[:f][url_field] = []
end

p[:f][url_field].push(value)

if item and item.respond_to?(:fq) and item.fq
Array(item.fq).each do |f,v|
p = add_facet_params(f, v, p)
end
end

p
end

# 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.
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.except! *request_keys.values

new_params
end

# Get url parameters to a search within a grouped result set
#
# @param [Blacklight::SolrResponse::Group]
# @return [Hash]
def add_group_facet_params_and_redirect group
add_facet_params_and_redirect(group.field, group.key)
current_path.add_facet_params_and_redirect(group.field, group.key, params)
end

# copies the current params (or whatever is passed in as the 3rd arg)
# removes the field value from params[:f]
# removes the field if there are no more values in params[:f][field]
# removes additional params (page, id, etc..)
def remove_facet_params(field, item, source_params=params)
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(source_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
p[:f][url_field] = p[:f][url_field] - [value]
p[:f].delete(url_field) if p[:f][url_field].size == 0
p.delete(:f) if p[:f].empty?
p
end

# A URL to refworks export, with an embedded callback URL to this app.
# the callback URL is to bookmarks#export, which delivers a list of
# user's bookmarks in 'refworks marc txt' format -- we tell refworks
Expand Down
2 changes: 1 addition & 1 deletion app/views/bookmarks/_tools.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul class="<%= controller_name %>Tools nav nav-pills">
<%= render_show_doc_actions document_list, document: nil, document_list: @document_list, url_opts: sanitize_search_params(params) do |config, inner| %>
<%= render_show_doc_actions document_list, document: nil, document_list: @document_list, url_opts: Blacklight::Parameters.sanitize(params) do |config, inner| %>
<li>
<%= inner %>
</li>
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(params_for_search(:per_page => count))) %></li>
<li><%= link_to(label, url_for(current_path.params_for_search(per_page: count))) %></li>
<%- end -%>
</ul>
</div>
Expand Down
3 changes: 1 addition & 2 deletions app/views/catalog/_search_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<%= form_tag search_action_url, method: :get, class: 'search-query-form clearfix navbar-form', role: 'search' do %>
<%= render_hash_as_hidden_fields(params_for_search().except(:q, :search_field, :qt, :page, :utf8)) %>

<%= render_hash_as_hidden_fields(current_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(params_for_search(:sort => sort_key))) %></li>
<li><%= link_to(field_config.label, url_for(current_path.params_for_search(sort: sort_key))) %></li>
<%- end -%>
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/catalog/_zero_results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<%- 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(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(current_path.params_for_search(search_field: blacklight_config.default_search_field.key)) %>
</li>
<%- end %>

</ul>
</div>
</div>
2 changes: 2 additions & 0 deletions lib/blacklight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Blacklight
autoload :Exceptions, 'blacklight/exceptions'
autoload :Path, 'blacklight/path'
autoload :Parameters, 'blacklight/parameters'
autoload :Routes, 'blacklight/routes'

extend Deprecation
Expand Down
13 changes: 13 additions & 0 deletions lib/blacklight/parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Blacklight
module Parameters
##
# Sanitize the search parameters by removing unnecessary parameters
# from the provided parameters
# @param [Hash] source_params parameters
def self.sanitize params
params
.reject { |k,v| v.nil? }
.except(:action, :controller, :id, :commit, :utf8)
end
end
end

0 comments on commit 1947978

Please sign in to comment.