refactor(dossier): use champ id as champ attributes key

This commit is contained in:
Paul Chavard 2022-04-28 15:03:07 +02:00
parent 1126099e0b
commit b94bc8fd8a
4 changed files with 34 additions and 10 deletions

View file

@ -137,6 +137,23 @@ class Champ < ApplicationRecord
"#{html_id}-input"
end
# A predictable string to use when generating an input name for this champ.
#
# Rail's FormBuilder can auto-generate input names, using the form "dossier[champs_attributes][5]",
# where [5] is the index of the field in the form.
# However the field index makes it difficult to render a single field, independent from the ordering of the others.
#
# Luckily, this is only used to make the name unique, but the actual value is ignored when Rails parses nested
# attributes. So instead of the field index, this method uses the champ id; which gives us an independent and
# predictable input name.
def input_name
if parent_id
"#{parent.input_name}[#{champs_attributes_accessor}][#{id}]"
else
"dossier[#{champs_attributes_accessor}][#{id}]"
end
end
def labelledby_id
"#{html_id}-label"
end
@ -169,6 +186,14 @@ class Champ < ApplicationRecord
"#{stable_id}-#{id}"
end
def champs_attributes_accessor
if private?
"champs_private_attributes"
else
"champs_attributes"
end
end
def needs_dossier_id?
!dossier_id && parent_id
end

View file

@ -6,10 +6,9 @@
- if @dossier.champs_private.present?
%section
= form_for @dossier, url: annotations_instructeur_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' } do |f|
= f.fields_for :champs_private, f.object.champs_private do |champ_form|
- champ = champ_form.object
= render partial: "shared/dossiers/editable_champs/editable_champ",
locals: { champ: champ, form: champ_form, seen_at: @annotations_privees_seen_at }
- @dossier.champs_private.each do |champ|
= fields_for champ.input_name, champ do |form|
= render partial: "shared/dossiers/editable_champs/editable_champ", locals: { form: form, champ: champ, seen_at: @annotations_privees_seen_at }
.send-wrapper
= f.submit 'Sauvegarder', class: 'button primary send', data: { disable: true }

View file

@ -35,10 +35,9 @@
dossier.procedure.groupe_instructeurs.order(:label).map { |gi| [gi.label, gi.id] },
{ include_blank: dossier.brouillon? }
= f.fields_for :champs, dossier.champs do |champ_form|
- champ = champ_form.object
= render partial: "shared/dossiers/editable_champs/editable_champ",
locals: { champ: champ, form: champ_form }
- dossier.champs.each do |champ|
= fields_for champ.input_name, champ do |form|
= render partial: "shared/dossiers/editable_champs/editable_champ", locals: { form: form, champ: champ }
- if !dossier.for_procedure_preview?
.dossier-edit-sticky-footer

View file

@ -8,5 +8,6 @@
= render partial: 'shared/dossiers/editable_champs/champ_label', locals: { form: form, champ: champ, seen_at: defined?(seen_at) ? seen_at : nil }
- if champ.type_champ == "titre_identite"
%p.notice Carte nationale didentité (uniquement le recto), passeport, titre de séjour ou autre justificatif didentité. Formats acceptés : jpg/png
= render partial: "shared/dossiers/editable_champs/#{champ.type_champ}",
locals: { champ: champ, form: form }
= form.hidden_field :id, value: champ.id
= render partial: "shared/dossiers/editable_champs/#{champ.type_champ}", locals: { form: form, champ: champ }