Merge pull request #6594 from tchak/fix-repetition-add-row-button

Fix repetition add row button
This commit is contained in:
Paul Chavard 2021-10-28 15:03:07 +02:00 committed by GitHub
commit 199f128a49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 11 deletions

View file

@ -2,12 +2,9 @@ class Champs::RepetitionController < ApplicationController
before_action :authenticate_logged_user! before_action :authenticate_logged_user!
def show def show
@champ = policy_scope(Champ).find(params[:champ_id]) @champ = policy_scope(Champ).includes(:champs).find(params[:champ_id])
@position = params[:position] @position = params[:position]
row = (@champ.champs.empty? ? 0 : @champ.champs.last.row) + 1 @champ.add_row
@champ.add_row(row)
if @champ.private? if @champ.private?
@attribute = "dossier[champs_private_attributes][#{@position}][champs_attributes]" @attribute = "dossier[champs_private_attributes][#{@position}][champs_attributes]"

View file

@ -26,9 +26,12 @@ class Champs::RepetitionChamp < Champ
champs.group_by(&:row).values champs.group_by(&:row).values
end end
def add_row(row = 0) def add_row
type_de_champ.types_de_champ.each do |type_de_champ| transaction do
self.champs << type_de_champ.champ.build(row: row) 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
end end

View file

@ -18,7 +18,7 @@
Supprimer lélément Supprimer lélément
- if champ.persisted? - 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 %span.icon.add
Ajouter un élément pour « #{champ.libelle} » Ajouter un élément pour « #{champ.libelle} »
- else - else

View file

@ -173,7 +173,7 @@ describe TagsSubstitutionConcern, type: :model do
before do before do
repetition = dossier.champs repetition = dossier.champs
.find { |champ| champ.libelle == 'Répétition' } .find { |champ| champ.libelle == 'Répétition' }
repetition.add_row(1) repetition.add_row
paul_champs, pierre_champs = repetition.rows paul_champs, pierre_champs = repetition.rows
paul_champs.first.update(value: 'Paul') paul_champs.first.update(value: 'Paul')

View file

@ -1497,7 +1497,11 @@ describe Dossier do
datetime_champ.update(value: Date.today.to_s) datetime_champ.update(value: Date.today.to_s)
text_champ.update(value: 'bonjour') 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 end
it "updates the brouillon champs with the latest revision changes" do it "updates the brouillon champs with the latest revision changes" do