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

add an access_panels component for linking to the same object in other user facing systems #4357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions app/components/access_panels/other_listings_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= render @layout.new do |component| %>
<% component.with_title { 'Also listed in' } %>
<% component.with_body do %>
<% if earthworks_url.present? %>
<p><%= link_to('Earthworks', earthworks_url) %></p>
<% end %>
<% purl_links.each do |purl_link| %>
<p><%= purl_link %></p>
<% end %>
<% end %>
<% end %>
50 changes: 50 additions & 0 deletions app/components/access_panels/other_listings_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module AccessPanels
class OtherListingsComponent < AccessPanels::Base
def render?
earthworks_url.present? || purls.present?
end

def earthworks_url
@earthworks_url ||= construct_earthworks_url
end

# @return [Array<String>] a tags representing PURL links
def purl_links
@purl_links ||=
if purls.size > 1 # text of the link is the actual URL if there is more than one unique URL, so there aren't multiple links with the same title
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea whether accounting for this possibility is overly complicated, i.e. i have no idea whether we'd ever see multiple PURLs in practice among the MARC and MODS metadata?

purls.map { |purl| link_to(purl, purl) }
elsif purls.size == 1
Array(link_to('Stanford Digital Repository', purls.first))
else
[]
end
end

private

# @return [Array<String>] URLs representing links to PURL, whether specified in MARC or MODS, and de-duped, e.g. ["https://purl.stanford.edu/hj948rn6493"]
def purls
@purls ||= (mods_purls + marc_managed_purls).uniq
end

# @return [Array<String>] URLs representing links to other locations, extracted from MODS metadata, e.g. ["https://purl.stanford.edu/hj948rn6493"]
def mods_purls
Array(document['url_fulltext']).find_all { |str| str.start_with?(Settings.PURL_EMBED_RESOURCE) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wasn't sure if document['url_fulltext'] was the right field to use. other possible options i saw were:

  • document.as_schema_dot_org['identifier'] (which seems to have an array of PURLs, but i ended up moving away form using as_schema_dot_org at all, so guessing we don't want to reintroduce its usage here)
  • document.mods&.location&.map { |location| location.values }&.flatten (in testing, this got me an array of a tags for the PURLs, but the text of the tag was the URL, which didn't match the mockup's language, and parsing the PURL out of the tag seemed unnecessarily indirect)

end

# @return [Array<String>] URLs representing links to other locations, extracted from MARC metadata, e.g. ["https://purl.stanford.edu/hj948rn6493"]
def marc_managed_purls
document.marc_links.managed_purls.filter_map(&:href) # filter_map because href can be nil, at least in fixture data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seemed safe to me based on looking at similar calls elsewhere in the app, but i can add more safeguards if marc_links or managed_purls might sometimes be nil.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mentioning this specifically because i would love for my first searchworks PR to not cause a bunch of unexpected 500 errors 😅

end

def construct_earthworks_url
return nil unless document['dor_content_type_ssi'] == 'geo'

# * Hardcoding this simple URL format because it should work for all things it'd apply to (anything released to both SW and EW should be a Stanford object)
# * Using Kernel.format because there's a clashing ViewComponent::Base#format, so the namespacing is needed
Kernel.format('https://%<hostname>s/catalog/stanford-%<bare_druid>s', hostname: Settings.earthworks.hostname, bare_druid: document.druid)
end
end
end
1 change: 1 addition & 0 deletions app/views/catalog/record/_metadata_panels.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<% potential_context = capture do %>
<%= render AccessPanels::ExhibitComponent.new(document: document) %>
<%= render AccessPanels::OtherListingsComponent.new(document: document) %>
<% end %>

<% if context.present? || potential_context.present? %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/record/_mods_bibliographic.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
<%= render ModsDisplay::FieldComponent.new(field: identifier, label_html_attributes: { class: 'col-md-3' }, value_html_attributes: { class: 'col-md-9' }) %>
<% end %>

<% document.mods.location.each do |location| %>
<% document.mods.location.reject { |location| location.values.detect { |loc| /href=.#{Settings.PURL_EMBED_RESOURCE}/.match?(loc) } }.each do |location| %>
<%= render ModsDisplay::FieldComponent.new(field: location, label_html_attributes: { class: 'col-md-3' }, value_html_attributes: { class: 'col-md-9' }) %>
<% end %>
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,6 @@ folio_unrequestable_statuses:
- Unavailable
- Unknown
- Withdrawn

earthworks:
hostname: 'earthworks.stanford.edu'
94 changes: 94 additions & 0 deletions spec/components/access_panels/other_listings_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe AccessPanels::OtherListingsComponent, type: :component do
include MarcMetadataFixtures

let(:component) { described_class.new(document:) }

let(:document_hash) { { id: '123', dor_content_type_ssi: 'notgeo' } }
let(:document) { SolrDocument.new(document_hash) }

subject(:page) { render_inline(component) }

it 'does not render if the item neither is released to Earthworks nor has a PURL' do
page
expect(rendered_content).to eq ""
end

context 'there is a PURL specified in the MARC' do
let(:document_hash) do
{
id: 'hj948rn6493',
marc_links_struct: [
{ href: 'https://purl.stanford.edu/hj948rn6493', managed_purl: true }
]
}
end

it 'displays a link to the PURL' do
expect(page).to have_link('Stanford Digital Repository', href: 'https://purl.stanford.edu/hj948rn6493')
end
end

context 'there is a PURL specified in the MODS' do
let(:document_hash) { { id: 'bd285ct9109', url_fulltext: 'https://purl.stanford.edu/hj948rn6493' } }

it 'displays a link to the PURL' do
expect(page).to have_link('Stanford Digital Repository', href: 'https://purl.stanford.edu/hj948rn6493')
end
end

context 'the same PURL is specified in both MARC and MODS' do
let(:document_hash) do
{
id: 'hj948rn6493',
url_fulltext: 'https://purl.stanford.edu/hj948rn6493',
marc_links_struct: [
{ href: 'https://purl.stanford.edu/hj948rn6493', managed_purl: true }
]
}
end

it 'displays a single link to the PURL' do
expect(page).to have_link('Stanford Digital Repository', href: 'https://purl.stanford.edu/hj948rn6493')
expect(page).to have_css('a', count: 1)
end
end

context 'MODS and MARC specify different PURLs' do
let(:document_hash) do
{
id: '123',
url_fulltext: 'https://purl.stanford.edu/bc123df4567',
marc_links_struct: [
{ href: 'https://purl.stanford.edu/gh890jk9876', managed_purl: true }
]
}
end

it 'displays a single link to the PURL' do
expect(page).to have_link('https://purl.stanford.edu/bc123df4567', href: 'https://purl.stanford.edu/bc123df4567')
expect(page).to have_link('https://purl.stanford.edu/gh890jk9876', href: 'https://purl.stanford.edu/gh890jk9876')
expect(page).to have_css('a', count: 2)
end
end

context 'the object has been released to Earthworks' do
let(:document_hash) { { id: 'bd285ct9109', druid: 'bd285ct9109', dor_content_type_ssi: 'geo' } }

it 'displays a link to Earthworks' do
expect(page).to have_link('Earthworks', href: "https://#{Settings.earthworks.hostname}/catalog/stanford-bd285ct9109")
end
end

context 'the object has been released to Earthworks and it has a PURL' do
let(:document_hash) { { druid: 'bd285ct9109', url_fulltext: 'https://purl.stanford.edu/bd285ct9109', dor_content_type_ssi: 'geo' } }

it 'displays a link to the PURL and a link to Earthworks' do
expect(page).to have_link('Stanford Digital Repository', href: 'https://purl.stanford.edu/bd285ct9109')
expect(page).to have_link('Earthworks', href: "https://#{Settings.earthworks.hostname}/catalog/stanford-bd285ct9109")
end
end
end