diff --git a/app/helpers/gallery_helper.rb b/app/helpers/gallery_helper.rb index 1f9ddeeab..89e27842e 100644 --- a/app/helpers/gallery_helper.rb +++ b/app/helpers/gallery_helper.rb @@ -8,19 +8,22 @@ module GalleryHelper end def preview_url_for(attachment) - attachment.preview(resize_to_limit: [400, 400]).processed.url + preview = attachment.preview(resize_to_limit: [400, 400]) + preview.image.attached? ? preview.processed.url : 'pdf-placeholder.png' rescue StandardError 'pdf-placeholder.png' end def variant_url_for(attachment) - attachment.variant(resize_to_limit: [400, 400]).processed.url + variant = attachment.variant(resize_to_limit: [400, 400]) + variant.key.present? ? variant.processed.url : 'apercu-indisponible.png' rescue StandardError 'apercu-indisponible.png' end def blob_url(attachment) - attachment.blob.content_type.in?(RARE_IMAGE_TYPES) ? attachment.variant(resize_to_limit: [2000, 2000]).processed.url : attachment.blob.url + variant = attachment.variant(resize_to_limit: [2000, 2000]) + attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && variant.key.present? ? variant.processed.url : attachment.blob.url rescue StandardError attachment.blob.url end diff --git a/spec/helpers/gallery_helper_spec.rb b/spec/helpers/gallery_helper_spec.rb index 41469798c..22ddabd32 100644 --- a/spec/helpers/gallery_helper_spec.rb +++ b/spec/helpers/gallery_helper_spec.rb @@ -22,13 +22,21 @@ RSpec.describe GalleryHelper, type: :helper do describe ".variant_url_for" do subject { variant_url_for(attachment) } - context "when attachment can be represented with a variant" do + context "when image attachment has a variant" do let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } - it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } + before { attachment.variant(resize_to_limit: [400, 400]).processed } + it { is_expected.not_to eq("apercu-indisponible.png") } end + context "when image attachment has no variant" do + let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } + + it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } } + it { is_expected.to eq("apercu-indisponible.png") } + end + context "when attachment cannot be represented with a variant" do let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') } @@ -40,13 +48,21 @@ RSpec.describe GalleryHelper, type: :helper do describe ".preview_url_for" do subject { preview_url_for(attachment) } - context "when attachment can be represented with a preview" do + context "when pdf attachment has a preview" do let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } - it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } + before { attachment.preview(resize_to_limit: [400, 400]).processed } + it { is_expected.not_to eq("pdf-placeholder.png") } end + context "when pdf attachment has no preview" do + let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } + + it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } } + it { is_expected.to eq("pdf-placeholder.png") } + end + context "when attachment cannot be represented with a preview" do let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') }