fix(groupe-instructeur): a closed groupe with an active groupe is a valid record

This commit is contained in:
Colin Darie 2023-03-02 15:52:50 +01:00
parent f752dd7085
commit 20d4a8153d
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 19 additions and 1 deletions

View file

@ -22,7 +22,7 @@ class GroupeInstructeur < ApplicationRecord
validates :label, presence: true, allow_nil: false
validates :label, uniqueness: { scope: :procedure }
validates :closed, acceptance: { accept: [false] }, if: -> { self.procedure.groupe_instructeurs.active.one? }
validates :closed, acceptance: { accept: [false] }, if: -> { closed_changed? && self.procedure.groupe_instructeurs.active.one? }
before_validation -> { label&.strip! }
after_save :toggle_routing

View file

@ -98,6 +98,24 @@ describe GroupeInstructeur, type: :model do
end
end
describe "active group validations" do
context "there is at least one active groupe instructeur" do
let!(:gi_active) { create(:groupe_instructeur, procedure:, closed: false) }
let!(:gi_closed) { create(:groupe_instructeur, procedure:, closed: true) }
before { procedure.defaut_groupe_instructeur.destroy! }
it "closed is valid when there is one other active groupe" do
expect(gi_active).to be_valid
expect(gi_closed).to be_valid
end
it "closed is invalid when there is no active groupe" do
gi_active.closed = true
expect(gi_active).not_to be_valid
end
end
end
private
def assign(procedure_to_assign, instructeur_assigne: instructeur)