Merge pull request #1101 from sgmap/fix-866-tag_unbreakable_space

[Fix #866] Allow use of non-breaking spaces in balise in attestation template
This commit is contained in:
Frederic Merizen 2017-12-14 18:27:57 +01:00 committed by GitHub
commit 11b58fbb0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View file

@ -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 tags 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

View file

@ -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