Use stable_id when replacing tags
This commit is contained in:
parent
8279418e0a
commit
acb277e650
5 changed files with 86 additions and 16 deletions
|
@ -196,12 +196,14 @@ module TagsSubstitutionConcern
|
||||||
tags.filter { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) }
|
tags.filter { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def champ_public_tags
|
def champ_public_tags(dossier: nil)
|
||||||
types_de_champ_tags(procedure.types_de_champ, Dossier::SOUMIS)
|
types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ
|
||||||
|
types_de_champ_tags(types_de_champ, Dossier::SOUMIS)
|
||||||
end
|
end
|
||||||
|
|
||||||
def champ_private_tags
|
def champ_private_tags(dossier: nil)
|
||||||
types_de_champ_tags(procedure.types_de_champ_private, Dossier::INSTRUCTION_COMMENCEE)
|
types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ_private
|
||||||
|
types_de_champ_tags(types_de_champ, Dossier::INSTRUCTION_COMMENCEE)
|
||||||
end
|
end
|
||||||
|
|
||||||
def types_de_champ_tags(types_de_champ, available_for_states)
|
def types_de_champ_tags(types_de_champ, available_for_states)
|
||||||
|
@ -217,9 +219,11 @@ module TagsSubstitutionConcern
|
||||||
return ''
|
return ''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
text = normalize_tags(text)
|
||||||
|
|
||||||
tags_and_datas = [
|
tags_and_datas = [
|
||||||
[champ_public_tags, dossier.champs],
|
[champ_public_tags(dossier: dossier), dossier.champs],
|
||||||
[champ_private_tags, dossier.champs_private],
|
[champ_private_tags(dossier: dossier), dossier.champs_private],
|
||||||
[dossier_tags, dossier],
|
[dossier_tags, dossier],
|
||||||
[ROUTAGE_TAGS, dossier],
|
[ROUTAGE_TAGS, dossier],
|
||||||
[INDIVIDUAL_TAGS, dossier.individual],
|
[INDIVIDUAL_TAGS, dossier.individual],
|
||||||
|
@ -242,7 +246,7 @@ module TagsSubstitutionConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def replace_tag(text, tag, data)
|
def replace_tag(text, tag, data)
|
||||||
libelle = Regexp.quote(tag[:libelle])
|
libelle = Regexp.quote(tag[:id] ? tag[:id] : 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
|
# 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)
|
# (the '\\ |' is there because plain ASCII spaces were escaped by preceding Regexp.quote)
|
||||||
|
@ -256,4 +260,19 @@ module TagsSubstitutionConcern
|
||||||
|
|
||||||
text.gsub(/--#{libelle}--/, value.to_s)
|
text.gsub(/--#{libelle}--/, value.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize_tags(text)
|
||||||
|
tags = types_de_champ_tags(procedure.types_de_champ_for_tags, Dossier::SOUMIS) + types_de_champ_tags(procedure.types_de_champ_private_for_tags, Dossier::INSTRUCTION_COMMENCEE)
|
||||||
|
filter_tags(tags).reduce(text) { |text, tag| normalize_tag(text, tag) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_tag(text, tag)
|
||||||
|
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}--/, "--#{tag[:id]}--")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,6 +87,30 @@ class Procedure < ApplicationRecord
|
||||||
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
|
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def types_de_champ_for_tags
|
||||||
|
if brouillon?
|
||||||
|
draft_types_de_champ
|
||||||
|
else
|
||||||
|
TypeDeChamp.root
|
||||||
|
.public_only
|
||||||
|
.where(revision: revisions - [draft_revision])
|
||||||
|
.order(:created_at)
|
||||||
|
.uniq
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def types_de_champ_private_for_tags
|
||||||
|
if brouillon?
|
||||||
|
draft_types_de_champ_private
|
||||||
|
else
|
||||||
|
TypeDeChamp.root
|
||||||
|
.private_only
|
||||||
|
.where(revision: revisions - [draft_revision])
|
||||||
|
.order(:created_at)
|
||||||
|
.uniq
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def types_de_champ_for_export
|
def types_de_champ_for_export
|
||||||
types_de_champ.reject(&:exclude_from_export?)
|
types_de_champ.reject(&:exclude_from_export?)
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,26 +6,24 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
||||||
|
|
||||||
def tags_for_template
|
def tags_for_template
|
||||||
tags = super
|
tags = super
|
||||||
tdc = @type_de_champ
|
stable_id = @type_de_champ.stable_id
|
||||||
tags.push(
|
tags.push(
|
||||||
{
|
{
|
||||||
libelle: "#{libelle}/primaire",
|
libelle: "#{libelle}/primaire",
|
||||||
|
id: "tdc#{stable_id}/primaire",
|
||||||
description: "#{description} (menu primaire)",
|
description: "#{description} (menu primaire)",
|
||||||
lambda: -> (champs) {
|
lambda: -> (champs) {
|
||||||
champs
|
champs.find { |champ| champ.stable_id == stable_id }&.primary_value
|
||||||
.find { |champ| champ.type_de_champ == tdc }
|
|
||||||
&.primary_value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
tags.push(
|
tags.push(
|
||||||
{
|
{
|
||||||
libelle: "#{libelle}/secondaire",
|
libelle: "#{libelle}/secondaire",
|
||||||
|
id: "tdc#{stable_id}/secondaire",
|
||||||
description: "#{description} (menu secondaire)",
|
description: "#{description} (menu secondaire)",
|
||||||
lambda: -> (champs) {
|
lambda: -> (champs) {
|
||||||
champs
|
champs.find { |champ| champ.stable_id == stable_id }&.secondary_value
|
||||||
.find { |champ| champ.type_de_champ == tdc }
|
|
||||||
&.secondary_value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,13 +8,14 @@ class TypesDeChamp::TypeDeChampBase
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags_for_template
|
def tags_for_template
|
||||||
tdc = @type_de_champ
|
stable_id = @type_de_champ.stable_id
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
libelle: libelle,
|
libelle: libelle,
|
||||||
|
id: "tdc#{stable_id}",
|
||||||
description: description,
|
description: description,
|
||||||
lambda: -> (champs) {
|
lambda: -> (champs) {
|
||||||
champs.find { |champ| champ.type_de_champ == tdc }&.for_tag
|
champs.find { |champ| champ.stable_id == stable_id }&.for_tag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -8,6 +8,7 @@ describe TagsSubstitutionConcern, type: :model do
|
||||||
|
|
||||||
let(:procedure) do
|
let(:procedure) do
|
||||||
create(:procedure,
|
create(:procedure,
|
||||||
|
:published,
|
||||||
libelle: 'Une magnifique démarche',
|
libelle: 'Une magnifique démarche',
|
||||||
types_de_champ: types_de_champ,
|
types_de_champ: types_de_champ,
|
||||||
types_de_champ_private: types_de_champ_private,
|
types_de_champ_private: types_de_champ_private,
|
||||||
|
@ -389,6 +390,33 @@ describe TagsSubstitutionConcern, type: :model do
|
||||||
is_expected.to eq('--motivation-- --date de décision--')
|
is_expected.to eq('--motivation-- --date de décision--')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when procedure has revisions' do
|
||||||
|
let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon tag')] }
|
||||||
|
let(:draft_type_de_champ) { procedure.draft_revision.find_or_clone_type_de_champ(types_de_champ[0].stable_id) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
draft_type_de_champ.update(libelle: 'ton tag')
|
||||||
|
dossier.champs.first.update(value: 'valeur')
|
||||||
|
procedure.update!(draft_revision: procedure.create_new_revision, published_revision: procedure.draft_revision)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "replace by old label" do
|
||||||
|
let(:template) { '--mon tag--' }
|
||||||
|
|
||||||
|
it "should replace tag" do
|
||||||
|
is_expected.to eq('valeur')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "replace by new label" do
|
||||||
|
let(:template) { '--ton tag--' }
|
||||||
|
|
||||||
|
it "should replace tag" do
|
||||||
|
is_expected.to eq('valeur')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'tags' do
|
describe 'tags' do
|
||||||
|
|
Loading…
Reference in a new issue