From 633f0d1ce5f5a4c9bc64ae0a593cf4caebafbead Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 18 May 2022 10:51:31 +0200 Subject: [PATCH] remove_repetition --- app/models/procedure_revision.rb | 10 +++++++--- app/models/type_de_champ.rb | 7 +++++-- spec/models/procedure_revision_spec.rb | 2 +- spec/models/type_de_champ_shared_example.rb | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index be1b03bf2..a8ad60d9f 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -157,9 +157,13 @@ class ProcedureRevision < ApplicationRecord def children_of(tdc) parent_revision_type_de_champ = revision_types_de_champ.find_by(type_de_champ: tdc) - types_de_champ - .where(procedure_revision_types_de_champ: { parent_id: parent_revision_type_de_champ.id }) - .order("procedure_revision_types_de_champ.position") + if parent_revision_type_de_champ.present? + types_de_champ + .where(procedure_revision_types_de_champ: { parent_id: parent_revision_type_de_champ.id }) + .order("procedure_revision_types_de_champ.position") + else + TypeDeChamp.none + end end private diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 479658ce4..0d9f26574 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -433,8 +433,11 @@ class TypeDeChamp < ApplicationRecord end def remove_repetition - if !repetition? - types_de_champ.destroy_all + if !repetition? && procedure.present? + procedure + .draft_revision # action occurs only on draft + .children_of(self) + .destroy_all end end end diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 5d756da02..65a5cc21f 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -409,7 +409,7 @@ describe ProcedureRevision do let(:procedure) { create(:procedure, :with_repetition) } before do - child = new_draft.types_de_champ_public.last.types_de_champ.first + child = new_draft.children_of(new_draft.types_de_champ_public.last).first new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :drop_down_list, drop_down_options: ['one', 'two']) end diff --git a/spec/models/type_de_champ_shared_example.rb b/spec/models/type_de_champ_shared_example.rb index 333ec997f..3275cc1b6 100644 --- a/spec/models/type_de_champ_shared_example.rb +++ b/spec/models/type_de_champ_shared_example.rb @@ -101,7 +101,7 @@ shared_examples 'type_de_champ_spec' do let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:text) } it 'removes the children types de champ' do - expect(tdc.types_de_champ).to be_empty + expect(procedure.draft_revision.children_of(tdc)).to be_empty end end end