From 9e4b7cc84ee0cafdf51ba04fad2ec786c6a2c98d Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:33:39 +0200 Subject: [PATCH 1/9] liaison for label/input --- app/components/attachment/edit_component.rb | 3 +-- .../administrateurs/procedures/_informations.html.haml | 6 +++--- app/views/shared/dossiers/messages/_form.html.haml | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index e096591ef..cd2e7628c 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -165,8 +165,7 @@ class Attachment::EditComponent < ApplicationComponent # There is always a single input by champ, its id must match the label "for" attribute. return champ.input_id end - - helpers.field_id(@form_object_name || @attached_file.record, attribute_name) + @form_object_name || dom_id(@attached_file.record, attribute_name) end def auto_attach_url diff --git a/app/views/administrateurs/procedures/_informations.html.haml b/app/views/administrateurs/procedures/_informations.html.haml index 98458512c..2700aec4f 100644 --- a/app/views/administrateurs/procedures/_informations.html.haml +++ b/app/views/administrateurs/procedures/_informations.html.haml @@ -16,7 +16,7 @@ .fr-fieldset__element .fr-input-group - = f.label :logo, 'Ajouter un logo de la démarche', class: 'fr-label' + = f.label :logo, 'Ajouter un logo de la démarche', class: 'fr-label', for: dom_id(@procedure, :logo) = render Attachment::EditComponent.new(attached_file: @procedure.logo, view_as: :link) .fr-fieldset__element .fr-input-group @@ -55,7 +55,7 @@ .fr-fieldset__element .fr-input-group - = f.label :deliberation, 'Cadre juridique - texte à importer', class: 'fr-label' + = f.label :deliberation, 'Cadre juridique - texte à importer', class: 'fr-label', for: dom_id(@procedure, :deliberation) = render Attachment::EditComponent.new(attached_file: @procedure.deliberation, view_as: :download) %fieldset.fr-fieldset @@ -80,7 +80,7 @@ .fr-fieldset__element .fr-input-group - = f.label :notice, 'Notice explicative de la démarche', class: 'fr-label' + = f.label :notice, 'Notice explicative de la démarche', class: 'fr-label', for: dom_id(@procedure, :notice) %p.fr-hint-text Une notice explicative est un document que vous avez élaboré, destiné à guider l’usager dans sa démarche. Le bouton pour télécharger cette notice apparaît en haut du formulaire pour l’usager. %br diff --git a/app/views/shared/dossiers/messages/_form.html.haml b/app/views/shared/dossiers/messages/_form.html.haml index bd0cfe98e..f8f9eb09c 100644 --- a/app/views/shared/dossiers/messages/_form.html.haml +++ b/app/views/shared/dossiers/messages/_form.html.haml @@ -11,7 +11,7 @@ - if local_assigns.has_key?(:dossier) .fr-mt-3w.fr-input-group - = f.label :piece_jointe, class: "fr-label" + = f.label :piece_jointe, class: "fr-label", for: dom_id(commentaire, :piece_jointe) %div{ data: { controller: "file-input-reset", delete_label: t('views.shared.messages.remove_file') } } = render Attachment::MultipleComponent.new(attached_file: commentaire.piece_jointe) %ul{ data: { 'file-input-reset-target': 'fileList' } } From 3e4aa0cde3063b9b9e00766f4a40b4e21c4b2a80 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Thu, 11 Jul 2024 17:33:52 +0200 Subject: [PATCH 2/9] disabled input with PJ and insert input when PJ is deleted --- app/components/attachment/edit_component.rb | 7 +++---- app/controllers/attachments_controller.rb | 14 +++++++++++++- app/views/attachments/destroy.turbo_stream.haml | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 96a5a8ea4..5ca264507 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -28,7 +28,6 @@ class Attachment::EditComponent < ApplicationComponent # Utilisation du premier attachement comme référence pour la rétrocompatibilité @attachment = @attachments.first - # When parent form has nested attributes, pass the form builder object_name # to correctly infer the input attribute name. @form_object_name = kwargs.delete(:form_object_name) @@ -55,7 +54,7 @@ class Attachment::EditComponent < ApplicationComponent end def destroy_attachment_path - attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id) + attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id, auto_attach_url: @auto_attach_url, view_as: @view_as) end def attachment_input_class @@ -66,7 +65,7 @@ class Attachment::EditComponent < ApplicationComponent track_issue_with_missing_validators if missing_validators? options = { - class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?), + class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true), direct_upload: @direct_upload, id: input_id, aria: { describedby: champ&.describedby_id }, @@ -78,7 +77,7 @@ class Attachment::EditComponent < ApplicationComponent options.merge!(has_content_type_validator? ? { accept: accept_content_type } : {}) options[:multiple] = true if as_multiple? - options[:disabled] = true if @max && @index >= @max + options[:disabled] = true if (@max && @index >= @max) || persisted? options end diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 721ec2cc2..17d5515b2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -21,7 +21,11 @@ class AttachmentsController < ApplicationController @attachment.purge_later flash.notice = 'La pièce jointe a bien été supprimée.' - @champ = find_champ if params[:dossier_id] + if params[:dossier_id] + @champ = find_champ + else + @attachment_options = attachment_options + end respond_to do |format| format.turbo_stream @@ -35,4 +39,12 @@ class AttachmentsController < ApplicationController dossier = policy_scope(Dossier).includes(:champs).find(params[:dossier_id]) dossier.champs.find_by(stable_id: params[:stable_id], row_id: params[:row_id]) end + + def attachment_options + { + attached_file: @attachment.record.public_send(@attachment.name), + auto_attach_url: params[:auto_attach_url], + view_as: params[:view_as]&.to_sym + } + end end diff --git a/app/views/attachments/destroy.turbo_stream.haml b/app/views/attachments/destroy.turbo_stream.haml index e66582528..f17dcaabe 100644 --- a/app/views/attachments/destroy.turbo_stream.haml +++ b/app/views/attachments/destroy.turbo_stream.haml @@ -1,9 +1,9 @@ -= turbo_stream.remove dom_id(@attachment, :persisted_row) - -= turbo_stream.show_all ".attachment-input-#{@attachment.id}" - - if @champ = fields_for @champ.input_name, @champ do |form| = turbo_stream.replace @champ.input_group_id do = render EditableChamp::EditableChampComponent.new champ: @champ, form: form = turbo_stream.focus_all "#attachment-multiple-empty-#{@champ.public_id} input" +- else + = turbo_stream.replace dom_id(@attachment, :edit) do + = render Attachment::EditComponent.new(**@attachment_options) + = turbo_stream.focus_all "##{dom_id(@attachment.record, @attachment.name)}" From 88710969bbb11b69fefb5936269a6617d9e2562a Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:17:51 +0200 Subject: [PATCH 3/9] correction du test sur la suppression de la PJ d'un dossier --- spec/controllers/attachments_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/attachments_controller_spec.rb b/spec/controllers/attachments_controller_spec.rb index b005efbf5..d717db7d4 100644 --- a/spec/controllers/attachments_controller_spec.rb +++ b/spec/controllers/attachments_controller_spec.rb @@ -51,7 +51,7 @@ describe AttachmentsController, type: :controller do let(:signed_id) { attachment.blob.signed_id } subject do - delete :destroy, params: { id: attachment.id, signed_id: signed_id }, format: :turbo_stream + delete :destroy, params: { id: attachment.id, signed_id: signed_id, dossier_id: dossier.id, stable_id: champ.stable_id }, format: :turbo_stream end context "when authenticated" do From 2a955a0f8c6a4d511b2e7801e2c2aae89203f517 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:36:43 +0200 Subject: [PATCH 4/9] =?UTF-8?q?correction=20du=20test=20sur=20l'=C3=A9tat?= =?UTF-8?q?=20de=20l'input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/components/attachment/edit_component_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/components/attachment/edit_component_spec.rb b/spec/components/attachment/edit_component_spec.rb index c9298f9ff..027abacfa 100644 --- a/spec/components/attachment/edit_component_spec.rb +++ b/spec/components/attachment/edit_component_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Attachment::EditComponent, type: :component do let(:attachment) { nil } it 'renders a form field for uploading a file' do - expect(subject).to have_selector('input[type=file]:not(.hidden)') + expect(subject).to have_selector('input[type=file]:not([disabled])') end it 'renders max size' do @@ -37,8 +37,8 @@ RSpec.describe Attachment::EditComponent, type: :component do expect(subject).to have_content(attachment.filename.to_s) end - it 'hides the file field by default' do - expect(subject).to have_selector('input[type=file].hidden') + it 'disabled the file field by default' do + expect(subject).to have_selector('input[type=file][disabled]') end it 'shows the Delete button by default' do From dbacbd8ae2cfab426dedc6a163fd248680528114 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Fri, 12 Jul 2024 18:08:51 +0200 Subject: [PATCH 5/9] ajustement pour PJ attestation --- app/components/attachment/edit_component.rb | 2 +- app/controllers/attachments_controller.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 5ca264507..24ea1535e 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -54,7 +54,7 @@ class Attachment::EditComponent < ApplicationComponent end def destroy_attachment_path - attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id, auto_attach_url: @auto_attach_url, view_as: @view_as) + attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id, auto_attach_url: @auto_attach_url, view_as: @view_as, direct_upload: @direct_upload) end def attachment_input_class diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 17d5515b2..074a102ff 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -43,8 +43,9 @@ class AttachmentsController < ApplicationController def attachment_options { attached_file: @attachment.record.public_send(@attachment.name), - auto_attach_url: params[:auto_attach_url], - view_as: params[:view_as]&.to_sym + view_as: params[:view_as]&.to_sym, + direct_upload: params[:direct_upload] == "true", + auto_attach_url: params[:direct_upload] == "true" ? params[:auto_attach_url] : nil } end end From 249da032873a7e2163f2dcd45d6e68d32ea6d31c Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Fri, 12 Jul 2024 20:14:13 +0200 Subject: [PATCH 6/9] adaptation attestation v2 --- .../administrateurs/attestation_template_v2s/edit.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/administrateurs/attestation_template_v2s/edit.html.haml b/app/views/administrateurs/attestation_template_v2s/edit.html.haml index 7c995e6b2..0c6b12582 100644 --- a/app/views/administrateurs/attestation_template_v2s/edit.html.haml +++ b/app/views/administrateurs/attestation_template_v2s/edit.html.haml @@ -60,7 +60,7 @@ - c.with_hint { "Exemple: Ministère de la Mer. 5 lignes maximum" } .fr-fieldset__element{ data: { attestation_target: 'logoAttachmentFieldset' } } - %label.fr-label{ for: field_id(@attestation_template, :logo) } + %label.fr-label{ for: dom_id(@attestation_template, :logo) } - if @attestation_template.official_layout? = AttestationTemplate.human_attribute_name(:logo_additional) - else @@ -108,7 +108,7 @@ %h2.fr-h4 Pied de page .fr-fieldset__element - %label.fr-label{ for: field_id(@attestation_template, :signature) } Tampon ou signature + %label.fr-label{ for: dom_id(@attestation_template, :signature) } Tampon ou signature %span.fr-hint-text Dimensions conseillées : au minimum 500px de largeur ou de hauteur. From ec24ce3dadcb53430d2d67b4ad7b6783fbb06403 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:58:45 +0200 Subject: [PATCH 7/9] ajustement test PJ dossier en construction --- app/controllers/attachments_controller.rb | 1 - spec/system/users/en_construction_spec.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 074a102ff..23fedafee 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -20,7 +20,6 @@ class AttachmentsController < ApplicationController @attachment = @blob.attachments.find(params[:id]) @attachment.purge_later flash.notice = 'La pièce jointe a bien été supprimée.' - if params[:dossier_id] @champ = find_champ else diff --git a/spec/system/users/en_construction_spec.rb b/spec/system/users/en_construction_spec.rb index f0fd43acf..ba662077e 100644 --- a/spec/system/users/en_construction_spec.rb +++ b/spec/system/users/en_construction_spec.rb @@ -30,6 +30,7 @@ describe "Dossier en_construction" do visit_dossier(dossier) click_on "Supprimer le fichier toto.txt" + expect(page).not_to have_text("toto.txt") input_selector = "#attachment-multiple-empty-#{champ.public_id}" expect(page).to have_selector(input_selector) @@ -37,7 +38,6 @@ describe "Dossier en_construction" do wait_until { champ.reload.for_export == 'file.pdf' } expect(page).to have_text("file.pdf") - expect(page).not_to have_text("toto.txt") end end @@ -54,13 +54,13 @@ describe "Dossier en_construction" do visit_dossier(dossier) click_on "Supprimer le fichier toto.png" + expect(page).not_to have_text("toto.png") input_selector = "##{champ.input_id}" expect(page).to have_selector(input_selector) find(input_selector).attach_file(Rails.root.join('spec/fixtures/files/file.pdf')) expect(page).to have_text("file.pdf") - expect(page).not_to have_text("toto.png") end end From 79ba52317a5105ac93c0c781c8309d54634f3adc Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:59:57 +0200 Subject: [PATCH 8/9] limitation des params selon le type de PJ --- app/components/attachment/edit_component.rb | 8 +++++++- app/controllers/attachments_controller.rb | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 24ea1535e..20b01788a 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -20,6 +20,7 @@ class Attachment::EditComponent < ApplicationComponent @user_can_replace = user_can_replace @as_multiple = as_multiple @auto_attach_url = auto_attach_url + # Adaptation pour la gestion des pièces jointes multiples @attachments = attachments.presence || (kwargs.key?(:attachment) ? [kwargs.delete(:attachment)] : []) @attachments << attached_file.attachment if attached_file.respond_to?(:attachment) && @attachments.empty? @@ -28,6 +29,7 @@ class Attachment::EditComponent < ApplicationComponent # Utilisation du premier attachement comme référence pour la rétrocompatibilité @attachment = @attachments.first + # When parent form has nested attributes, pass the form builder object_name # to correctly infer the input attribute name. @form_object_name = kwargs.delete(:form_object_name) @@ -54,7 +56,11 @@ class Attachment::EditComponent < ApplicationComponent end def destroy_attachment_path - attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id, auto_attach_url: @auto_attach_url, view_as: @view_as, direct_upload: @direct_upload) + if champ.present? + attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id) + else + attachment_path(auto_attach_url: @auto_attach_url, view_as: @view_as, direct_upload: @direct_upload) + end end def attachment_input_class diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 23fedafee..074a102ff 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -20,6 +20,7 @@ class AttachmentsController < ApplicationController @attachment = @blob.attachments.find(params[:id]) @attachment.purge_later flash.notice = 'La pièce jointe a bien été supprimée.' + if params[:dossier_id] @champ = find_champ else From 81932915c61db6ffe6f3290f44c2d0fc82e300b6 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:39:12 +0200 Subject: [PATCH 9/9] suppresion du form_object_name dans input_id --- app/components/attachment/edit_component.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 20b01788a..90d0a5e58 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -171,9 +171,10 @@ class Attachment::EditComponent < ApplicationComponent def input_id if champ.present? # There is always a single input by champ, its id must match the label "for" attribute. - return champ.input_id + champ.input_id + else + dom_id(@attached_file.record, attribute_name) end - @form_object_name || dom_id(@attached_file.record, attribute_name) end def auto_attach_url