[Fix #866] Allow use of non-breaking spaces in balise in attestation template
This commit is contained in:
parent
83dc3d738e
commit
94f1a1fa0c
2 changed files with 52 additions and 2 deletions
|
@ -128,17 +128,27 @@ class AttestationTemplate < ApplicationRecord
|
|||
.select { |dossier_champ| dossier_champ.libelle == tag[:libelle] }
|
||||
.first
|
||||
|
||||
acc.gsub("--#{tag[:libelle]}--", champ.to_s)
|
||||
replace_tag(acc, tag, champ)
|
||||
end
|
||||
end
|
||||
|
||||
def replace_tags_with_values_from_data(text, tags, data)
|
||||
if data.present?
|
||||
tags.inject(text) do |acc, tag|
|
||||
acc.gsub("--#{tag[:libelle]}--", data.send(tag[:target].to_sym).to_s)
|
||||
replace_tag(acc, tag, data.send(tag[:target].to_sym))
|
||||
end
|
||||
else
|
||||
text
|
||||
end
|
||||
end
|
||||
|
||||
def replace_tag(text, tag, value)
|
||||
libelle = Regexp.quote(tag[:libelle])
|
||||
|
||||
# allow any kind of space (non-breaking or other) in the tag’s libellé to match any kind of space in the template
|
||||
# (the '\\ |' is there because plain ASCII spaces were escaped by preceding Regexp.quote)
|
||||
libelle.gsub!(/\\ |[[:blank:]]/, "[[:blank:]]")
|
||||
|
||||
text.gsub(/--#{libelle}--/, value.to_s)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -283,5 +283,45 @@ describe AttestationTemplate, type: :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "match breaking and non breaking spaces" do
|
||||
before do
|
||||
c = dossier.champs.first
|
||||
c.value = 'valeur'
|
||||
c.save
|
||||
end
|
||||
|
||||
context "when the tag contains a non breaking space" do
|
||||
let(:template_body) { 'body --mon tag--' }
|
||||
|
||||
context 'and the champ contains the non breaking space' do
|
||||
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'mon tag')] }
|
||||
|
||||
it { expect(view_args[:body]).to eq('body valeur') }
|
||||
end
|
||||
|
||||
context 'and the champ has an ordinary space' do
|
||||
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'mon tag')] }
|
||||
|
||||
it { expect(view_args[:body]).to eq('body valeur') }
|
||||
end
|
||||
end
|
||||
|
||||
context "when the tag contains an ordinay space" do
|
||||
let(:template_body) { 'body --mon tag--' }
|
||||
|
||||
context 'and the champ contains a non breaking space' do
|
||||
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'mon tag')] }
|
||||
|
||||
it { expect(view_args[:body]).to eq('body valeur') }
|
||||
end
|
||||
|
||||
context 'and the champ has an ordinary space' do
|
||||
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'mon tag')] }
|
||||
|
||||
it { expect(view_args[:body]).to eq('body valeur') }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue