[#3356] Let tags handle their own substitution

This commit is contained in:
Frederic Merizen 2019-02-05 15:25:54 +01:00 committed by simon lehericey
parent 656061b21a
commit 5bc8bbbaa2
2 changed files with 15 additions and 13 deletions

View file

@ -195,35 +195,33 @@ module TagsSubstitutionConcern
def replace_type_de_champ_tags(text, tags, dossier_champs)
tags.inject(text) do |acc, tag|
champ = dossier_champs
.detect { |dossier_champ| dossier_champ.libelle == tag[:libelle] }
replace_tag(acc, tag, champ)
replace_tag(acc, tag, dossier_champs)
end
end
def replace_tags_with_values_from_data(text, tags, data)
if data.present?
tags.inject(text) do |acc, tag|
if tag.key?(:target)
value = data.send(tag[:target])
else
value = instance_exec(data, &tag[:lambda])
end
replace_tag(acc, tag, value)
replace_tag(acc, tag, data)
end
else
text
end
end
def replace_tag(text, tag, value)
def replace_tag(text, tag, data)
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:]]")
if tag.key?(:target)
value = data.send(tag[:target])
else
value = instance_exec(data, &tag[:lambda])
end
text.gsub(/--#{libelle}--/, value.to_s)
end
end

View file

@ -175,10 +175,14 @@ class TypeDeChamp < ApplicationRecord
end
def tags_for_template
l = libelle
[
{
libelle: libelle,
description: description
libelle: l,
description: description,
lambda: -> (champs) {
champs.detect { |champ| champ.libelle == l }
}
}
]
end