-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor some view logic into its own component (#3551)
- Loading branch information
1 parent
c48635c
commit 586b7ed
Showing
5 changed files
with
95 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters