From 903cf0cef5749ad3539fed1f78e9f35c862833cc Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 19 Mar 2020 18:33:38 +0100 Subject: [PATCH] Keep operation logs after dossier removal --- app/models/dossier.rb | 2 +- app/models/dossier_operation_log.rb | 3 ++- spec/models/dossier_spec.rb | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index f0eef0d1d..0dca1c033 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -38,7 +38,7 @@ class Dossier < ApplicationRecord has_many :previous_followers_instructeurs, -> { distinct }, through: :previous_follows, source: :instructeur has_many :avis, inverse_of: :dossier, dependent: :destroy - has_many :dossier_operation_logs, dependent: :destroy + has_many :dossier_operation_logs, dependent: :nullify belongs_to :groupe_instructeur has_one :procedure, through: :groupe_instructeur diff --git a/app/models/dossier_operation_log.rb b/app/models/dossier_operation_log.rb index bfad08b97..f1cd8ce1e 100644 --- a/app/models/dossier_operation_log.rb +++ b/app/models/dossier_operation_log.rb @@ -12,8 +12,9 @@ class DossierOperationLog < ApplicationRecord demander_un_avis: 'demander_un_avis' } - belongs_to :dossier has_one_attached :serialized + + belongs_to :dossier, optional: true belongs_to :bill_signature, optional: true def self.create_and_serialize(params) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index b620ad968..da7c114fd 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1131,4 +1131,17 @@ describe Dossier do end end end + + describe 'dossier_operation_log after dossier deletion' do + let(:dossier) { create(:dossier) } + let(:dossier_operation_log) { create(:dossier_operation_log, dossier: dossier) } + + it 'should nullify dossier link' do + expect(dossier_operation_log.dossier).to eq(dossier) + expect(DossierOperationLog.count).to eq(1) + dossier.destroy + expect(dossier_operation_log.reload.dossier).to be_nil + expect(DossierOperationLog.count).to eq(1) + end + end end