chore(piece_justificative): continue attachments refactor

This commit is contained in:
Colin Darie 2022-10-29 16:08:59 +02:00
parent 7ae6efe776
commit 6a0379086d
11 changed files with 36 additions and 32 deletions

View file

@ -4,16 +4,16 @@ class Attachment::EditComponent < ApplicationComponent
delegate :persisted?, to: :attachment, allow_nil: true
def initialize(form:, attached_file:, attachment: nil, user_can_destroy: false, direct_upload: true, id: nil, index: 0)
def initialize(form:, attached_file:, user_can_destroy: false, direct_upload: true, id: nil, index: 0, **kwargs)
@form = form
@attached_file = attached_file
@attachment = if attachment
attachment
@attachment = if kwargs.key?(:attachment)
kwargs[:attachment]
elsif attached_file.respond_to?(:attachment)
attached_file.attachment
else
# multiple attachments: attachment kwarg is expected, unless adding a new attachment
fail ArgumentError, "You must pass an `attachment` kwarg when not using as single attachment like in #{attached_file.name}. Set it to nil for a new attachment."
end
@user_can_destroy = user_can_destroy

View file

@ -1,4 +1,4 @@
.fr-mb-2w
.fr-mb-2w.attachment
- if persisted?
.attachment-actions{ id: dom_id(attachment, :actions) }
.attachment-action

View file

@ -6,7 +6,7 @@
= render Attachment::EditComponent.new(form:, attached_file:, attachment:, user_can_destroy:, direct_upload:, id:, index:)
%div{class: [attachments_empty? ? nil : "hidden"], data: { "#{stimulus_controller_name}-target": "empty" }}
= render Attachment::EditComponent.new(form:, attached_file:, user_can_destroy:, direct_upload:, id:, index: attachments_count)
= render Attachment::EditComponent.new(form:, attached_file:, attachment: nil, user_can_destroy:, direct_upload:, id:, index: attachments_count)
- if can_attach_next?
%button.fr-btn.fr-btn--tertiary.fr-btn--sm{ data: { "#{stimulus_controller_name}-target": "buttonAdd", action: "click->attachment-multiple#add" }} Ajouter un fichier

View file

@ -1,2 +1,2 @@
- user_can_destroy = !@champ.mandatory? || @champ.dossier.brouillon?
= render Attachment::EditComponent.new(form: @form, attached_file: @champ.piece_justificative_file, user_can_destroy: user_can_destroy)
= render Attachment::EditComponent.new(form: @form, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], user_can_destroy: user_can_destroy)

View file

@ -118,9 +118,9 @@ export class AutoUpload {
}
get errorElement() {
return this.#input.parentElement?.querySelector<HTMLElement>(
'.attachment-error'
);
return this.#input
.closest('.attachment')
?.querySelector<HTMLElement>('.attachment-error');
}
get errorTitleElement() {

View file

@ -1,5 +1,6 @@
- pj = champ.piece_justificative_file
- if pj.attached?
= render Attachment::ShowComponent.new(attachment: pj.attachment)
- pj.attachments.each do |attachment|
= render Attachment::ShowComponent.new(attachment:)
- else
Pièce justificative non fournie

View file

@ -1,6 +1,6 @@
describe AttachmentsController, type: :controller do
let(:user) { create(:user) }
let(:attachment) { champ.piece_justificative_file.attachment }
let(:attachment) { champ.piece_justificative_file.attachments.first }
let(:dossier) { create(:dossier, user: user) }
let(:champ) { create(:champ_piece_justificative, dossier_id: dossier.id) }
let(:signed_id) { attachment.blob.signed_id }
@ -45,7 +45,7 @@ describe AttachmentsController, type: :controller do
describe '#destroy' do
render_views
let(:attachment) { champ.piece_justificative_file.attachment }
let(:attachment) { champ.piece_justificative_file.attachments.first }
let(:dossier) { create(:dossier, user: user) }
let(:champ) { create(:champ_piece_justificative, dossier_id: dossier.id) }
let(:signed_id) { attachment.blob.signed_id }

View file

@ -23,7 +23,7 @@ describe Champs::PieceJustificativeController, type: :controller do
subject
champ.reload
expect(champ.piece_justificative_file.attached?).to be true
expect(champ.piece_justificative_file.filename).to eq('piece_justificative_0.pdf')
expect(champ.piece_justificative_file[0].filename).to eq('piece_justificative_0.pdf')
end
it 'renders the attachment template as Javascript' do

View file

@ -6,7 +6,9 @@ describe ChampSerializer do
context 'when type champ is piece justificative' do
let(:champ) { create(:champ_piece_justificative) }
it { expect(subject[:value]).to match('/rails/active_storage/disk/') }
it {
expect(subject[:value]).to match_array([a_string_matching('/rails/active_storage/disk/')])
}
end
context 'when type champ is not piece justificative' do

View file

@ -3,16 +3,16 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:virus_scan_result) { nil }
before do
champ.piece_justificative_file.blob.update(metadata: champ.piece_justificative_file.blob.metadata.merge(virus_scan_result: virus_scan_result))
champ.piece_justificative_file[0].blob.update(metadata: champ.piece_justificative_file[0].blob.metadata.merge(virus_scan_result: virus_scan_result))
end
subject { render Attachment::ShowComponent.new(attachment: champ.piece_justificative_file.attachment) }
subject { render Attachment::ShowComponent.new(attachment: champ.piece_justificative_file.attachments.first) }
context 'when there is no anti-virus scan' do
let(:virus_scan_result) { nil }
it 'allows to download the file' do
expect(subject).to have_link(champ.piece_justificative_file.filename.to_s)
expect(subject).to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text('ce fichier na pas été analysé par notre antivirus')
end
end
@ -21,8 +21,8 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file.filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file.filename.to_s)
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text('analyse antivirus en cours')
end
end
@ -31,9 +31,9 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:champ) { create(:champ_titre_identite) }
it 'displays the filename, but doesnt allow to download the file' do
pp champ.piece_justificative_file.attachment.watermark_pending?
expect(subject).to have_text(champ.piece_justificative_file.filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file.filename.to_s)
expect(champ.piece_justificative_file.attachments[0].watermark_pending?).to be_truthy
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text('traitement de la pièce en cours')
end
end
@ -42,7 +42,7 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
it 'allows to download the file' do
expect(subject).to have_link(champ.piece_justificative_file.filename.to_s)
expect(subject).to have_link(champ.piece_justificative_file[0].filename.to_s)
end
end
@ -50,8 +50,8 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file.filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file.filename.to_s)
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text('virus détecté')
end
end
@ -60,8 +60,8 @@ describe 'shared/attachment/_show.html.haml', type: :view do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INTEGRITY_ERROR }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file.filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file.filename.to_s)
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text('corrompu')
end
end

View file

@ -1,12 +1,12 @@
describe 'shared/attachment/_update.html.haml', type: :view do
let(:champ) { build(:champ_piece_justificative, dossier: create(:dossier)) }
let(:champ) { build(:champ_titre_identite, dossier: create(:dossier)) }
let(:attached_file) { champ.piece_justificative_file }
let(:user_can_destroy) { false }
let(:template) { nil }
subject do
form_for(champ.dossier) do |form|
view.render Attachment::EditComponent.new(form: form, attached_file: attached_file, user_can_destroy: true, direct_upload: true)
view.render Attachment::EditComponent.new(form: form, attached_file: attached_file, attachment: attached_file[0], user_can_destroy: true, direct_upload: true)
end
end
@ -26,7 +26,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
end
it 'does not renders a link to the unsaved file' do
expect(subject).not_to have_content(attached_file.filename.to_s)
expect(subject).not_to have_content(attached_file.attachments[0].filename.to_s)
end
it 'does not render action buttons' do
@ -37,7 +37,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
before { champ.save! }
it 'renders a link to the file' do
expect(subject).to have_content(attached_file.filename.to_s)
expect(subject).to have_content(attached_file.attachments[0].filename.to_s)
end
it 'hides the form field by default' do
@ -55,6 +55,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
form_for(champ.dossier) do |form|
render Attachment::EditComponent.new(form: form,
attached_file: attached_file,
attachment: attached_file[0],
user_can_destroy: user_can_destroy,
direct_upload: true)
end