diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index a391e9272..446b2096e 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -11,6 +11,7 @@ class AttestationTemplate < ApplicationRecord validates :footer, length: { maximum: 190 } FILE_MAX_SIZE_IN_MB = 0.5 + DOSSIER_STATE = 'accepte' def attestation_for(dossier) Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier)) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index 17fdd5246..1e4f9ad98 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -13,10 +13,6 @@ module MailTemplateConcern replace_tags(body, dossier) end - def tags(is_dossier_termine: self.class.const_get(:IS_DOSSIER_TERMINE)) - super - end - module ClassMethods def default_for_procedure(procedure) body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME)) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index 6dcfc25ce..bfc6b6539 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -1,25 +1,33 @@ module TagsSubstitutionConcern extend ActiveSupport::Concern - def tags(is_dossier_termine: true) + def tags if procedure.for_individual? identity_tags = individual_tags else identity_tags = entreprise_tags + etablissement_tags end - tags = identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags - filter_tags(tags, is_dossier_termine) + filter_tags(identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags) end private - def filter_tags(tags, is_dossier_termine) - if !is_dossier_termine - tags.reject { |tag| tag[:dossier_termine_only] } - else - tags + def filter_tags(tags) + # Implementation note: emails and attestation generations are generally + # triggerred by changes to the dossier’s state. The email or attestation + # is generated right after the dossier has reached its new state. + # + # DOSSIER_STATE should be equal to this new state. + # + # For instance, for an email that gets generated for the brouillon->en_construction + # transition, DOSSIER_STATE should equal 'en_construction'. + + if !defined?(self.class::DOSSIER_STATE) + raise NameError.new("The class #{self.class.name} includes TagsSubstitutionConcern, it should define the DOSSIER_STATE constant but it does not", :DOSSIER_STATE) end + + tags.select { |tag| (tag[:available_for_states] || Dossier::SOUMIS).include?(self.class::DOSSIER_STATE) } end def procedure_type_de_champ_public_private_tags @@ -33,7 +41,7 @@ module TagsSubstitutionConcern libelle: 'motivation', description: 'Motivation facultative associée à la décision finale d’acceptation, refus ou classement sans suite', target: :motivation, - dossier_termine_only: true + available_for_states: Dossier::TERMINE }, { libelle: 'date de dépôt', @@ -49,7 +57,7 @@ module TagsSubstitutionConcern libelle: 'date de décision', description: 'Date de la décision d’acceptation, refus, ou classement sans suite', lambda: -> (d) { format_date(d.processed_at) }, - dossier_termine_only: true + available_for_states: Dossier::TERMINE }, { libelle: 'libellé procédure', description: '', lambda: -> (d) { d.procedure.libelle } }, { libelle: 'numéro du dossier', description: '', target: :id } @@ -101,7 +109,7 @@ module TagsSubstitutionConcern ] tags_and_datas - .map { |(tags, data)| [filter_tags(tags, dossier.termine?), data] } + .map { |(tags, data)| [filter_tags(tags), data] } .inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) } end diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb index cf7231836..97f0f1985 100644 --- a/app/models/mails/closed_mail.rb +++ b/app/models/mails/closed_mail.rb @@ -8,6 +8,6 @@ module Mails TEMPLATE_NAME = "mails/closed_mail" DISPLAYED_NAME = "Accusé d'acceptation" DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été accepté' - IS_DOSSIER_TERMINE = true + DOSSIER_STATE = 'accepte' end end diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb index c9ca384ac..d4a2cbe0f 100644 --- a/app/models/mails/initiated_mail.rb +++ b/app/models/mails/initiated_mail.rb @@ -8,6 +8,6 @@ module Mails TEMPLATE_NAME = "mails/initiated_mail" DISPLAYED_NAME = 'Accusé de réception' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a bien été reçu' - IS_DOSSIER_TERMINE = false + DOSSIER_STATE = 'en_construction' end end diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb index 30f58de9a..ef903b388 100644 --- a/app/models/mails/received_mail.rb +++ b/app/models/mails/received_mail.rb @@ -8,6 +8,6 @@ module Mails TEMPLATE_NAME = "mails/received_mail" DISPLAYED_NAME = 'Accusé de passage en instruction' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- va être instruit' - IS_DOSSIER_TERMINE = false + DOSSIER_STATE = 'en_instruction' end end diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index 4e0d818c6..a133e6534 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -8,6 +8,6 @@ module Mails TEMPLATE_NAME = "mails/refused_mail" DISPLAYED_NAME = 'Accusé de rejet du dossier' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été refusé' - IS_DOSSIER_TERMINE = true + DOSSIER_STATE = 'refuse' end end diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb index 2c1e2f8b2..142db0982 100644 --- a/app/models/mails/without_continuation_mail.rb +++ b/app/models/mails/without_continuation_mail.rb @@ -8,6 +8,6 @@ module Mails TEMPLATE_NAME = "mails/without_continuation_mail" DISPLAYED_NAME = 'Accusé de classement sans suite' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été classé sans suite' - IS_DOSSIER_TERMINE = true + DOSSIER_STATE = 'sans_suite' end end diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index 911fa29de..a05b30590 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -2,6 +2,7 @@ describe TagsSubstitutionConcern, type: :model do let(:types_de_champ) { [] } let(:types_de_champ_private) { [] } let(:for_individual) { false } + let(:state) { 'accepte' } let(:procedure) do create(:procedure, @@ -13,17 +14,18 @@ describe TagsSubstitutionConcern, type: :model do let(:template_concern) do (Class.new do - include TagsSubstitutionConcern - public :replace_tags + include TagsSubstitutionConcern + public :replace_tags - def initialize(p) - @procedure = p - end + def initialize(p, s) + @procedure = p + self.class.const_set(:DOSSIER_STATE, s) + end - def procedure - @procedure - end - end).new(procedure) + def procedure + @procedure + end + end).new(procedure, state) end describe 'replace_tags' do @@ -119,8 +121,6 @@ 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--' } @@ -173,7 +173,6 @@ describe TagsSubstitutionConcern, type: :model do context "when using a date tag" do before do - dossier.accepte! dossier.en_construction_at = DateTime.new(2001, 2, 3) dossier.en_instruction_at = DateTime.new(2004, 5, 6) dossier.processed_at = DateTime.new(2007, 8, 9) @@ -237,6 +236,7 @@ describe TagsSubstitutionConcern, type: :model do context 'when generating a document for a dossier that is not termine' do let(:dossier) { create(:dossier) } let(:template) { '--motivation-- --date de décision--' } + let(:state) { 'en_instruction' } subject { template_concern.replace_tags(template, dossier) } @@ -247,15 +247,15 @@ describe TagsSubstitutionConcern, type: :model do end describe 'tags' do - context 'when generating a document for a dossier terminé' do - subject { template_concern.tags } + subject { template_concern.tags } + context 'when generating a document for a dossier terminé' do 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) } + let(:state) { 'en_instruction' } it { is_expected.not_to include(include({ libelle: 'motivation' })) } it { is_expected.not_to include(include({ libelle: 'date de décision' })) }