diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index 8431ef00b..458537d4c 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -6,24 +6,24 @@ module MailTemplateConcern TAGS = [] TAGS << TAG_NUMERO_DOSSIER = { - name: "numero_dossier", - description: "Permet d'afficher le numéro de dossier de l'utilisateur." + libelle: "numero_dossier", + description: "Permet d'afficher le numéro de dossier de l'utilisateur." } TAGS << TAG_LIEN_DOSSIER = { - name: "lien_dossier", - description: "Permet d'afficher un lien vers le dossier de l'utilisateur." + libelle: "lien_dossier", + description: "Permet d'afficher un lien vers le dossier de l'utilisateur." } TAGS << TAG_LIBELLE_PROCEDURE = { - name: "libelle_procedure", - description: "Permet d'afficher le libellé de la procédure." + libelle: "libelle_procedure", + description: "Permet d'afficher le libellé de la procédure." } TAGS << TAG_DATE_DE_DECISION = { - name: "date_de_decision", - description: "Permet d'afficher la date à laquelle la décision finale (acceptation, refus, classement sans suite) sur le dossier a été prise." + libelle: "date_de_decision", + description: "Permet d'afficher la date à laquelle la décision finale (acceptation, refus, classement sans suite) sur le dossier a été prise." } TAGS << TAG_MOTIVATION = { - name: "motivation", - description: "Permet d'afficher la motivation associée à la décision finale (acceptation, refus, classement sans suite) sur le dossier. Attention, elle est facultative." + libelle: "motivation", + description: "Permet d'afficher la motivation associée à la décision finale (acceptation, refus, classement sans suite) sur le dossier. Attention, elle est facultative." } def object_for_dossier(dossier) @@ -34,16 +34,20 @@ module MailTemplateConcern replace_tags(body, dossier) end + def tags + self.class.const_get(:ALLOWED_TAGS) + end + def replace_tags(string, dossier) TAGS.inject(string) do |acc, tag| - acc.gsub("--#{tag[:name]}--", replace_tag(tag, dossier)) || acc + acc.gsub("--#{tag[:libelle]}--", replace_tag(tag, dossier)) || acc end end module ClassMethods - def default + def default_for_procedure(procedure) body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME)) - self.new(object: self.const_get(:DEFAULT_OBJECT), body: body) + self.new(object: self.const_get(:DEFAULT_OBJECT), body: body, procedure: procedure) end end diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index d0da15797..932276b1d 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -1,18 +1,27 @@ module TagsSubstitutionConcern extend ActiveSupport::Concern - def tags + def tags(is_dossier_termine: true) if procedure.for_individual? identity_tags = individual_tags else identity_tags = entreprise_tags + etablissement_tags end - identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags + tags = identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags + filter_tags(tags, is_dossier_termine) end private + def filter_tags(tags, is_dossier_termine) + if !is_dossier_termine + tags.reject { |tag| tag[:dossier_termine_only] } + else + tags + end + end + def procedure_type_de_champ_public_private_tags (procedure.types_de_champ + procedure.types_de_champ_private) .map { |tdc| { libelle: tdc.libelle, description: tdc.description } } @@ -21,10 +30,12 @@ module TagsSubstitutionConcern def dossier_tags [{ libelle: 'motivation', description: 'Motivation facultative associée à la décision finale d’acceptation, refus ou classement sans suite', - target: :motivation }, + target: :motivation, + dossier_termine_only: true }, { libelle: 'date de décision', description: 'Date de la décision d’acceptation, refus, ou classement sans suite', - lambda: -> (d) { d.processed_at.present? ? d.processed_at.localtime.strftime('%d/%m/%Y') : '' } }, + lambda: -> (d) { d.processed_at.present? ? d.processed_at.localtime.strftime('%d/%m/%Y') : '' }, + dossier_termine_only: true }, { libelle: 'libellé procédure', description: '', lambda: -> (d) { d.procedure.libelle } }, { libelle: 'numéro du dossier', description: '', target: :id }] end @@ -60,8 +71,10 @@ module TagsSubstitutionConcern [entreprise_tags, dossier.entreprise], [etablissement_tags, dossier.entreprise&.etablissement]] - tags_and_datas.inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) } - end + tags_and_datas + .map { |(tags, data)| [filter_tags(tags, dossier.termine?), data] } + .inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) } + end def replace_type_de_champ_tags(text, types_de_champ, dossier_champs) types_de_champ.inject(text) do |acc, tag| diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb index cef3a274f..b01cb416b 100644 --- a/app/models/mails/closed_mail.rb +++ b/app/models/mails/closed_mail.rb @@ -2,6 +2,8 @@ module Mails class ClosedMail < ApplicationRecord include MailTemplateConcern + belongs_to :procedure + SLUG = "closed_mail" TEMPLATE_NAME = "mails/closed_mail" DISPLAYED_NAME = "Accusé d'acceptation" diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb index 7ffa4082e..ffb09f018 100644 --- a/app/models/mails/initiated_mail.rb +++ b/app/models/mails/initiated_mail.rb @@ -2,6 +2,8 @@ module Mails class InitiatedMail < ApplicationRecord include MailTemplateConcern + belongs_to :procedure + SLUG = "initiated_mail" TEMPLATE_NAME = "mails/initiated_mail" DISPLAYED_NAME = 'Accusé de réception' diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb index 27175df37..09bd9aaf7 100644 --- a/app/models/mails/received_mail.rb +++ b/app/models/mails/received_mail.rb @@ -2,6 +2,8 @@ module Mails class ReceivedMail < ApplicationRecord include MailTemplateConcern + belongs_to :procedure + SLUG = "received_mail" TEMPLATE_NAME = "mails/received_mail" DISPLAYED_NAME = 'Accusé de passage en instruction' diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index 8d0b9d032..cde52960b 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -2,6 +2,8 @@ module Mails class RefusedMail < ApplicationRecord include MailTemplateConcern + belongs_to :procedure + SLUG = "refused_mail" TEMPLATE_NAME = "mails/refused_mail" DISPLAYED_NAME = 'Accusé de rejet du dossier' diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb index 7e615ca75..b9a97ceee 100644 --- a/app/models/mails/without_continuation_mail.rb +++ b/app/models/mails/without_continuation_mail.rb @@ -2,6 +2,8 @@ module Mails class WithoutContinuationMail < ApplicationRecord include MailTemplateConcern + belongs_to :procedure + SLUG = "without_continuation" TEMPLATE_NAME = "mails/without_continuation_mail" DISPLAYED_NAME = 'Accusé de classement sans suite' diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 40f3bf7ce..8639b7924 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -167,23 +167,23 @@ class Procedure < ActiveRecord::Base end def initiated_mail_template - initiated_mail || Mails::InitiatedMail.default + initiated_mail || Mails::InitiatedMail.default_for_procedure(self) end def received_mail_template - received_mail || Mails::ReceivedMail.default + received_mail || Mails::ReceivedMail.default_for_procedure(self) end def closed_mail_template - closed_mail || Mails::ClosedMail.default + closed_mail || Mails::ClosedMail.default_for_procedure(self) end def refused_mail_template - refused_mail || Mails::RefusedMail.default + refused_mail || Mails::RefusedMail.default_for_procedure(self) end def without_continuation_mail_template - without_continuation_mail || Mails::WithoutContinuationMail.default + without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self) end def fields diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index af010fb95..0e5718ce9 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -22,9 +22,9 @@ Balise %th Description - - @mail_template.class.const_get(:ALLOWED_TAGS).each do |tag| + - @mail_template.tags.each do |tag| %tr %td.center - = "--#{tag[:name]}--" + = "--#{tag[:libelle]}--" %td = tag[:description] diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index 09db84650..6368da749 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' describe Admin::MailTemplatesController, type: :controller do - let(:initiated_mail) { Mails::InitiatedMail.default } let(:procedure) { create :procedure } + let(:initiated_mail) { Mails::InitiatedMail.default_for_procedure(procedure) } before do sign_in procedure.administrateur diff --git a/spec/models/concern/mail_template_concern_spec.rb b/spec/models/concern/mail_template_concern_spec.rb index e249b73be..4bc7f366b 100644 --- a/spec/models/concern/mail_template_concern_spec.rb +++ b/spec/models/concern/mail_template_concern_spec.rb @@ -1,9 +1,10 @@ require 'spec_helper' describe MailTemplateConcern do - let(:dossier) { create :dossier } - let(:dossier2) { create :dossier } - let(:initiated_mail) { Mails::InitiatedMail.default } + let(:procedure) { create(:procedure)} + let(:dossier) { create(:dossier, procedure: procedure) } + let(:dossier2) { create(:dossier, procedure: procedure) } + let(:initiated_mail) { Mails::InitiatedMail.default_for_procedure(procedure) } shared_examples "can replace tokens in template" do describe 'with no token to replace' do diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index 51e512734..99c3aeea1 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -117,6 +117,8 @@ describe TagsSubstitutionConcern, type: :model do context 'when the dossier has a motivation' do let(:dossier) { create(:dossier, motivation: 'motivation') } + before { dossier.accepte! } + context 'and the template has some dossier tags' do let(:template) { '--motivation-- --numéro du dossier--' } @@ -208,11 +210,32 @@ describe TagsSubstitutionConcern, type: :model do it_behaves_like "treat all kinds of space as equivalent" end end + + context 'when generating a document for a dossier that is not termine' do + let(:dossier) { create(:dossier) } + let(:template) { '--motivation-- --date de décision--' } + + subject { template_concern.replace_tags(template, dossier) } + + it "does not treat motivation or date de décision as tags" do + is_expected.to eq('--motivation-- --date de décision--') + end + end end describe 'tags' do - subject { template_concern.tags } + context 'when generating a document for a dossier terminé' do + subject { template_concern.tags } - it { is_expected.to include(include({ libelle: 'date de décision' })) } + it { is_expected.to include(include({ libelle: 'motivation' })) } + it { is_expected.to include(include({ libelle: 'date de décision' })) } + end + + context 'when generating a document for a dossier that is not terminé' do + subject { template_concern.tags(is_dossier_termine: false) } + + it { is_expected.not_to include(include({ libelle: 'motivation' })) } + it { is_expected.not_to include(include({ libelle: 'date de décision' })) } + end end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 4b19cafa0..0bed281ef 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -12,10 +12,12 @@ describe Procedure do end describe 'initiated_mail' do - subject { create(:procedure) } + let(:procedure) { create(:procedure) } + + subject { procedure } context 'when initiated_mail is not customize' do - it { expect(subject.initiated_mail_template.body).to eq(Mails::InitiatedMail.default.body) } + it { expect(subject.initiated_mail_template.body).to eq(Mails::InitiatedMail.default_for_procedure(procedure).body) } end context 'when initiated_mail is customize' do @@ -209,7 +211,7 @@ describe Procedure do end it 'should not duplicate default mail_template' do - expect(subject.initiated_mail_template.attributes).to eq Mails::InitiatedMail.default.attributes + expect(subject.initiated_mail_template.attributes).to eq Mails::InitiatedMail.default_for_procedure(subject).attributes end it 'should not duplicate specific related objects' do