[#1203] Allow filtering tags based on an arbitray status
This commit is contained in:
parent
1282fc2113
commit
9f7ffddb31
9 changed files with 40 additions and 35 deletions
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
@ -16,14 +17,15 @@ describe TagsSubstitutionConcern, type: :model do
|
|||
include TagsSubstitutionConcern
|
||||
public :replace_tags
|
||||
|
||||
def initialize(p)
|
||||
def initialize(p, s)
|
||||
@procedure = p
|
||||
self.class.const_set(:DOSSIER_STATE, s)
|
||||
end
|
||||
|
||||
def procedure
|
||||
@procedure
|
||||
end
|
||||
end).new(procedure)
|
||||
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 }
|
||||
|
||||
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' })) }
|
||||
|
|
Loading…
Reference in a new issue