Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata links when app is deployed in a subdirectory #233

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ A discovery portal for Princeton research data. Initially it will provide a bett
* yarn: 1.22.10
* [Lando](https://github.com/lando/lando/releases): 3.0.0

## Updating the banner

Update the file `config/banner.yml`. Note that each environment can have its own banner text.

## Local development

### Setup
Expand Down
14 changes: 14 additions & 0 deletions app/assets/stylesheets/components/banner.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#banner {
text-align: center;
background-color: #e1f0fb;
padding: 20px 0px 10px 0px;
border-bottom: #0060a7 solid 2px;
color: #0060a7;
h1 {
font-size: 24px;
font-weight: bold;
}
p {
font-size: 18px;
}
}
16 changes: 15 additions & 1 deletion 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 Down
14 changes: 14 additions & 0 deletions app/helpers/banner_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'yaml'

module BannerHelper
# rubocop:disable Rails/ContentTag
def banner_content
@yaml_data = YAML.load_file('config/banner.yml')
return false if @yaml_data[Rails.env].nil?
@banner_title = content_tag(:h1, @yaml_data[Rails.env]['title'])
@banner_body = content_tag(:p, @yaml_data[Rails.env]['body'])
end
# rubocop:enable Rails/ContentTag
end
3 changes: 3 additions & 0 deletions app/views/layouts/blacklight/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<%= link_to t('blacklight.skip_links.main_content'), '#main-container', class: 'element-invisible element-focusable rounded-bottom py-2 px-3', data: { turbolinks: 'false' } %>
<%= content_for(:skip_links) %>
</nav>

<%= render partial: 'shared/banner' %>

<%= render partial: 'shared/header_navbar' %>

<main id="main-container" class="<%= container_classes %>" role="main" aria-label="<%= t('blacklight.main.aria.main_container') %>">
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_banner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if banner_content %>
<div id="banner">
<%= @banner_title %>
<%= @banner_body %>
</div>
<% end %>
12 changes: 12 additions & 0 deletions config/banner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
development:
title: Welcome to the development site
body: This is a message for development
test:
title: Welcome to the test site
body: This is a message for test
staging:
title: Welcome to the staging site
body: This is a message for staging
production:
title: Welcome to the production site
body: This is a message for production
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
34 changes: 34 additions & 0 deletions spec/system/banner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

describe 'Website banner', type: :system, js: true do
let(:community_fetch_with_expanded_metadata) { file_fixture("single_item.xml").read }
let(:indexer) do
Indexer.new(community_fetch_with_expanded_metadata)
end

before do
Blacklight.default_index.connection.delete_by_query("*:*")
Blacklight.default_index.connection.commit
indexer.index
end

it "has the banner on the homepage" do
visit '/'
expect(page).to have_css '#banner'
end

it "has the banner on a static page" do
visit '/about'
expect(page).to have_css '#banner'
end

it "has the banner on the search results page" do
visit '/?search_field=all_fields&q=test'
expect(page).to have_css '#banner'
end

it "has the banner on a single record page" do
visit '/catalog/78348'
expect(page).to have_css '#banner'
end
end
2 changes: 1 addition & 1 deletion spec/system/single_item_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
indexer.index
end

it "has expected header fiedlds" do
it "has expected header fields" do
visit '/catalog/78348'
expect(page).to have_css '.document-title-heading'
expect(page).to have_css '.authors-heading'
Expand Down