refactor(champ): simplify repetition champ

This commit is contained in:
Paul Chavard 2022-04-28 15:06:40 +02:00
parent 39ecc669b1
commit 2d1370d123
8 changed files with 27 additions and 49 deletions

View file

@ -1,16 +0,0 @@
- champs = champ.rows.last
- if champs.present?
- index = (champ.rows.size - 1) * champs.size
- row_dom_id = "row-#{SecureRandom.hex(4)}"
%div{ class: "row row-#{champs.first.row}", id: row_dom_id }
-# Tell the controller which DOM element should be removed once the row deletion is successful
= hidden_field_tag 'deleted_row_dom_ids[]', row_dom_id, disabled: true
- champs.each.with_index(index) do |champ, index|
= fields_for "#{attribute}[#{index}]", champ do |form|
= render partial: "shared/dossiers/editable_champs/editable_champ", locals: { champ: champ, form: form }
= form.hidden_field :id
= form.hidden_field :_destroy, disabled: true
.flex.row-reverse
%button.button.danger.remove-row
Supprimer lélément

View file

@ -1,3 +1,3 @@
<%= append_to_element(".repetition-#{@position}",
partial: 'champs/repetition/show',
locals: { champ: @champ, attribute: @attribute }) %>
<%= fields_for @champ.input_name, @champ do |form| %>
<%= append_to_element("##{@champ.input_group_id} .repetition", partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: @champ, row: @champs }) %>
<% end %>

View file

@ -1,24 +1,9 @@
%div{ class: "repetition-#{form.index}" }
.repetition
- champ.rows.each do |champs|
- row_dom_id = "row-#{SecureRandom.hex(4)}"
%div{ class: "row row-#{champs.first.row}", id: row_dom_id }
-# Tell the controller which DOM element should be removed once the row deletion is successful
= hidden_field_tag 'deleted_row_dom_ids[]', row_dom_id, disabled: true
- champs.each do |champ|
= form.fields_for :champs, champ do |form|
= render partial: 'shared/dossiers/editable_champs/editable_champ', locals: { champ: form.object, form: form }
= form.hidden_field :_destroy, disabled: true
.flex.row-reverse
- if champ.persisted?
%button.button.danger.remove-row{ type: :button }
Supprimer lélément
- else
%button.button.danger{ type: :button }
Supprimer lélément
= render partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: champ, row: champs }
- if champ.persisted?
= link_to champs_repetition_path(form.index), class: 'button add-row', data: { remote: true, disable: true, method: 'POST', params: { champ_id: champ&.id }.to_query } do
= link_to champs_repetition_path(champ.id), class: 'button add-row', data: { remote: true, disable: true, method: 'POST' } do
%span.icon.add
Ajouter un élément pour « #{champ.libelle} »
- else

View file

@ -0,0 +1,13 @@
- row_dom_id = "row-#{SecureRandom.hex(4)}"
.row{ id: row_dom_id }
-# Tell the controller which DOM element should be removed once the row deletion is successful
= hidden_field_tag 'deleted_row_dom_ids[]', row_dom_id, disabled: true
- row.each do |champ|
= fields_for champ.input_name, champ do |form|
= render partial: 'shared/dossiers/editable_champs/editable_champ', locals: { form: form, champ: champ }
= form.hidden_field :_destroy, disabled: true
.flex.row-reverse
%button.button.danger.remove-row{ type: :button }
Supprimer lélément