From ceaf8ebbe797532734343ccd5b0a117babd22eda Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 7 Oct 2022 14:13:42 +0200 Subject: [PATCH] fix: orphan tdc due to reset_draft --- app/models/procedure.rb | 1 + spec/models/procedure_spec.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index c75d225ee..d2a3a19ba 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -803,6 +803,7 @@ class Procedure < ApplicationRecord if published_revision.present? && draft_changed? transaction do reset! + draft_revision.types_de_champ.filter(&:only_present_on_draft?).each(&:destroy) draft_revision.update(attestation_template: nil, dossier_submitted_message: nil) draft_revision.destroy update!(draft_revision: create_new_revision(published_revision)) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 69063f825..8080cec63 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -908,7 +908,15 @@ describe Procedure do end context "published procedure" do - let(:procedure) { create(:procedure, :published, attestation_template: create(:attestation_template), dossier_submitted_message: create(:dossier_submitted_message)) } + let(:procedure) do + create( + :procedure, + :published, + attestation_template: create(:attestation_template), + dossier_submitted_message: create(:dossier_submitted_message), + types_de_champ_public: [{ type: :text, libelle: 'published tdc' }] + ) + end it "should reset draft revision" do procedure.draft_revision.add_type_de_champ(tdc_attributes) @@ -924,6 +932,16 @@ describe Procedure do expect(procedure.draft_revision.attestation_template).to eq(previous_attestation_template) expect(procedure.draft_revision.dossier_submitted_message).to eq(previous_dossier_submitted_message) end + + it "should erase orphan tdc" do + published_tdc = procedure.published_revision.types_de_champ.first + draft_tdc = procedure.draft_revision.add_type_de_champ(tdc_attributes) + + procedure.reset_draft_revision! + + expect { published_tdc.reload }.not_to raise_error(ActiveRecord::RecordNotFound) + expect { draft_tdc.reload }.to raise_error(ActiveRecord::RecordNotFound) + end end end