refactor(attachment): add Attachment::ShowComponent

This commit is contained in:
Paul Chavard 2022-05-06 12:20:57 +02:00
parent d6384caf10
commit 7e1c793728
5 changed files with 39 additions and 14 deletions

View file

@ -0,0 +1,27 @@
class Attachment::ShowComponent < ApplicationComponent
def initialize(attachment:, user_can_upload: false)
@attachment = attachment
@user_can_upload = user_can_upload
end
attr_reader :attachment
def user_can_upload?
@user_can_upload
end
def should_display_link?
(attachment.virus_scanner.safe? || !attachment.virus_scanner.started?) && !attachment.watermark_pending?
end
def attachment_path
helpers.attachment_path(attachment.id, { signed_id: attachment.blob.signed_id, user_can_upload: user_can_upload? })
end
def poll_controller_options
{
controller: 'turbo-poll',
turbo_poll_url_value: attachment_path
}
end
end

View file

@ -0,0 +1,2 @@
---
en:

View file

@ -0,0 +1,2 @@
---
fr:

View file

@ -1,12 +1,5 @@
- should_display_link = (attachment.virus_scanner.safe? || !attachment.virus_scanner.started?) && !attachment.watermark_pending?
- user_can_upload = defined?(user_can_upload) ? user_can_upload : false
- if should_display_link
- attachment_check_url = false
- else
- attachment_check_url = attachment_url(attachment.id, { signed_id: attachment.blob.signed_id, user_can_upload: user_can_upload })
.attachment-link{ 'data-attachment-id': attachment.id, 'data-attachment-poll-url': attachment_check_url }
- if should_display_link
.attachment-link{ id: dom_id(attachment, :show) }
- if should_display_link?
= link_to url_for(attachment.blob), target: '_blank', rel: 'noopener', title: "Télécharger la pièce jointe" do
%span.icon.attached
= attachment.filename.to_s
@ -14,22 +7,23 @@
(ce fichier na pas été analysé par notre antivirus, téléchargez-le avec précaution)
- else
%span{ data: poll_controller_options }
= attachment.filename.to_s
- if attachment.virus_scanner.pending?
(analyse antivirus en cours
= link_to "rafraichir", request.path, data: { 'attachment-refresh': true }
= link_to "rafraichir", attachment_path, data: { action: 'turbo-poll#refresh' }
)
- elsif attachment.watermark_pending?
(traitement de la pièce en cours
= link_to "rafraichir", request.path, data: { 'attachment-refresh': true }
= link_to "rafraichir", attachment_path, data: { action: 'turbo-poll#refresh' }
)
- elsif attachment.virus_scanner.infected?
- if user_can_upload
- if user_can_upload?
(virus détecté, merci denvoyer un autre fichier)
- else
(virus détecté, le téléchargement de ce fichier est bloqué)
- elsif attachment.virus_scanner.corrupt?
- if user_can_upload
- if user_can_upload?
(le fichier est corrompu, merci denvoyer un autre fichier)
- else
(le fichier est corrompu, le téléchargement est bloqué)

View file

@ -6,7 +6,7 @@ describe 'shared/attachment/_show.html.haml', type: :view do
champ.piece_justificative_file.blob.update(metadata: champ.piece_justificative_file.blob.metadata.merge(virus_scan_result: virus_scan_result))
end
subject { render 'shared/attachment/show', attachment: champ.piece_justificative_file.attachment }
subject { render Attachment::ShowComponent.new(attachment: champ.piece_justificative_file.attachment) }
context 'when there is no anti-virus scan' do
let(:virus_scan_result) { nil }