diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 21a15b543..6f43a774e 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -4,7 +4,8 @@ class Procedure < ActiveRecord::Base has_many :types_de_champ_private, dependent: :destroy has_many :dossiers has_many :mail_templates - + has_one :mail_received + has_one :mail_validated has_one :procedure_path, dependent: :destroy @@ -32,8 +33,8 @@ class Procedure < ActiveRecord::Base after_save :build_default_mails, if: Proc.new { id_changed? } def build_default_mails - mail_templates << MailReceived.create unless mail_received - mail_templates << MailValidated.create unless mail_validated + MailReceived.create(procedure: self) unless mail_received + MailValidated.create(procedure: self) unless mail_validated end def path @@ -111,11 +112,4 @@ class Procedure < ActiveRecord::Base self.dossiers.where.not(state: :draft).size end - def mail_validated - mail_templates.where(type: :MailValidated).first - end - - def mail_received - mail_templates.where(type: :MailReceived).first - end end diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 967843fa7..4acc90c57 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -18,6 +18,8 @@ RSpec.describe NotificationMailer, type: :mailer do subject(:subject) { described_class.dossier_validated(dossier) } + before { dossier.reload } + it { expect(subject.body).to match("Votre dossier N°#{dossier.id} est prêt à être déposé pour instruction.") } it { expect(subject.body).to include("Afin de finaliser son dépôt, merci de vous rendre sur", users_dossier_recapitulatif_url(dossier_id: dossier.id)) } it { expect(subject.subject).to eq("[TPS] Votre dossier TPS N°#{dossier.id} a été validé") } @@ -29,6 +31,8 @@ RSpec.describe NotificationMailer, type: :mailer do subject(:subject) { described_class.dossier_submitted(dossier) } + before { dossier.reload } + it { expect(subject.body).to match("Nous vous confirmons que votre dossier N°#{dossier.id} a été déposé") } it { expect(subject.body).to match("auprès de #{dossier.procedure.organisation} avec succès") } it { expect(subject.body).to match("ce jour à #{dossier.updated_at}.") } @@ -41,6 +45,8 @@ RSpec.describe NotificationMailer, type: :mailer do subject(:subject) { described_class.dossier_received(dossier) } + before { dossier.reload } + it { expect(subject.subject).to eq("[TPS] Accusé de réception pour votre dossier n°#{dossier.id}") } it { expect(subject.body).to match("Votre administration vous confirme la bonne réception de votre dossier n°#{dossier.id}") } end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index e7979514e..5219587ba 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -46,6 +46,7 @@ describe Procedure do before do subject.save + subject.reload end it { expect(subject.mail_templates.size).to eq 2 }