Skip to content

Commit

Permalink
Refactor some view logic into its own component (#3551)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbergja committed May 8, 2023
1 parent c48635c commit 586b7ed
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 133 deletions.
5 changes: 5 additions & 0 deletions app/components/request_button_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= link_to label, url,
class: 'request btn btn-xs btn-primary',
title: tooltip,
data: { toggle: 'tooltip' }
%>
32 changes: 32 additions & 0 deletions app/components/request_button_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# ViewComponent that displays a request button on the show page
class RequestButtonComponent < ViewComponent::Base
def initialize(location:, doc_id:, holding_id: nil, force_aeon: false)
@location = location
@doc_id = doc_id
@holding_id = holding_id
@force_aeon = force_aeon
end

def label
return 'Reading Room Request' if aeon?
'Request'
end

def tooltip
return 'Request to view in Reading Room' if aeon?
'View Options to Request copies from this Location'
end

def url
query = { mfhd: @holding_id, aeon: aeon?.to_s }.compact.to_query
URI::HTTP.build(path: "/requests/#{@doc_id}", query:).request_uri
end

private

def aeon?
@aeon ||= @force_aeon || @location&.dig(:aeon_location)
end
end
55 changes: 6 additions & 49 deletions app/services/physical_holdings_markup_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,28 +229,6 @@ def self.location_services_block(adapter, holding_id, location_rules, link, hold
})
end

# Generate a request label based upon the holding location
# @param location_rules [Hash] the location for the holding
# @return [String] the label
def request_label(location_rules)
if aeon_location?(location_rules)
'Reading Room Request'
else
'Request'
end
end

# Generate a request tooltip based upon the holding location
# @param location_rules [Hash] the location for the holding
# @return [String] the label
def self.request_tooltip(location_rules)
if aeon_location?(location_rules)
'Request to view in Reading Room'
else
'View Options to Request copies from this Location'
end
end

def self.scsb_supervised_items?(holding)
if holding.key? 'items'
restricted_items = holding['items'].select do |item|
Expand Down Expand Up @@ -299,42 +277,21 @@ def request_placeholder(adapter, holding_id, location_rules, holding)
# check if it is a boundwith and read the id for each holding
# so that it will be "/request/#{doc_id}", where doc_id can be either the record page mms_id or the host id(s) if they exist.
doc_id = doc_id(holding)
aeon = aeon_location?(location_rules)
view_base = ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)
link = if !location_rules.nil? && /^scsb.+/ =~ location_rules['code']
if scsb_supervised_items?(holding)
# All SCSB supervised items go through Aeon
aeon = 'true'
link_to('Reading Room Request',
request_url(doc_id:, aeon:),
title: 'Request to view in Reading Room',
class: 'request btn btn-xs btn-primary',
data: { toggle: 'tooltip' })
RequestButtonComponent.new(doc_id:, location: location_rules, force_aeon: true).render_in(view_base)
else
link_to(request_label(location_rules),
request_url(doc_id:, aeon:),
title: self.class.request_tooltip(location_rules),
class: 'request btn btn-xs btn-primary',
data: { toggle: 'tooltip' })
RequestButtonComponent.new(doc_id:, location: location_rules).render_in(view_base)
end
elsif !adapter.alma_holding?(holding_id)
link_to('Reading Room Request',
request_url(doc_id:, aeon:, holding_id:),
title: 'Request to view in Reading Room',
class: 'request btn btn-xs btn-primary',
data: { toggle: 'tooltip' })
RequestButtonComponent.new(doc_id:, location: location_rules, holding_id:, force_aeon: true).render_in(view_base)
elsif self.class.temporary_holding_id?(holding_id)
holding_identifier = self.class.temporary_location_holding_id_first(holding)
link_to(request_label(location_rules),
request_url(doc_id:, aeon:, holding_id: holding_identifier),
title: self.class.request_tooltip(location_rules),
class: 'request btn btn-xs btn-primary',
data: { toggle: 'tooltip' })
RequestButtonComponent.new(doc_id:, holding_id: holding_identifier, location: location_rules).render_in(view_base)
else
link_to(request_label(location_rules),
request_url(doc_id:, aeon:, holding_id:),
title: self.class.request_tooltip(location_rules),
class: 'request btn btn-xs btn-primary',
data: { toggle: 'tooltip' })
RequestButtonComponent.new(doc_id:, holding_id:, location: location_rules).render_in(view_base)
end
markup = self.class.location_services_block(adapter, holding_id, location_rules, link, holding)
markup
Expand Down
42 changes: 42 additions & 0 deletions spec/components/request_button_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe RequestButtonComponent, type: :component do
let(:location) do
{ aeon_location: false }
end
subject { render_inline(described_class.new(location:, doc_id: '123', holding_id: '456')) }
it "renders a link with the appropriate classes" do
expect(subject.css('a').attribute('class').to_s).to eq('request btn btn-xs btn-primary')
end
it 'renders the typical title tooltip' do
expect(subject.css('a').attribute('title').text).to eq('View Options to Request copies from this Location')
end
it 'renders the typical request text' do
expect(subject.css('a').text).to eq('Request')
end
it 'includes aeon=false in the link url' do
expect(subject.css('a').attribute('href').text).to eq('/requests/123?aeon=false&mfhd=456')
end
context 'when at an aeon location' do
let(:location) do
{ aeon_location: true }
end
it 'renders the aeon request text' do
expect(subject.css('a').text).to eq('Reading Room Request')
end
it 'renders the aeon title tooltip' do
expect(subject.css('a').attribute('title').text).to eq('Request to view in Reading Room')
end
it 'includes aeon=true in the link url' do
expect(subject.css('a').attribute('href').text).to eq('/requests/123?aeon=true&mfhd=456')
end
end
context 'when no holding_id' do
subject { render_inline(described_class.new(location:, doc_id: '123')) }
it 'does not include mfhd param in the link url' do
expect(subject.css('a').attribute('href').text).to eq('/requests/123?aeon=false')
end
end
end
94 changes: 10 additions & 84 deletions spec/services/physical_holdings_markup_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,85 +59,6 @@
allow(adapter).to receive(:sc_location_with_suppressed_button?).with(holding).and_return(false)
end

describe '.request_label' do
let(:request_label) { builder.request_label(location_rules) }

context 'for holdings within aeon locations' do
let(:location_rules) do
{
'label': 'Sylvia Beach Collection',
'code': 'beac',
'aeon_location': true,
'recap_electronic_delivery_location': false,
'open': false,
'requestable': false,
'always_requestable': true,
'circulates': false,
'url': 'https://bibdata.princeton.edu/locations/holding_locations/beac.json',
'library': {
'label': 'Rare Books and Special Collections',
'code': 'rare',
'order': 2
},
'holding_library': nil,
'hours_location': {
'label': 'Firestone Library - Rare Books and Special Collections',
'code': 'rbsc'
}
}.with_indifferent_access
end

it 'generates a reading room request label' do
expect(request_label).to eq 'Reading Room Request'
expect(builder.request_url(doc_id: '123456', aeon: true)).to eq('/requests/123456?aeon=true')
end
end

context 'for circulating locations' do
it 'generates a generic request label' do
expect(request_label).to eq 'Request'
end
end
end

describe '.request_tooltip' do
let(:request_tooltip) { described_class.request_tooltip(location_rules) }

context 'for holdings within aeon locations' do
let(:location_rules) do
{
'label': 'Sylvia Beach Collection',
'code': 'beac',
'aeon_location': true,
'recap_electronic_delivery_location': false,
'open': false,
'requestable': false,
'always_requestable': true,
'circulates': false,
'url': 'https://bibdata.princeton.edu/locations/holding_locations/beac.json',
'library': {
'label': 'Rare Books and Special Collections',
'code': 'rare',
'order': 2
},
'holding_library': nil,
'hours_location': {
'label': 'Firestone Library - Rare Books and Special Collections',
'code': 'rbsc'
}
}
end

it 'generates a tooltip for requesting a view within the reading room' do
expect(request_tooltip).to eq 'Request to view in Reading Room'
end
end

it 'generates a tooltip for viewing options for requests' do
expect(request_tooltip).to eq 'View Options to Request copies from this Location'
end
end

describe '.holding_location_span' do
let(:holding_location_span_markup) { builder.holding_location_span('test-location', 'test-holding-id') }

Expand Down Expand Up @@ -240,7 +161,8 @@
expect(request_placeholder_markup).to include 'data-requestable="true"'
expect(request_placeholder_markup).to include 'data-aeon="false"'
expect(request_placeholder_markup).to include 'data-holding-id="3668455"'
expect(request_placeholder_markup).to include '<a title="View Options to Request copies from this Location"'
expect(request_placeholder_markup).to include '<a '
expect(request_placeholder_markup).to include 'title="View Options to Request copies from this Location"'
expect(request_placeholder_markup).to include 'href="/requests/123456?aeon=false&amp;mfhd=3668455"'
end

Expand Down Expand Up @@ -291,7 +213,8 @@
expect(request_placeholder_markup).to include 'data-requestable="true"'
expect(request_placeholder_markup).to include 'data-aeon="true"'
expect(request_placeholder_markup).to include 'data-holding-id="numismatics"'
expect(request_placeholder_markup).to include '<a title="Request to view in Reading Room"'
expect(request_placeholder_markup).to include '<a '
expect(request_placeholder_markup).to include 'title="Request to view in Reading Room"'
expect(request_placeholder_markup).to include 'href="/requests/coin-3750?aeon=true&amp;mfhd=numismatics"'
end
end
Expand Down Expand Up @@ -354,7 +277,8 @@
expect(request_placeholder_markup).to include 'data-open="false"'
expect(request_placeholder_markup).to include 'data-requestable="true"'
expect(request_placeholder_markup).to include 'data-holding-id="6670178"'
expect(request_placeholder_markup).to include '<a title="Request to view in Reading Room"'
expect(request_placeholder_markup).to include '<a '
expect(request_placeholder_markup).to include 'title="Request to view in Reading Room"'
# The general scsbnypl location is *not* an aeon location, but if the holding use_statement is "Supervised Use",
# it goes through aeon.
expect(request_placeholder_markup).to include 'data-aeon="false"'
Expand Down Expand Up @@ -419,7 +343,8 @@
expect(request_placeholder_markup).to include 'data-requestable="true"'
expect(request_placeholder_markup).to include 'data-aeon="false"'
expect(request_placeholder_markup).to include 'data-holding-id="11198370"'
expect(request_placeholder_markup).to include '<a title="View Options to Request copies from this Location"'
expect(request_placeholder_markup).to include '<a '
expect(request_placeholder_markup).to include 'title="View Options to Request copies from this Location"'
expect(request_placeholder_markup).to include 'href="/requests/SCSB-10422725?aeon=false"'
end
end
Expand Down Expand Up @@ -473,7 +398,8 @@
expect(request_placeholder_markup).to include 'data-requestable="true"'
expect(request_placeholder_markup).to include 'data-aeon="true"'
expect(request_placeholder_markup).to include 'data-holding-id="22692760320006421"'
expect(request_placeholder_markup).to include '<a title="Request to view in Reading Room"'
expect(request_placeholder_markup).to include '<a '
expect(request_placeholder_markup).to include 'title="Request to view in Reading Room"'
expect(request_placeholder_markup).to include 'href="/requests/99125038613506421?aeon=true&amp;mfhd=22692760320006421"'
end
end
Expand Down

0 comments on commit 586b7ed

Please sign in to comment.