Skip to content

Commit

Permalink
Merge pull request #234 from pulibrary/i230-bugfix
Browse files Browse the repository at this point in the history
Pre-pend deploy subdirectory to metadata links
  • Loading branch information
kelynch authored Mar 17, 2022
2 parents b862ede + 6cd089a commit 96b8f45
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
# rubocop:disable Rails/OutputSafety
# rubocop:disable Metrics/ModuleLength
module ApplicationHelper
# This application is deployed in a subdirectory ("/discovery")
# in staging and production. We control this by setting
# Rails.application.config.assets.prefix. This method reads
# that Rails setting and extracts the prefix needed in order for
# application links to work as expected.
# @return [String]
def subdirectory_for_links
(Rails.application.config.assets.prefix.split("/") - ["assets"]).join("/")
end

# Outputs the HTML to render a single value as an HTML table row
# to be displayed on the metadata section of the show page.
def render_field_row(title, value, show_always = false)
Expand Down Expand Up @@ -44,14 +54,18 @@ def render_field_row_link(title, url, show_always = false)
html.html_safe
end

def search_link(value, field)
"#{subdirectory_for_links}/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields"
end

# Outputs the HTML to render a single value as an HTML table row with a search link
def render_field_row_search_link(title, value, field, show_always = false)
return if value.blank?
css_class = show_always ? "" : "toggable-row hidden-row"
html = <<-HTML
<tr class="#{css_class}">
<th scope="row"><span>#{title}</span></th>
<td><span>#{link_to(value, "/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields")}</span></td>
<td><span>#{link_to(value, search_link(value, field))}</span></td>
</tr>
HTML
html.html_safe
Expand All @@ -62,7 +76,7 @@ def render_field_row_search_links(title, values, field, show_always = false)
return if values.blank?
css_class = show_always ? "" : "toggable-row hidden-row"
links = values.map do |value|
"<span>" + link_to(value, "/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields") + "</span>"
"<span>" + link_to(value, search_link(value, field)) + "</span>"
end
html = <<-HTML
<tr class="#{css_class}">
Expand Down Expand Up @@ -166,7 +180,7 @@ def render_subject_search_links(title, values, field)
return if values.count.zero?
# Must use <divs> instead of <spans> for them to wrap inside the sidebar
links_html = values.map do |value|
"#{link_to(value, "/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields", class: 'badge badge-dark sidebar-value-badge', title: value)}<br/>"
"#{link_to(value, search_link(value, field), class: 'badge badge-dark sidebar-value-badge', title: value)}<br/>"
end

html = <<-HTML
Expand Down
36 changes: 36 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

RSpec.describe ApplicationHelper, type: :helper do
describe "#render_field_row_search_link" do
let(:title) { "Domain" }
let(:value) { "Humanities" }
let(:field) { "domain_ssi" }
let(:search_link) { helper.search_link(value, field) }
let(:rendered_search_link) { helper.render_field_row_search_link(title, value, field) }

it "creates a search link" do
expect(rendered_search_link).to match(title)
expect(rendered_search_link).to match(value)
expect(rendered_search_link).to match(field)
end

# config.assets.prefix = "/discovery/assets/"
context "when there is an assets prefix" do
around do |example|
Rails.application.config.assets.prefix = "/discovery/assets/"
example.run
Rails.application.config.assets.prefix = "/assets/"
end

it "pre-pends the link" do
expect(search_link).to eq "/discovery/?f[domain_ssi][]=Humanities&q=&search_field=all_fields"
end
end

context "when there is not an assets prefix" do
it "does not pre-pend the link" do
expect(search_link).to eq "/?f[domain_ssi][]=Humanities&q=&search_field=all_fields"
end
end
end
end

0 comments on commit 96b8f45

Please sign in to comment.