From 72e27eb7b48a5cf7cba435740099bc4ef0da253f Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 11 Mar 2024 09:17:03 +0100 Subject: [PATCH] fix(dossier): safely remove child champs in apply_diff --- app/models/concerns/dossier_clone_concern.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/dossier_clone_concern.rb b/app/models/concerns/dossier_clone_concern.rb index 000e6a6e5..06283d891 100644 --- a/app/models/concerns/dossier_clone_concern.rb +++ b/app/models/concerns/dossier_clone_concern.rb @@ -167,11 +167,12 @@ module DossierCloneConcern end champs_to_remove += diff[:removed] - champs_to_remove - .filter { !_1.child? || !champs_to_remove.include?(_1.parent) } - .each do |champ| - champ.rows.flatten.each(&:destroy!) if champ.repetition? - champ.destroy! - end + children_champs_to_remove, root_champs_to_remove = champs_to_remove.partition(&:child?) + + children_champs_to_remove.each(&:destroy!) + root_champs_to_remove.each do |champ| + champ.rows.flatten.each(&:destroy!) if champ.repetition? + champ.destroy! + end end end