Skip to content

Commit

Permalink
feat(gallery): do not create representations in web machines
Browse files Browse the repository at this point in the history
  • Loading branch information
E-L-T committed Jun 12, 2024
1 parent 069cb04 commit c69244a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 6 additions & 3 deletions app/helpers/gallery_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ def displayable_image?(blob)
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
Expand Down
24 changes: 20 additions & 4 deletions spec/helpers/gallery_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@
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') }

Expand All @@ -40,13 +48,21 @@
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') }

Expand Down

0 comments on commit c69244a

Please sign in to comment.