[#1110] Motivation and date de décision are only tags for terminé dossiers

This commit is contained in:
Frederic Merizen 2018-01-02 16:10:37 +01:00
parent e985439416
commit ab63f80080
2 changed files with 44 additions and 8 deletions

View file

@ -1,18 +1,27 @@
module TagsSubstitutionConcern module TagsSubstitutionConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
def tags def tags(is_dossier_termine: true)
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
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 end
private 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 def procedure_type_de_champ_public_private_tags
(procedure.types_de_champ + procedure.types_de_champ_private) (procedure.types_de_champ + procedure.types_de_champ_private)
.map { |tdc| { libelle: tdc.libelle, description: tdc.description } } .map { |tdc| { libelle: tdc.libelle, description: tdc.description } }
@ -21,10 +30,12 @@ module TagsSubstitutionConcern
def dossier_tags def dossier_tags
[{ 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 },
{ 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) { 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: '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 }]
end end
@ -60,8 +71,10 @@ module TagsSubstitutionConcern
[entreprise_tags, dossier.entreprise], [entreprise_tags, dossier.entreprise],
[etablissement_tags, dossier.entreprise&.etablissement]] [etablissement_tags, dossier.entreprise&.etablissement]]
tags_and_datas.inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) } tags_and_datas
end .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) def replace_type_de_champ_tags(text, types_de_champ, dossier_champs)
types_de_champ.inject(text) do |acc, tag| types_de_champ.inject(text) do |acc, tag|

View file

@ -117,6 +117,8 @@ 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--' }
@ -208,11 +210,32 @@ describe TagsSubstitutionConcern, type: :model do
it_behaves_like "treat all kinds of space as equivalent" it_behaves_like "treat all kinds of space as equivalent"
end end
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 end
describe 'tags' do 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
end end