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!
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]"

View file

@ -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

View file

@ -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

View file

@ -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')

View file

@ -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