From 346687bd33a7f5402167fed8f4571676515cbbb4 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Fri, 25 May 2018 00:16:13 +0200 Subject: [PATCH] [#1946] Once durees de conservation have been set, forbid unsetting them --- app/models/procedure.rb | 6 ++++++ spec/models/procedure_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 5d153ab9f..8a0e216a0 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -56,6 +56,7 @@ class Procedure < ApplicationRecord validates :duree_conservation_dossiers_hors_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, unless: :durees_conservation_required before_save :update_juridique_required + before_save :update_durees_conservation_required include AASM @@ -383,4 +384,9 @@ class Procedure < ApplicationRecord 'column' => column } end + + def update_durees_conservation_required + self.durees_conservation_required ||= duree_conservation_dossiers_hors_ds.present? && duree_conservation_dossiers_dans_ds.present? + true + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 38c36fb2b..7ff08b45c 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -233,6 +233,31 @@ describe Procedure do end end + describe '#duree_de_conservation_required' do + it 'automatically jumps to true once both durees de conservation have been set' do + p = build( + :procedure, + durees_conservation_required: false, + duree_conservation_dossiers_dans_ds: nil, + duree_conservation_dossiers_hors_ds: nil + ) + p.save + expect(p.durees_conservation_required).to be_falsey + + p.duree_conservation_dossiers_hors_ds = 3 + p.save + expect(p.durees_conservation_required).to be_falsey + + p.duree_conservation_dossiers_dans_ds = 6 + p.save + expect(p.durees_conservation_required).to be_truthy + + p.duree_conservation_dossiers_dans_ds = nil + p.save + expect(p.durees_conservation_required).to be_truthy + end + end + describe '#types_de_champ_ordered' do let(:procedure) { create(:procedure) } let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 1) }