Skip to content

Commit

Permalink
create linked facets in index results
Browse files Browse the repository at this point in the history
* Documenting example
* Documenting parameters
* Removing custom exception to defer to Hash#fetch raising a meaningful
  exception.
  • Loading branch information
jenlindner committed Dec 19, 2016
1 parent 9cfe062 commit 4f2196d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/helpers/sufia/sufia_helper_behavior.rb
Expand Up @@ -123,6 +123,33 @@ def index_field_link(options)
safe_join(links, ", ")
end

# A Blacklight helper_method
#
# @example
# link_to_each_facet_field({ value: "Imaging > Object Photography", config: { helper_facet: :document_types_sim }})
# ```html
# <a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">Imaging</a> &gt; <a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>
# ```
#
# @param options [Hash] from blacklight invocation of helper_method
# @option options [Array<#strip>] :value
# @option options [Hash>] :config Example: { separator: '>', helper_facet: :document_types_sim, output_separator: '::' }
# @return the html_safe facet links separated by the given separator (default: ' > ') to indicate facet hierarchy
#
# @raise KeyError when the options are note properly configured
def link_to_each_facet_field(options)
config = options.fetch(:config)
separator = config.fetch(:separator, ' > ')
output_separator = config.fetch(:output_separator, separator)
facet_search = config.fetch(:helper_facet)
facet_fields = Array.wrap(options.fetch(:value)).first.split(separator).map(&:strip)

facet_links = facet_fields.map do |type|
link_to(type, main_app.search_catalog_path(f: { facet_search => [type] }))
end
safe_join(facet_links, output_separator)
end

# Uses Rails auto_link to add links to fields
#
# @param field [String,Hash] string to format and escape, or a hash as per helper_method
Expand Down
23 changes: 23 additions & 0 deletions spec/helpers/sufia_helper_spec.rb
Expand Up @@ -68,6 +68,29 @@ def new_state
end
end

describe "#link_to_each_facet_field" do
subject { helper.link_to_each_facet_field(options) }
context "with helper_facet and default separator" do
let(:options) { { config: { helper_facet: Solrizer.solr_name("document_types", :facetable).to_sym }, value: ["Imaging > Object Photography"] } }
it { is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">Imaging</a> &gt; <a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>") }
end

context "with helper_facet and optional separator" do
let(:options) { { config: { helper_facet: Solrizer.solr_name("document_types", :facetable).to_sym, separator: " : " }, value: ["Imaging : Object Photography"] } }
it { is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">Imaging</a> : <a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>") }
end

context "with :output_separator" do
let(:options) { { config: { helper_facet: Solrizer.solr_name("document_types", :facetable).to_sym, output_separator: ' ~ ', separator: ":" }, value: ["Imaging : Object Photography"] } }
it { is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">Imaging</a> ~ <a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>") }
end

context "with :no_spaces_around_separator" do
let(:options) { { config: { helper_facet: Solrizer.solr_name("document_types", :facetable).to_sym, output_separator: '~', separator: ":" }, value: ["Imaging : Object Photography"] } }
it { is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">Imaging</a>~<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>") }
end
end

describe "has_collection_search_parameters?" do
subject { helper }
context "when cq is set" do
Expand Down

0 comments on commit 4f2196d

Please sign in to comment.