Use has_one instead of scope

This commit is contained in:
Mathieu Magnin 2017-02-06 16:52:57 +01:00
parent b40193e7b5
commit 6444aa5be6
3 changed files with 11 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -46,6 +46,7 @@ describe Procedure do
before do
subject.save
subject.reload
end
it { expect(subject.mail_templates.size).to eq 2 }