[#1946] Add validation to durees conservation

This commit is contained in:
Frederic Merizen 2018-05-24 23:29:33 +02:00
parent d3cfcaa8ac
commit 42765efb91
5 changed files with 44 additions and 0 deletions

View file

@ -1,4 +1,6 @@
class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36
has_many :types_de_piece_justificative, -> { order "order_place ASC" }, dependent: :destroy
has_many :types_de_champ, -> { public_only }, dependent: :destroy
has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy
@ -47,6 +49,11 @@ class Procedure < ApplicationRecord
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false
validate :check_juridique
# FIXME: remove duree_conservation_required flag once all procedures are converted to the new style
validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, if: :durees_conservation_required
validates :duree_conservation_dossiers_hors_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :durees_conservation_required
validates :duree_conservation_dossiers_dans_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, unless: :durees_conservation_required
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

View file

@ -88,6 +88,7 @@ fr:
errors:
messages:
blank: "doit être rempli"
not_a_number: 'doit être un nombre'
models:
attestation_template:
attributes:

View file

@ -3,3 +3,5 @@ fr:
attributes:
procedure:
organisation: Organisme
duree_conservation_dossiers_dans_ds: Durée de conservation des dossiers sur demarches-simplifiees.fr
duree_conservation_dossiers_hors_ds: Durée de conservation des dossiers hors demarches-simplifiees.fr

View file

@ -10,6 +10,8 @@ FactoryBot.define do
published_at nil
cerfa_flag false
administrateur { create(:administrateur) }
duree_conservation_dossiers_dans_ds 3
duree_conservation_dossiers_hors_ds 6
after(:build) do |procedure, _evaluator|
if procedure.module_api_carto.nil?

View file

@ -199,6 +199,38 @@ describe Procedure do
it { expect(procedure.valid?).to eq(true) }
end
shared_examples 'duree de conservation' do
context 'duree_conservation_required it true, the field gets validated' do
before { subject.durees_conservation_required = true }
it { is_expected.not_to allow_value(nil).for(field_name) }
it { is_expected.not_to allow_value('').for(field_name) }
it { is_expected.not_to allow_value('trois').for(field_name) }
it { is_expected.to allow_value(3).for(field_name) }
end
context 'duree_conservation_required is false, the field doesnt get validated' do
before { subject.durees_conservation_required = false }
it { is_expected.to allow_value(nil).for(field_name) }
it { is_expected.to allow_value('').for(field_name) }
it { is_expected.not_to allow_value('trois').for(field_name) }
it { is_expected.to allow_value(3).for(field_name) }
end
end
describe 'duree de conservation dans ds' do
let(:field_name) { :duree_conservation_dossiers_dans_ds }
it_behaves_like 'duree de conservation'
end
describe 'duree de conservation hors ds' do
let(:field_name) { :duree_conservation_dossiers_hors_ds }
it_behaves_like 'duree de conservation'
end
end
describe '#types_de_champ_ordered' do