Validate messagerie_available? when creating a new Commentaire
Commentaires bu Users and Gestionnaire need the messagerie to be available; Automatic system Commentaires can be created anytime. This reintroduces Commentaire validation that was introduced in #3979 and disabled in #4018
This commit is contained in:
parent
3f439ac07a
commit
930fd345de
2 changed files with 35 additions and 3 deletions
|
@ -7,6 +7,7 @@ class Commentaire < ApplicationRecord
|
|||
mount_uploader :file, CommentaireFileUploader
|
||||
validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" }
|
||||
validate :is_virus_free?
|
||||
validate :messagerie_available?, on: :create
|
||||
validates :body, presence: { message: "Votre message ne peut être vide" }
|
||||
|
||||
default_scope { order(created_at: :asc) }
|
||||
|
@ -78,4 +79,11 @@ class Commentaire < ApplicationRecord
|
|||
errors.add(:file, "Virus détecté dans le fichier joint, merci de changer de fichier")
|
||||
end
|
||||
end
|
||||
|
||||
def messagerie_available?
|
||||
return if sent_by_system?
|
||||
if dossier.present? && !dossier.messagerie_available?
|
||||
errors.add(:dossier, "Il n’est pas possible d’envoyer un message sur un dossier archivé ou en brouillon")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,8 +7,32 @@ describe Commentaire do
|
|||
it { is_expected.to have_db_column(:updated_at) }
|
||||
it { is_expected.to belong_to(:dossier) }
|
||||
|
||||
describe "#is_sent_by_system?" do
|
||||
subject { commentaire.is_sent_by_system? }
|
||||
describe 'messagerie_available validation' do
|
||||
subject { commentaire.valid?(:create) }
|
||||
|
||||
context 'with a commentaire created by the DS system' do
|
||||
let(:commentaire) { build :commentaire, email: CONTACT_EMAIL }
|
||||
|
||||
it { is_expected.to be_truthy }
|
||||
end
|
||||
|
||||
context 'on an archived dossier' do
|
||||
let(:dossier) { create :dossier, :archived }
|
||||
let(:commentaire) { build :commentaire, dossier: dossier }
|
||||
|
||||
it { is_expected.to be_falsey }
|
||||
end
|
||||
|
||||
context 'on a dossier en_construction' do
|
||||
let(:dossier) { create :dossier, :en_construction }
|
||||
let(:commentaire) { build :commentaire, dossier: dossier }
|
||||
|
||||
it { is_expected.to be_truthy }
|
||||
end
|
||||
end
|
||||
|
||||
describe "#sent_by_system?" do
|
||||
subject { commentaire.sent_by_system? }
|
||||
|
||||
let(:commentaire) { build :commentaire, email: email }
|
||||
|
||||
|
@ -42,7 +66,7 @@ describe Commentaire do
|
|||
let(:gestionnaire) { create(:gestionnaire) }
|
||||
let(:assign_to) { create(:assign_to, gestionnaire: gestionnaire, procedure: procedure) }
|
||||
let(:user) { create(:user) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure, user: user) }
|
||||
let(:dossier) { create(:dossier, :en_construction, procedure: procedure, user: user) }
|
||||
let(:commentaire) { Commentaire.new(dossier: dossier, body: "Mon commentaire") }
|
||||
|
||||
context "with a commentaire created by a gestionnaire" do
|
||||
|
|
Loading…
Reference in a new issue