[#1203] Allow filtering tags based on an arbitray status

This commit is contained in:
Frederic Merizen 2018-01-10 15:03:56 +01:00
parent 1282fc2113
commit 9f7ffddb31
9 changed files with 40 additions and 35 deletions

View file

@ -11,6 +11,7 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE_IN_MB = 0.5 FILE_MAX_SIZE_IN_MB = 0.5
DOSSIER_STATE = 'accepte'
def attestation_for(dossier) def attestation_for(dossier)
Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier)) Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier))

View file

@ -13,10 +13,6 @@ module MailTemplateConcern
replace_tags(body, dossier) replace_tags(body, dossier)
end end
def tags(is_dossier_termine: self.class.const_get(:IS_DOSSIER_TERMINE))
super
end
module ClassMethods module ClassMethods
def default_for_procedure(procedure) def default_for_procedure(procedure)
body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME)) body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME))

View file

@ -1,25 +1,33 @@
module TagsSubstitutionConcern module TagsSubstitutionConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
def tags(is_dossier_termine: true) def tags
if procedure.for_individual? if procedure.for_individual?
identity_tags = individual_tags identity_tags = individual_tags
else else
identity_tags = entreprise_tags + etablissement_tags identity_tags = entreprise_tags + etablissement_tags
end end
tags = identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags filter_tags(identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags)
filter_tags(tags, is_dossier_termine)
end end
private private
def filter_tags(tags, is_dossier_termine) def filter_tags(tags)
if !is_dossier_termine # Implementation note: emails and attestation generations are generally
tags.reject { |tag| tag[:dossier_termine_only] } # triggerred by changes to the dossiers state. The email or attestation
else # is generated right after the dossier has reached its new state.
tags #
# 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 end
tags.select { |tag| (tag[:available_for_states] || Dossier::SOUMIS).include?(self.class::DOSSIER_STATE) }
end end
def procedure_type_de_champ_public_private_tags def procedure_type_de_champ_public_private_tags
@ -33,7 +41,7 @@ module TagsSubstitutionConcern
libelle: 'motivation', libelle: 'motivation',
description: 'Motivation facultative associée à la décision finale dacceptation, refus ou classement sans suite', description: 'Motivation facultative associée à la décision finale dacceptation, refus ou classement sans suite',
target: :motivation, target: :motivation,
dossier_termine_only: true available_for_states: Dossier::TERMINE
}, },
{ {
libelle: 'date de dépôt', libelle: 'date de dépôt',
@ -49,7 +57,7 @@ module TagsSubstitutionConcern
libelle: 'date de décision', libelle: 'date de décision',
description: 'Date de la décision dacceptation, refus, ou classement sans suite', description: 'Date de la décision dacceptation, refus, ou classement sans suite',
lambda: -> (d) { format_date(d.processed_at) }, 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: 'libellé procédure', description: '', lambda: -> (d) { d.procedure.libelle } },
{ libelle: 'numéro du dossier', description: '', target: :id } { libelle: 'numéro du dossier', description: '', target: :id }
@ -101,7 +109,7 @@ module TagsSubstitutionConcern
] ]
tags_and_datas 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) } .inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) }
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/closed_mail" TEMPLATE_NAME = "mails/closed_mail"
DISPLAYED_NAME = "Accusé d'acceptation" DISPLAYED_NAME = "Accusé d'acceptation"
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été accepté' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été accepté'
IS_DOSSIER_TERMINE = true DOSSIER_STATE = 'accepte'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/initiated_mail" TEMPLATE_NAME = "mails/initiated_mail"
DISPLAYED_NAME = 'Accusé de réception' DISPLAYED_NAME = 'Accusé de réception'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a bien été reçu' 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
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/received_mail" TEMPLATE_NAME = "mails/received_mail"
DISPLAYED_NAME = 'Accusé de passage en instruction' DISPLAYED_NAME = 'Accusé de passage en instruction'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- va être instruit' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- va être instruit'
IS_DOSSIER_TERMINE = false DOSSIER_STATE = 'en_instruction'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/refused_mail" TEMPLATE_NAME = "mails/refused_mail"
DISPLAYED_NAME = 'Accusé de rejet du dossier' DISPLAYED_NAME = 'Accusé de rejet du dossier'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été refusé' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été refusé'
IS_DOSSIER_TERMINE = true DOSSIER_STATE = 'refuse'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/without_continuation_mail" TEMPLATE_NAME = "mails/without_continuation_mail"
DISPLAYED_NAME = 'Accusé de classement sans suite' DISPLAYED_NAME = 'Accusé de classement sans suite'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été classé 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
end end

View file

@ -2,6 +2,7 @@ describe TagsSubstitutionConcern, type: :model do
let(:types_de_champ) { [] } let(:types_de_champ) { [] }
let(:types_de_champ_private) { [] } let(:types_de_champ_private) { [] }
let(:for_individual) { false } let(:for_individual) { false }
let(:state) { 'accepte' }
let(:procedure) do let(:procedure) do
create(:procedure, create(:procedure,
@ -13,17 +14,18 @@ describe TagsSubstitutionConcern, type: :model do
let(:template_concern) do let(:template_concern) do
(Class.new do (Class.new do
include TagsSubstitutionConcern include TagsSubstitutionConcern
public :replace_tags public :replace_tags
def initialize(p) def initialize(p, s)
@procedure = p @procedure = p
end self.class.const_set(:DOSSIER_STATE, s)
end
def procedure def procedure
@procedure @procedure
end end
end).new(procedure) end).new(procedure, state)
end end
describe 'replace_tags' do describe 'replace_tags' do
@ -119,8 +121,6 @@ describe TagsSubstitutionConcern, type: :model do
context 'when the dossier has a motivation' do context 'when the dossier has a motivation' do
let(:dossier) { create(:dossier, motivation: 'motivation') } let(:dossier) { create(:dossier, motivation: 'motivation') }
before { dossier.accepte! }
context 'and the template has some dossier tags' do context 'and the template has some dossier tags' do
let(:template) { '--motivation-- --numéro du dossier--' } let(:template) { '--motivation-- --numéro du dossier--' }
@ -173,7 +173,6 @@ describe TagsSubstitutionConcern, type: :model do
context "when using a date tag" do context "when using a date tag" do
before do before do
dossier.accepte!
dossier.en_construction_at = DateTime.new(2001, 2, 3) dossier.en_construction_at = DateTime.new(2001, 2, 3)
dossier.en_instruction_at = DateTime.new(2004, 5, 6) dossier.en_instruction_at = DateTime.new(2004, 5, 6)
dossier.processed_at = DateTime.new(2007, 8, 9) 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 context 'when generating a document for a dossier that is not termine' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
let(:template) { '--motivation-- --date de décision--' } let(:template) { '--motivation-- --date de décision--' }
let(:state) { 'en_instruction' }
subject { template_concern.replace_tags(template, dossier) } subject { template_concern.replace_tags(template, dossier) }
@ -247,15 +247,15 @@ describe TagsSubstitutionConcern, type: :model do
end end
describe 'tags' do 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: 'motivation' })) }
it { is_expected.to include(include({ libelle: 'date de décision' })) } it { is_expected.to include(include({ libelle: 'date de décision' })) }
end end
context 'when generating a document for a dossier that is not terminé' do 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: 'motivation' })) }
it { is_expected.not_to include(include({ libelle: 'date de décision' })) } it { is_expected.not_to include(include({ libelle: 'date de décision' })) }