From 6e8b9cf668c8ce5d745cc5ce190146ef51a066b1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 2 May 2019 11:37:27 +0200 Subject: [PATCH] Refactor piece_jointe template to avoid repetition --- .../piece_justificative/_pj_link.html.haml | 21 ------------------- .../piece_justificative/_show.html.haml | 2 +- .../_piece_justificative.html.haml | 2 +- .../shared/piece_jointe/_pj_link.html.haml | 10 ++++----- .../_pj_link.html.haml_spec.rb | 20 ++++++++---------- 5 files changed, 16 insertions(+), 39 deletions(-) delete mode 100644 app/views/shared/champs/piece_justificative/_pj_link.html.haml rename spec/views/shared/{champs/piece_justificative => piece_jointe}/_pj_link.html.haml_spec.rb (65%) diff --git a/app/views/shared/champs/piece_justificative/_pj_link.html.haml b/app/views/shared/champs/piece_justificative/_pj_link.html.haml deleted file mode 100644 index 6eadf1fbd..000000000 --- a/app/views/shared/champs/piece_justificative/_pj_link.html.haml +++ /dev/null @@ -1,21 +0,0 @@ -- pj = champ.piece_justificative_file - -.pj-link - - if champ.virus_scan.blank? || champ.virus_scan.safe? - = link_to url_for(pj), target: '_blank', rel: 'noopener', title: "Télécharger la pièce jointe" do - %span.icon.attachment - = pj.filename.to_s - - if champ.virus_scan.blank? - (ce fichier n’a pas été analysé par notre antivirus, téléchargez-le avec précaution) - - - else - = pj.filename.to_s - - if champ.virus_scan.pending? - (analyse antivirus en cours - = link_to "rafraichir", request.path - ) - - elsif champ.virus_scan.infected? - - if user_can_upload - (virus détecté, merci d’envoyer un autre fichier) - - else - (virus détecté, le téléchargement de ce fichier est bloqué) diff --git a/app/views/shared/champs/piece_justificative/_show.html.haml b/app/views/shared/champs/piece_justificative/_show.html.haml index 6c0e1cf7e..2332a8fc7 100644 --- a/app/views/shared/champs/piece_justificative/_show.html.haml +++ b/app/views/shared/champs/piece_justificative/_show.html.haml @@ -1,5 +1,5 @@ - pj = champ.piece_justificative_file - if pj.attached? - = render partial: "shared/champs/piece_justificative/pj_link", locals: { champ: champ, user_can_upload: false } + = render partial: "shared/piece_jointe/pj_link", locals: { object: champ, pj: pj, user_can_upload: false } - else Pièce justificative non fournie diff --git a/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml b/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml index 2906296a5..a20ba19d7 100644 --- a/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml +++ b/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml @@ -9,7 +9,7 @@ - if pj.attached? .piece-justificative-actions{ id: "piece_justificative_#{champ.id}" } .piece-justificative-action - = render partial: "shared/champs/piece_justificative/pj_link", locals: { champ: champ, user_can_upload: true } + = render partial: "shared/piece_jointe/pj_link", locals: { object: champ, pj: champ.piece_justificative_file, user_can_upload: true } .piece-justificative-action - if champ.private? = link_to 'Supprimer', gestionnaire_champ_purge_champ_piece_justificative_path(procedure_id: champ.dossier.procedure_id, dossier_id: champ.dossier_id, champ_id: champ.id), remote: true, method: :delete, class: 'button small danger' diff --git a/app/views/shared/piece_jointe/_pj_link.html.haml b/app/views/shared/piece_jointe/_pj_link.html.haml index 656d1dd4a..e6d804918 100644 --- a/app/views/shared/piece_jointe/_pj_link.html.haml +++ b/app/views/shared/piece_jointe/_pj_link.html.haml @@ -1,19 +1,19 @@ - if pj.attached? .pj-link - - if object.virus_scan.blank? || object.virus_scan.safe? - = link_to url_for(pj), target: '_blank', title: "Télécharger la pièce jointe" do + - if object.virus_scan_safe? || object.virus_scan_no_scan? + = link_to url_for(pj), target: '_blank', rel: 'noopener', title: "Télécharger la pièce jointe" do %span.icon.attachment = pj.filename.to_s - - if object.virus_scan.blank? + - if object.virus_scan_no_scan? (ce fichier n’a pas été analysé par notre antivirus, téléchargez-le avec précaution) - else = pj.filename.to_s - - if object.virus_scan.pending? + - if object.virus_scan_pending? (analyse antivirus en cours = link_to "rafraichir", request.path ) - - elsif object.virus_scan.infected? + - elsif object.virus_scan_infected? - if user_can_upload (virus détecté, merci d’envoyer un autre fichier) - else diff --git a/spec/views/shared/champs/piece_justificative/_pj_link.html.haml_spec.rb b/spec/views/shared/piece_jointe/_pj_link.html.haml_spec.rb similarity index 65% rename from spec/views/shared/champs/piece_justificative/_pj_link.html.haml_spec.rb rename to spec/views/shared/piece_jointe/_pj_link.html.haml_spec.rb index 4eb6c12d4..9bf655204 100644 --- a/spec/views/shared/champs/piece_justificative/_pj_link.html.haml_spec.rb +++ b/spec/views/shared/piece_jointe/_pj_link.html.haml_spec.rb @@ -1,19 +1,17 @@ require 'rails_helper' -describe 'shared/champs/piece_justificative/_pj_link.html.haml', type: :view do - let(:champ) { create(:champ, :piece_justificative, :with_piece_justificative_file) } - let(:virus_scan) { nil } +describe 'shared/piece_jointe/_pj_link.html.haml', type: :view do + let(:champ) { create(:champ_piece_justificative) } + let(:virus_scan_result) { nil } before do - if virus_scan - champ.update(virus_scan: virus_scan) - end + champ.piece_justificative_file.blob.update(metadata: champ.piece_justificative_file.blob.metadata.merge(virus_scan_result: virus_scan_result)) end - subject { render 'shared/champs/piece_justificative/pj_link', champ: champ, user_can_upload: false } + subject { render 'shared/piece_jointe/pj_link', object: champ, pj: champ.piece_justificative_file, user_can_upload: false } context 'when there is no anti-virus scan' do - let(:virus_scan) { nil } + let(:virus_scan_result) { nil } it 'allows to download the file' do expect(subject).to have_link(champ.piece_justificative_file.filename.to_s) @@ -22,7 +20,7 @@ describe 'shared/champs/piece_justificative/_pj_link.html.haml', type: :view do end context 'when the anti-virus scan is pending' do - let(:virus_scan) { create(:virus_scan, :pending) } + let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING } it 'displays the filename, but doesn’t allow to download the file' do expect(subject).to have_text(champ.piece_justificative_file.filename.to_s) @@ -32,7 +30,7 @@ describe 'shared/champs/piece_justificative/_pj_link.html.haml', type: :view do end context 'when the file is scanned and safe' do - let(:virus_scan) { create(:virus_scan, :safe) } + 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) @@ -40,7 +38,7 @@ describe 'shared/champs/piece_justificative/_pj_link.html.haml', type: :view do end context 'when the file is scanned and infected' do - let(:virus_scan) { create(:virus_scan, :infected) } + let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED } it 'displays the filename, but doesn’t allow to download the file' do expect(subject).to have_text(champ.piece_justificative_file.filename.to_s)