From 9e8e29f7eef256d4ac980bbc29e0ee6c5d9bef58 Mon Sep 17 00:00:00 2001 From: Bess Sadler Date: Thu, 17 Mar 2022 12:53:59 -0400 Subject: [PATCH 1/2] Pre-pend deploy subdirectory to metadata links --- app/helpers/application_helper.rb | 16 ++++++++++- spec/helpers/application_helper_spec.rb | 36 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/application_helper_spec.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 72f881fd..c60d4403 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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) @@ -44,6 +54,10 @@ 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? @@ -51,7 +65,7 @@ def render_field_row_search_link(title, value, field, show_always = false) html = <<-HTML #{title} - #{link_to(value, "/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields")} + #{link_to(value, search_link(value, field))} HTML html.html_safe diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 00000000..568c2fd3 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -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 From 6cd089a0432510eb7da458ef2275aba4179f57b2 Mon Sep 17 00:00:00 2001 From: Bess Sadler Date: Thu, 17 Mar 2022 13:44:41 -0400 Subject: [PATCH 2/2] Replace all metadata links with subdirectory pre-pended versions --- app/helpers/application_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c60d4403..e84ff1fd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -76,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| - "" + link_to(value, "/?f[#{field}][]=#{CGI.escape(value)}&q=&search_field=all_fields") + "" + "" + link_to(value, search_link(value, field)) + "" end html = <<-HTML @@ -180,7 +180,7 @@ def render_subject_search_links(title, values, field) return if values.count.zero? # Must use instead of 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)}
" + "#{link_to(value, search_link(value, field), class: 'badge badge-dark sidebar-value-badge', title: value)}
" end html = <<-HTML