From ab63f80080f44d32192c72a334cbf71ba55f1ace Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Tue, 2 Jan 2018 16:10:37 +0100 Subject: [PATCH] =?UTF-8?q?[#1110]=20Motivation=20and=20date=20de=20d?= =?UTF-8?q?=C3=A9cision=20are=20only=20tags=20for=20termin=C3=A9=20dossier?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concerns/tags_substitution_concern.rb | 25 ++++++++++++----- .../concern/tags_substitution_concern_spec.rb | 27 +++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) 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/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