diff --git a/app/models/concerns/dossier_clone_concern.rb b/app/models/concerns/dossier_clone_concern.rb index 8418f8ed4..b5987cdec 100644 --- a/app/models/concerns/dossier_clone_concern.rb +++ b/app/models/concerns/dossier_clone_concern.rb @@ -145,11 +145,11 @@ module DossierCloneConcern end def apply_diff(diff) - champs_index = (champs + diff[:added]).index_by(&:stable_id_with_row) + champs_index = (champs_public_all + diff[:added]).index_by(&:stable_id_with_row) diff[:added].each do |champ| if champ.child? - champ.update_columns(dossier_id: id, parent_id: champs_index[champ.parent.stable_id_with_row].id) + champ.update_columns(dossier_id: id, parent_id: champs_index.fetch(champ.parent.stable_id_with_row).id) else champ.update_column(:dossier_id, id) end @@ -157,13 +157,13 @@ module DossierCloneConcern champs_to_remove = [] diff[:updated].each do |champ| - old_champ = champs_index[champ.stable_id_with_row] + old_champ = champs_index.fetch(champ.stable_id_with_row) champs_to_remove << old_champ if champ.child? # we need to do that in order to avoid a foreign key constraint old_champ.update(row_id: nil) - champ.update_columns(dossier_id: id, parent_id: champs_index[champ.parent.stable_id_with_row].id) + champ.update_columns(dossier_id: id, parent_id: champs_index.fetch(champ.parent.stable_id_with_row).id) else champ.update_column(:dossier_id, id) end diff --git a/app/models/concerns/dossier_rebase_concern.rb b/app/models/concerns/dossier_rebase_concern.rb index e947656dc..90c0909a0 100644 --- a/app/models/concerns/dossier_rebase_concern.rb +++ b/app/models/concerns/dossier_rebase_concern.rb @@ -129,10 +129,10 @@ module DossierRebaseConcern champs.filter { _1.stable_id == parent_stable_id }.each do |champ_repetition| if champ_repetition.champs.present? champ_repetition.champs.map(&:row_id).uniq.each do |row_id| - create_champ(target_coordinate, champ_repetition, row_id:) + champs << create_champ(target_coordinate, champ_repetition, row_id:) end elsif champ_repetition.mandatory? - create_champ(target_coordinate, champ_repetition, row_id: ULID.generate) + champs << create_champ(target_coordinate, champ_repetition, row_id: ULID.generate) end end else @@ -141,10 +141,10 @@ module DossierRebaseConcern end def create_champ(target_coordinate, parent, row_id: nil) - champ = target_coordinate + target_coordinate .type_de_champ .build_champ(rebased_at: Time.zone.now, row_id:) - parent.champs << champ + .tap { parent.champs << _1 } end def purge_piece_justificative_file(champ) diff --git a/spec/models/concern/dossier_clone_concern_spec.rb b/spec/models/concern/dossier_clone_concern_spec.rb index e1e4f6a8b..6a28316d3 100644 --- a/spec/models/concern/dossier_clone_concern_spec.rb +++ b/spec/models/concern/dossier_clone_concern_spec.rb @@ -295,6 +295,7 @@ RSpec.describe DossierCloneConcern do context 'with new revision' do let(:added_champ) { forked_dossier.champs.find { _1.libelle == "Un nouveau champ text" } } + let(:added_repetition_champ) { forked_dossier.champs.find { _1.libelle == "Texte en répétition" } } let(:removed_champ) { dossier.champs.find { _1.stable_id == 99 } } let(:updated_champ) { dossier.champs.find { _1.stable_id == 991 } } @@ -306,6 +307,11 @@ RSpec.describe DossierCloneConcern do type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: "Un nouveau champ text" }) + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:text), + parent_stable_id: 993, + libelle: "Texte en répétition" + }) procedure.draft_revision.remove_type_de_champ(removed_champ.stable_id) procedure.draft_revision.find_and_ensure_exclusive_use(updated_champ.stable_id).update(libelle: "Un nouveau libelle") procedure.publish_revision! @@ -314,13 +320,14 @@ RSpec.describe DossierCloneConcern do subject { added_champ.update(value: 'new value for added champ') updated_champ.update(value: 'new value for updated champ') + added_repetition_champ.update(value: "new value in repetition champ") dossier.reload super() dossier.reload } - it { expect { subject }.to change { dossier.reload.champs.size }.by(0) } - it { expect { subject }.to change { dossier.reload.champs.order(:created_at).map(&:to_s) }.from(['old value', 'old value', 'Non', 'old value', 'old value']).to(['new value for updated champ', 'Non', 'old value', 'old value', 'new value for added champ']) } + it { expect { subject }.to change { dossier.reload.champs.size }.by(1) } + it { expect { subject }.to change { dossier.reload.champs.order(:created_at).map(&:to_s) }.from(['old value', 'old value', 'Non', 'old value', 'old value']).to(['new value for updated champ', 'Non', 'old value', 'old value', 'new value for added champ', 'new value in repetition champ']) } it "dossier after merge should be on last published revision" do expect(dossier.revision_id).to eq(procedure.revisions.first.id)