From 2d1370d1231c89829fc5fef04c7bd746552cf33e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 28 Apr 2022 15:06:40 +0200 Subject: [PATCH] refactor(champ): simplify repetition champ --- .../champs/repetition_controller.rb | 9 +------- app/models/champs/repetition_champ.rb | 5 ++++- app/views/champs/repetition/_show.html.haml | 16 -------------- app/views/champs/repetition/show.js.erb | 6 +++--- .../editable_champs/_repetition.html.haml | 21 +++---------------- .../editable_champs/_repetition_row.html.haml | 13 ++++++++++++ config/routes.rb | 2 +- spec/system/users/brouillon_spec.rb | 4 ++-- 8 files changed, 27 insertions(+), 49 deletions(-) delete mode 100644 app/views/champs/repetition/_show.html.haml create mode 100644 app/views/shared/dossiers/editable_champs/_repetition_row.html.haml diff --git a/app/controllers/champs/repetition_controller.rb b/app/controllers/champs/repetition_controller.rb index 33b817836..8ed626fb2 100644 --- a/app/controllers/champs/repetition_controller.rb +++ b/app/controllers/champs/repetition_controller.rb @@ -3,13 +3,6 @@ class Champs::RepetitionController < ApplicationController def show @champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) - @position = params[:position] - @champ.add_row - - if @champ.private? - @attribute = "dossier[champs_private_attributes][#{@position}][champs_attributes]" - else - @attribute = "dossier[champs_attributes][#{@position}][champs_attributes]" - end + @champs = @champ.add_row end end diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 25ed1700d..929d8f004 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -28,12 +28,15 @@ class Champs::RepetitionChamp < Champ end def add_row + added_champs = [] transaction do row = (blank? ? -1 : champs.last.row) + 1 type_de_champ.types_de_champ.each do |type_de_champ| - self.champs << type_de_champ.champ.build(row: row) + added_champs << type_de_champ.champ.build(row: row) end + self.champs << added_champs end + added_champs end def blank? diff --git a/app/views/champs/repetition/_show.html.haml b/app/views/champs/repetition/_show.html.haml deleted file mode 100644 index 98ba3f239..000000000 --- a/app/views/champs/repetition/_show.html.haml +++ /dev/null @@ -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 diff --git a/app/views/champs/repetition/show.js.erb b/app/views/champs/repetition/show.js.erb index af552c482..1ac03b11c 100644 --- a/app/views/champs/repetition/show.js.erb +++ b/app/views/champs/repetition/show.js.erb @@ -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 %> diff --git a/app/views/shared/dossiers/editable_champs/_repetition.html.haml b/app/views/shared/dossiers/editable_champs/_repetition.html.haml index 2f694ce63..010289cb0 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition.html.haml @@ -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 diff --git a/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml new file mode 100644 index 000000000..d416a3665 --- /dev/null +++ b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 367445016..b1fcf5bf6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -144,13 +144,13 @@ Rails.application.routes.draw do get ':champ_id/siret', to: 'siret#show', as: :siret get ':champ_id/dossier_link', to: 'dossier_link#show', as: :dossier_link post ':champ_id/carte', to: 'carte#show', as: :carte + post ':champ_id/repetition', to: 'repetition#show', as: :repetition get ':champ_id/carte/features', to: 'carte#index', as: :carte_features post ':champ_id/carte/features', to: 'carte#create' patch ':champ_id/carte/features/:id', to: 'carte#update' delete ':champ_id/carte/features/:id', to: 'carte#destroy' - post ':position/repetition', to: 'repetition#show', as: :repetition put 'piece_justificative/:champ_id', to: 'piece_justificative#update', as: :piece_justificative end diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index d590b43ed..9ceca4599 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -113,7 +113,7 @@ describe 'The user' do click_on 'Ajouter un élément pour' - within '.row-1' do + within '.repetition .row:first-child' do fill_in('sub type de champ', with: 'un autre texte') end @@ -124,7 +124,7 @@ describe 'The user' do expect(page).to have_content('Supprimer', count: 2) - within '.row-1' do + within '.repetition .row:first-child' do click_on 'Supprimer l’élément' end