From f9a8fb4adaab106d541791d83839dbe03a3802c5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 27 Oct 2021 16:26:07 +0200 Subject: [PATCH 1/2] fix(repetition): prevent rage clicks on add row button --- app/views/shared/dossiers/editable_champs/_repetition.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/dossiers/editable_champs/_repetition.html.haml b/app/views/shared/dossiers/editable_champs/_repetition.html.haml index 64ff2c084..2f694ce63 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition.html.haml @@ -18,7 +18,7 @@ Supprimer l’élément - if champ.persisted? - = link_to champs_repetition_path(form.index), class: 'button add-row', data: { remote: true, method: 'POST', params: { champ_id: champ&.id }.to_query } do + = 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 %span.icon.add Ajouter un élément pour « #{champ.libelle} » - else From da49a6447f32e4a8af74cdd5b6127f4d1188f685 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 27 Oct 2021 16:27:17 +0200 Subject: [PATCH 2/2] refactor(repetition): optimize and add a transaction around repetition add row --- app/controllers/champs/repetition_controller.rb | 7 ++----- app/models/champs/repetition_champ.rb | 9 ++++++--- spec/models/concern/tags_substitution_concern_spec.rb | 2 +- spec/models/dossier_spec.rb | 6 +++++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/controllers/champs/repetition_controller.rb b/app/controllers/champs/repetition_controller.rb index ab0596abb..33b817836 100644 --- a/app/controllers/champs/repetition_controller.rb +++ b/app/controllers/champs/repetition_controller.rb @@ -2,12 +2,9 @@ class Champs::RepetitionController < ApplicationController before_action :authenticate_logged_user! def show - @champ = policy_scope(Champ).find(params[:champ_id]) - + @champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) @position = params[:position] - row = (@champ.champs.empty? ? 0 : @champ.champs.last.row) + 1 - - @champ.add_row(row) + @champ.add_row if @champ.private? @attribute = "dossier[champs_private_attributes][#{@position}][champs_attributes]" diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 75451686b..3390bb1ec 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -26,9 +26,12 @@ class Champs::RepetitionChamp < Champ champs.group_by(&:row).values end - def add_row(row = 0) - type_de_champ.types_de_champ.each do |type_de_champ| - self.champs << type_de_champ.champ.build(row: row) + def add_row + 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) + end end end diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index 8ab94ca3e..cf96ef3a1 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -173,7 +173,7 @@ describe TagsSubstitutionConcern, type: :model do before do repetition = dossier.champs .find { |champ| champ.libelle == 'Répétition' } - repetition.add_row(1) + repetition.add_row paul_champs, pierre_champs = repetition.rows paul_champs.first.update(value: 'Paul') diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 7e6238bbc..426ab34b9 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1497,7 +1497,11 @@ describe Dossier do datetime_champ.update(value: Date.today.to_s) text_champ.update(value: 'bonjour') - repetition_champ.add_row(repetition_champ.champs.last.row + 2) + # Add two rows then remove previous to last row in order to create a "hole" in the sequence + repetition_champ.add_row + repetition_champ.add_row + repetition_champ.champs.where(row: repetition_champ.champs.last.row - 1).destroy_all + repetition_champ.reload end it "updates the brouillon champs with the latest revision changes" do