diff --git a/app/components/attachment/gallery_item_component/gallery_item_component.html.haml b/app/components/attachment/gallery_item_component/gallery_item_component.html.haml index 09ebb3744..d59ade805 100644 --- a/app/components/attachment/gallery_item_component/gallery_item_component.html.haml +++ b/app/components/attachment/gallery_item_component/gallery_item_component.html.haml @@ -2,7 +2,7 @@ - if displayable_pdf?(blob) = link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: title do .thumbnail - = image_tag(preview_url_for(attachment), loading: :lazy) + = image_tag(representation_url_for(attachment), loading: :lazy) .fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button } Visualiser .champ-libelle @@ -12,13 +12,12 @@ - elsif displayable_image?(blob) = link_to image_url(blob_url(attachment)), title: title, data: { src: blob.url }, class: 'gallery-link' do .thumbnail - = image_tag(variant_url_for(attachment), loading: :lazy) + = image_tag(representation_url_for(attachment), loading: :lazy) .fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button } Visualiser .champ-libelle = libelle.truncate(25) = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - - else .thumbnail = image_tag('apercu-indisponible.png') diff --git a/app/helpers/gallery_helper.rb b/app/helpers/gallery_helper.rb index b9f97ca8d..0f97ea9b9 100644 --- a/app/helpers/gallery_helper.rb +++ b/app/helpers/gallery_helper.rb @@ -9,6 +9,12 @@ module GalleryHelper blob.variable? && blob.content_type.in?(AUTHORIZED_IMAGE_TYPES) end + def representation_url_for(attachment) + return variant_url_for(attachment) if displayable_image?(attachment.blob) + + preview_url_for(attachment) if displayable_pdf?(attachment.blob) + end + def preview_url_for(attachment) preview = attachment.preview(resize_to_limit: [400, 400]) preview.image.attached? ? preview.processed.url : 'pdf-placeholder.png' diff --git a/spec/helpers/gallery_helper_spec.rb b/spec/helpers/gallery_helper_spec.rb index 6f4d3567c..a6783f13f 100644 --- a/spec/helpers/gallery_helper_spec.rb +++ b/spec/helpers/gallery_helper_spec.rb @@ -74,4 +74,20 @@ RSpec.describe GalleryHelper, type: :helper do it { is_expected.to eq("pdf-placeholder.png") } end end + + describe ".representation_url_for" do + subject { representation_url_for(attachment) } + + context "when attachment is an image with no variant" do + let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } + + it { is_expected.to eq("apercu-indisponible.png") } + end + + context "when attachment is a pdf with no preview" do + let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } + + it { is_expected.to eq("pdf-placeholder.png") } + end + end end