Skip to content

Commit

Permalink
Handle the case where resource label is blank
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 18, 2023
1 parent 7ed0d0b commit ac837b3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<li>
<a href='<%= url %>' title='<%= file.title %>' target='_blank' rel='noopener noreferrer'>
Download <%= file.label %>
Download <%= file.label_or_filename %>
</a>
<%= render 'embed/restrictions_text_for_file', file: file %>
<%= file_size %>
Expand Down
2 changes: 1 addition & 1 deletion app/components/embed/media_wrapper_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def call # rubocop:disable Metrics/MethodLength
action: 'thumbnail-clicked@window->media-wrapper#toggleVisibility',
stanford_only: @file.stanford_only?,
location_restricted: @file.location_restricted?,
file_label: @file.label,
file_label: @file.label_or_filename,
slider_object: @file_index,
thumbnail_url: @thumbnail.presence,
default_icon: @type == 'audio' ? 'sul-i-file-music-1' : 'sul-i-file-video-3',
Expand Down
4 changes: 4 additions & 0 deletions app/models/embed/purl/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def stacks_url
"#{Settings.stacks_url}/file/druid:#{@druid}"
end

def label_or_filename
label.presence || filename
end

def hierarchical_title
title.split('/').last
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/embed/body/_resource_file.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<% end %>

<div class='sul-embed-description' data-description='true'>
<%= file.label %>
<%= file.label_or_filename %>
</div>
<div class='sul-embed-download'>
<i class='sul-i-download-3'></i>
Expand Down
14 changes: 7 additions & 7 deletions spec/components/embed/media_wrapper_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

describe 'data-default-icon attribute' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label_or_filename: 'ignored', duration: nil) }

context 'with audio' do
it 'renders the page' do
Expand All @@ -39,15 +39,15 @@

describe 'data-stanford-only attribute' do
context 'with Stanford only files' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label_or_filename: 'ignored', duration: nil) }

it 'renders the page' do
expect(page).to have_css('[data-stanford-only="true"]')
end
end

context 'with public files' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label_or_filename: 'ignored', duration: nil) }

it 'renders the page' do
expect(page).to have_css('[data-stanford-only="false"]')
Expand All @@ -57,15 +57,15 @@

describe 'duration' do
context 'when the resource file has duration' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label: 'ignored', duration: '1:02') }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: false, label_or_filename: 'ignored', duration: '1:02') }

it 'renders the page' do
expect(page).to have_css('[data-duration="1:02"]')
end
end

context 'when the resource file is missing duration' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label_or_filename: 'ignored', duration: nil) }

it 'renders the page' do
expect(page).not_to have_css('[data-duration]')
Expand All @@ -75,15 +75,15 @@

describe 'data-location-restricted attribute' do
context 'when location restricted' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: true, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: false, location_restricted?: true, label_or_filename: 'ignored', duration: nil) }

it 'renders the page' do
expect(page).to have_css('[data-location-restricted="true"]')
end
end

context 'when not location restricted' do
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label: 'ignored', duration: nil) }
let(:file) { instance_double(Embed::Purl::ResourceFile, stanford_only?: true, location_restricted?: false, label_or_filename: 'ignored', duration: nil) }

it 'renders the page' do
expect(page).to have_css('[data-location-restricted="false"]')
Expand Down
16 changes: 16 additions & 0 deletions spec/models/embed/purl/resource_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
end
end

describe '#label_or_filename' do
subject { resource_file.label_or_filename }

context 'with a label' do
let(:resource_file) { described_class.new(label: 'The Resource Description') }

it { is_expected.to eq 'The Resource Description' }
end

context 'without a blank label' do
let(:resource_file) { described_class.new(label: '', filename: 'llama.mp3') }

it { is_expected.to eq 'llama.mp3' }
end
end

describe 'image?' do
it 'returns true if the mimetype of the file is an image' do
stub_purl_xml_response_with_fixture(image_purl_xml)
Expand Down

0 comments on commit ac837b3

Please sign in to comment.