From 59bdd4bd92da41a250b563161b7016ac98d47e10 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 14 Dec 2022 17:32:38 +0100 Subject: [PATCH] fix(tags): relax parser roules --- app/models/concerns/tags_substitution_concern.rb | 9 ++++++++- .../linked_drop_down_list_type_de_champ.rb | 4 ++-- app/models/types_de_champ/type_de_champ_base.rb | 2 +- spec/models/concern/tags_substitution_concern_spec.rb | 8 +++++++- spec/models/mail_template_spec.rb | 2 +- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index 41cf26437..a84412e47 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -12,6 +12,13 @@ module TagsSubstitutionConcern doc.parse io end + def self.normalize(str) + str + .sub(/^[[:space:]]+/, '') + .sub(/[[:space:]]+$/, '') + .gsub(/[[:space:]]/, ' ') + end + define_combinator :doc do many(tag | text) < eof end @@ -24,7 +31,7 @@ module TagsSubstitutionConcern define_combinator :tag do between(tag_delimiter, tag_delimiter, tag_text).fmap do |tag| - { tag: tag } + { tag: TagsParser.normalize(tag) } end end diff --git a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb index 46ebc9768..58ecb6a92 100644 --- a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb @@ -9,7 +9,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas stable_id = @type_de_champ.stable_id tags.push( { - libelle: "#{libelle}/primaire", + libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/primaire", id: "tdc#{stable_id}/primaire", description: "#{description} (menu primaire)", lambda: -> (champs) { @@ -19,7 +19,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas ) tags.push( { - libelle: "#{libelle}/secondaire", + libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/secondaire", id: "tdc#{stable_id}/secondaire", description: "#{description} (menu secondaire)", lambda: -> (champs) { diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index 2e9c5b1e6..9c71f1a82 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -16,7 +16,7 @@ class TypesDeChamp::TypeDeChampBase stable_id = self.stable_id [ { - libelle: libelle.gsub(/[[:space:]]/, ' '), + libelle: TagsSubstitutionConcern::TagsParser.normalize(libelle), id: "tdc#{stable_id}", description: description, lambda: -> (champs) { diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index fda04c214..b169a7df5 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -512,10 +512,16 @@ describe TagsSubstitutionConcern, type: :model do end it 'allow for - before tag' do - tokens = TagsSubstitutionConcern::TagsParser.parse("hello --yolo-- world ---numéro-du - dossier--") + tokens = TagsSubstitutionConcern::TagsParser.parse("hello --yolo-- -- before-- --after -- -- around -- world ---numéro-du - dossier--") expect(tokens).to eq([ { text: "hello " }, { tag: "yolo" }, + { text: " " }, + { tag: "before" }, + { text: " " }, + { tag: "after" }, + { text: " " }, + { tag: "around" }, { text: " world -" }, { tag: "numéro-du - dossier" } ]) diff --git a/spec/models/mail_template_spec.rb b/spec/models/mail_template_spec.rb index 6f981af2b..6616bc1b1 100644 --- a/spec/models/mail_template_spec.rb +++ b/spec/models/mail_template_spec.rb @@ -38,7 +38,7 @@ describe Mails::InitiatedMail, type: :model do context 'template with invalid tag' do let(:email_body) { 'foo --numéro du -- bar' } - it { expect(subject.errors.full_messages).to eq(["Le contenu de l’email de notification de passage du dossier en instruction réfère au champ \"numéro du \" qui n’existe pas"]) } + it { expect(subject.errors.full_messages).to eq(["Le contenu de l’email de notification de passage du dossier en instruction réfère au champ \"numéro du\" qui n’existe pas"]) } end context 'template with unpublished tag' do