From b8a49e901231b1bc0d613db6b9853938170851f9 Mon Sep 17 00:00:00 2001 From: Judith Date: Wed, 29 Jul 2020 12:15:58 +0200 Subject: [PATCH] creation of attachment_upload_helpers and integration in the code --- app/helpers/attachment_upload_helper.rb | 19 +++++++++++++++++++ .../instructeurs/avis/instruction.html.haml | 5 +---- .../instructeurs/shared/avis/_form.html.haml | 5 +---- .../procedures/_informations.html.haml | 16 +++------------- app/views/shared/attachment/_edit.html.haml | 3 ++- .../attachment/_update.html.haml_spec.rb | 7 +------ 6 files changed, 27 insertions(+), 28 deletions(-) create mode 100644 app/helpers/attachment_upload_helper.rb diff --git a/app/helpers/attachment_upload_helper.rb b/app/helpers/attachment_upload_helper.rb new file mode 100644 index 000000000..0edcc7cb9 --- /dev/null +++ b/app/helpers/attachment_upload_helper.rb @@ -0,0 +1,19 @@ +module AttachmentUploadHelper + def image_upload_and_render(form, file, direct_upload = nil) + render 'shared/attachment/edit', { + form: form, + attached_file: file, + accept: 'image/png, image/jpg, image/jpeg', + user_can_destroy: true, + direct_upload: direct_upload + } + end + + def text_upload_and_render(form, file) + render 'shared/attachment/edit', { + form: form, + attached_file: file, + user_can_destroy: true + } + end +end diff --git a/app/views/instructeurs/avis/instruction.html.haml b/app/views/instructeurs/avis/instruction.html.haml index 1cefc6aea..2dc248ee2 100644 --- a/app/views/instructeurs/avis/instruction.html.haml +++ b/app/views/instructeurs/avis/instruction.html.haml @@ -17,10 +17,7 @@ = form_for @avis, url: instructeur_avis_path(@avis.procedure, @avis), html: { class: 'form' } do |f| = f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true - = render 'shared/attachment/edit', - { form: f, - attached_file: @avis.piece_justificative_file, - user_can_destroy: true } + = text_upload_and_render f, @avis.piece_justificative_file .flex.justify-between.align-baseline %p.confidentiel.flex diff --git a/app/views/instructeurs/shared/avis/_form.html.haml b/app/views/instructeurs/shared/avis/_form.html.haml index e06b538c4..a1bcf1fac 100644 --- a/app/views/instructeurs/shared/avis/_form.html.haml +++ b/app/views/instructeurs/shared/avis/_form.html.haml @@ -7,10 +7,7 @@ = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true %p.tab-title Ajouter une pièce jointe .form-group - = render 'shared/attachment/edit', - { form: f, - attached_file: avis.introduction_file, - user_can_destroy: true } + = text_upload_and_render f, avis.introduction_file - if linked_dossiers.present? = f.check_box :invite_linked_dossiers, {}, true, false diff --git a/app/views/new_administrateur/procedures/_informations.html.haml b/app/views/new_administrateur/procedures/_informations.html.haml index 4b2c5ce8b..e3ba343bb 100644 --- a/app/views/new_administrateur/procedures/_informations.html.haml +++ b/app/views/new_administrateur/procedures/_informations.html.haml @@ -14,11 +14,7 @@ = f.text_area :description, rows: '6', placeholder: 'Description de la démarche, destinataires, etc. ', class: 'form-control' %h3.header-subsection Logo de la démarche -= render 'shared/attachment/edit', - { form: f, - attached_file: @procedure.logo, - accept: 'image/png, image/jpg, image/jpeg', - user_can_destroy: true } += image_upload_and_render f, @procedure.logo - if !@procedure.locked? %h3.header-subsection Conservation des données @@ -59,10 +55,7 @@ = f.text_field :cadre_juridique, class: 'form-control', placeholder: 'https://www.legifrance.gouv.fr/' = f.label :deliberation, 'Importer le texte' -= render 'shared/attachment/edit', - { form: f, - attached_file: @procedure.deliberation, - user_can_destroy: true } += text_upload_and_render f, @procedure.deliberation %h3.header-subsection Notice explicative de la démarche @@ -73,10 +66,7 @@ %p.notice Formats acceptés : .doc, .odt, .pdf, .ppt, .pptx - notice = @procedure.notice -= render 'shared/attachment/edit', - { form: f, - attached_file: @procedure.notice, - user_can_destroy: true } += text_upload_and_render f, @procedure.notice - if !@procedure.locked? %h3.header-subsection À qui s’adresse ma démarche ? diff --git a/app/views/shared/attachment/_edit.html.haml b/app/views/shared/attachment/_edit.html.haml index 6af28a889..79910db79 100644 --- a/app/views/shared/attachment/_edit.html.haml +++ b/app/views/shared/attachment/_edit.html.haml @@ -5,6 +5,7 @@ - persisted = attachment && attachment.persisted? - accept = defined?(accept) ? accept : nil - user_can_destroy = defined?(user_can_destroy) ? user_can_destroy : false +- direct_upload = direct_upload != nil ? false : true .attachment - if defined?(template) && template.attached? @@ -34,5 +35,5 @@ = form.file_field attached_file.name, class: "attachment-input attachment-input-#{attachment_id} #{'hidden' if persisted}", accept: accept, - direct_upload: true, + direct_upload: direct_upload, data: { 'auto-attach-url': auto_attach_url(form, form.object) } diff --git a/spec/views/shared/attachment/_update.html.haml_spec.rb b/spec/views/shared/attachment/_update.html.haml_spec.rb index dc65cc00c..3e94f9e8e 100644 --- a/spec/views/shared/attachment/_update.html.haml_spec.rb +++ b/spec/views/shared/attachment/_update.html.haml_spec.rb @@ -5,12 +5,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do subject do form_for(champ.dossier) do |form| - render 'shared/attachment/edit', { - form: form, - attached_file: attached_file, - accept: 'image/png', - user_can_destroy: user_can_destroy - } + view.image_upload_and_render form, attached_file end end