refactor(dossier): use champ id as champ attributes key
This commit is contained in:
parent
1126099e0b
commit
b94bc8fd8a
4 changed files with 34 additions and 10 deletions
|
@ -137,6 +137,23 @@ class Champ < ApplicationRecord
|
||||||
"#{html_id}-input"
|
"#{html_id}-input"
|
||||||
end
|
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
|
def labelledby_id
|
||||||
"#{html_id}-label"
|
"#{html_id}-label"
|
||||||
end
|
end
|
||||||
|
@ -169,6 +186,14 @@ class Champ < ApplicationRecord
|
||||||
"#{stable_id}-#{id}"
|
"#{stable_id}-#{id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def champs_attributes_accessor
|
||||||
|
if private?
|
||||||
|
"champs_private_attributes"
|
||||||
|
else
|
||||||
|
"champs_attributes"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def needs_dossier_id?
|
def needs_dossier_id?
|
||||||
!dossier_id && parent_id
|
!dossier_id && parent_id
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,10 +6,9 @@
|
||||||
- if @dossier.champs_private.present?
|
- if @dossier.champs_private.present?
|
||||||
%section
|
%section
|
||||||
= form_for @dossier, url: annotations_instructeur_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' } do |f|
|
= 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|
|
- @dossier.champs_private.each do |champ|
|
||||||
- champ = champ_form.object
|
= fields_for champ.input_name, champ do |form|
|
||||||
= render partial: "shared/dossiers/editable_champs/editable_champ",
|
= render partial: "shared/dossiers/editable_champs/editable_champ", locals: { form: form, champ: champ, seen_at: @annotations_privees_seen_at }
|
||||||
locals: { champ: champ, form: champ_form, seen_at: @annotations_privees_seen_at }
|
|
||||||
|
|
||||||
.send-wrapper
|
.send-wrapper
|
||||||
= f.submit 'Sauvegarder', class: 'button primary send', data: { disable: true }
|
= f.submit 'Sauvegarder', class: 'button primary send', data: { disable: true }
|
||||||
|
|
|
@ -35,10 +35,9 @@
|
||||||
dossier.procedure.groupe_instructeurs.order(:label).map { |gi| [gi.label, gi.id] },
|
dossier.procedure.groupe_instructeurs.order(:label).map { |gi| [gi.label, gi.id] },
|
||||||
{ include_blank: dossier.brouillon? }
|
{ include_blank: dossier.brouillon? }
|
||||||
|
|
||||||
= f.fields_for :champs, dossier.champs do |champ_form|
|
- dossier.champs.each do |champ|
|
||||||
- champ = champ_form.object
|
= fields_for champ.input_name, champ do |form|
|
||||||
= render partial: "shared/dossiers/editable_champs/editable_champ",
|
= render partial: "shared/dossiers/editable_champs/editable_champ", locals: { form: form, champ: champ }
|
||||||
locals: { champ: champ, form: champ_form }
|
|
||||||
|
|
||||||
- if !dossier.for_procedure_preview?
|
- if !dossier.for_procedure_preview?
|
||||||
.dossier-edit-sticky-footer
|
.dossier-edit-sticky-footer
|
||||||
|
|
|
@ -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 }
|
= 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"
|
- if champ.type_champ == "titre_identite"
|
||||||
%p.notice Carte nationale d’identité (uniquement le recto), passeport, titre de séjour ou autre justificatif d’identité. Formats acceptés : jpg/png
|
%p.notice Carte nationale d’identité (uniquement le recto), passeport, titre de séjour ou autre justificatif d’identité. 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 }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue