Merge pull request #10959 from thibpoullain/fix/procedure_minimal_admin

🛠️ Fix | Procedure minimal admins presence
This commit is contained in:
LeSim 2024-10-22 15:11:34 +00:00 committed by GitHub
commit f04e052dc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View file

@ -119,7 +119,7 @@ class Procedure < ApplicationRecord
end
has_many :administrateurs_procedures, dependent: :delete_all
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
has_many :administrateurs, through: :administrateurs_procedures, before_remove: :check_administrateur_minimal_presence
has_many :groupe_instructeurs, -> { order(:label) }, inverse_of: :procedure, dependent: :destroy
has_many :instructeurs, through: :groupe_instructeurs
has_many :export_templates, through: :groupe_instructeurs
@ -318,6 +318,12 @@ class Procedure < ApplicationRecord
end
end
def check_administrateur_minimal_presence(_object)
if self.administrateurs.count <= 1
raise ActiveRecord::RecordNotDestroyed.new("Cannot remove the last administrateur of procedure #{self.libelle} (#{self.id})")
end
end
def dossiers_close_to_expiration
dossiers.close_to_expiration.count
end

View file

@ -210,6 +210,20 @@ describe Procedure do
it { is_expected.not_to allow_value([]).for(:administrateurs) }
end
context 'before_remove callback for minimal administrator presence' do
let(:procedure) { create(:procedure) }
it 'raises an error when trying to remove the last administrateur' do
expect(procedure.administrateurs.count).to eq(1)
expect {
procedure.administrateurs.destroy(procedure.administrateurs.first)
}.to raise_error(
ActiveRecord::RecordNotDestroyed,
"Cannot remove the last administrateur of procedure #{procedure.libelle} (#{procedure.id})"
)
end
end
context 'juridique' do
it { is_expected.not_to allow_value(nil).on(:publication).for(:cadre_juridique) }
it { is_expected.to allow_value('text').on(:publication).for(:cadre_juridique) }