add PDF preview in view for gallery
This commit is contained in:
parent
93c3761107
commit
affb1820d8
9 changed files with 13 additions and 18 deletions
Binary file not shown.
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 5.1 KiB |
Binary file not shown.
Before Width: | Height: | Size: 3.5 KiB |
|
@ -16,7 +16,7 @@ class ImageProcessorJob < ApplicationJob
|
||||||
return if ActiveStorage::Attachment.find_by(blob_id: blob.id)&.record_type == "ActiveStorage::VariantRecord"
|
return if ActiveStorage::Attachment.find_by(blob_id: blob.id)&.record_type == "ActiveStorage::VariantRecord"
|
||||||
|
|
||||||
auto_rotate(blob) if ["image/jpeg", "image/jpg"].include?(blob.content_type)
|
auto_rotate(blob) if ["image/jpeg", "image/jpg"].include?(blob.content_type)
|
||||||
create_variants(blob) if blob.variant_required?
|
create_representations(blob) if blob.representation_required?
|
||||||
add_watermark(blob) if blob.watermark_pending?
|
add_watermark(blob) if blob.watermark_pending?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,10 +34,9 @@ class ImageProcessorJob < ApplicationJob
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_variants(blob)
|
def create_representations(blob)
|
||||||
blob.attachments.each do |attachment|
|
blob.attachments.each do |attachment|
|
||||||
next unless attachment&.representable?
|
next unless attachment&.representable?
|
||||||
attachment.representation(resize_to_limit: [300, 300]).processed
|
|
||||||
attachment.representation(resize_to_limit: [400, 400]).processed
|
attachment.representation(resize_to_limit: [400, 400]).processed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
class Champs::PieceJustificativeChamp < Champ
|
class Champs::PieceJustificativeChamp < Champ
|
||||||
FILE_MAX_SIZE = 200.megabytes
|
FILE_MAX_SIZE = 200.megabytes
|
||||||
|
|
||||||
has_many_attached :piece_justificative_file do |attachable|
|
has_many_attached :piece_justificative_file
|
||||||
attachable.variant :medium, resize: '400x400'
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
|
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
|
||||||
validates :piece_justificative_file,
|
validates :piece_justificative_file,
|
||||||
|
|
|
@ -2,9 +2,7 @@ class Champs::TitreIdentiteChamp < Champ
|
||||||
FILE_MAX_SIZE = 20.megabytes
|
FILE_MAX_SIZE = 20.megabytes
|
||||||
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
|
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
|
||||||
|
|
||||||
has_many_attached :piece_justificative_file do |attachable|
|
has_many_attached :piece_justificative_file
|
||||||
attachable.variant :medium, resize: '400x400'
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
|
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
|
||||||
validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }
|
validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }
|
||||||
|
|
|
@ -7,7 +7,7 @@ module BlobImageProcessorConcern
|
||||||
watermarked_at.present?
|
watermarked_at.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def variant_required?
|
def representation_required?
|
||||||
attachments.any? { _1.record.class == Champs::TitreIdentiteChamp || _1.record.class == Champs::PieceJustificativeChamp }
|
attachments.any? { _1.record.class == Champs::TitreIdentiteChamp || _1.record.class == Champs::PieceJustificativeChamp }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,13 @@
|
||||||
.fr-container
|
.fr-container
|
||||||
.gallery.gallery-pieces-jointes{ "data-controller": "lightbox" }
|
.gallery.gallery-pieces-jointes{ "data-controller": "lightbox" }
|
||||||
- @champs_with_pieces_jointes.each do |champ|
|
- @champs_with_pieces_jointes.each do |champ|
|
||||||
- champ.piece_justificative_file.each do |attachment|
|
- champ.piece_justificative_file.with_all_variant_records.each do |attachment|
|
||||||
.gallery-item
|
.gallery-item
|
||||||
- blob = attachment.blob
|
- blob = attachment.blob
|
||||||
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
|
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
|
||||||
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
|
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
|
||||||
.thumbnail
|
.thumbnail
|
||||||
= image_tag("pdf-placeholder.png")
|
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
|
||||||
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
||||||
Visualiser
|
Visualiser
|
||||||
.champ-libelle
|
.champ-libelle
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
|
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
|
||||||
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
|
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
|
||||||
.thumbnail
|
.thumbnail
|
||||||
= image_tag(attachment.variant(:medium), loading: :lazy)
|
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
|
||||||
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
||||||
Visualiser
|
Visualiser
|
||||||
.champ-libelle
|
.champ-libelle
|
||||||
|
|
|
@ -5,20 +5,20 @@
|
||||||
%li= render Attachment::ShowComponent.new(attachment:, new_tab: true)
|
%li= render Attachment::ShowComponent.new(attachment:, new_tab: true)
|
||||||
- else
|
- else
|
||||||
.gallery-items-list
|
.gallery-items-list
|
||||||
- champ.piece_justificative_file.attachments.each do |attachment|
|
- champ.piece_justificative_file.attachments.with_all_variant_records.each do |attachment|
|
||||||
.gallery-item
|
.gallery-item
|
||||||
- blob = attachment.blob
|
- blob = attachment.blob
|
||||||
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
|
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
|
||||||
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
|
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
|
||||||
.thumbnail
|
.thumbnail
|
||||||
= image_tag("pdf-placeholder.png")
|
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
|
||||||
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
||||||
= 'Visualiser'
|
= 'Visualiser'
|
||||||
|
|
||||||
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
|
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
|
||||||
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
|
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
|
||||||
.thumbnail
|
.thumbnail
|
||||||
= image_tag(attachment.variant(:medium), loading: :lazy)
|
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
|
||||||
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
|
||||||
= 'Visualiser'
|
= 'Visualiser'
|
||||||
- else
|
- else
|
||||||
|
|
|
@ -89,11 +89,11 @@ describe ImageProcessorJob, type: :job do
|
||||||
|
|
||||||
context "when representation is required" do
|
context "when representation is required" do
|
||||||
before do
|
before do
|
||||||
allow(blob).to receive(:variant_required?).and_return(true)
|
allow(blob).to receive(:representation_required?).and_return(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "it creates blob representation" do
|
it "it creates blob representation" do
|
||||||
expect { described_class.perform_now(blob) }.to change { ActiveStorage::VariantRecord.count }.by(2)
|
expect { described_class.perform_now(blob) }.to change { ActiveStorage::VariantRecord.count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue