Merge pull request #5948 from tchak/revisions-and-tags
Use stable_id when replacing tags
This commit is contained in:
commit
d7c7f3f2d7
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) }
|
||||
end
|
||||
|
||||
def champ_public_tags
|
||||
types_de_champ_tags(procedure.types_de_champ, Dossier::SOUMIS)
|
||||
def champ_public_tags(dossier: nil)
|
||||
types_de_champ = (dossier || procedure.active_revision).types_de_champ
|
||||
types_de_champ_tags(types_de_champ, Dossier::SOUMIS)
|
||||
end
|
||||
|
||||
def champ_private_tags
|
||||
types_de_champ_tags(procedure.types_de_champ_private, Dossier::INSTRUCTION_COMMENCEE)
|
||||
def champ_private_tags(dossier: nil)
|
||||
types_de_champ = (dossier || procedure.active_revision).types_de_champ_private
|
||||
types_de_champ_tags(types_de_champ, Dossier::INSTRUCTION_COMMENCEE)
|
||||
end
|
||||
|
||||
def types_de_champ_tags(types_de_champ, available_for_states)
|
||||
|
@ -217,9 +219,11 @@ module TagsSubstitutionConcern
|
|||
return ''
|
||||
end
|
||||
|
||||
text = normalize_tags(text)
|
||||
|
||||
tags_and_datas = [
|
||||
[champ_public_tags, dossier.champs],
|
||||
[champ_private_tags, dossier.champs_private],
|
||||
[champ_public_tags(dossier: dossier), dossier.champs],
|
||||
[champ_private_tags(dossier: dossier), dossier.champs_private],
|
||||
[dossier_tags, dossier],
|
||||
[ROUTAGE_TAGS, dossier],
|
||||
[INDIVIDUAL_TAGS, dossier.individual],
|
||||
|
@ -242,7 +246,7 @@ module TagsSubstitutionConcern
|
|||
end
|
||||
|
||||
def replace_tag(text, tag, data)
|
||||
libelle = Regexp.quote(tag[:libelle])
|
||||
libelle = Regexp.quote(tag[:id].presence || 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)
|
||||
|
@ -256,4 +260,19 @@ module TagsSubstitutionConcern
|
|||
|
||||
text.gsub(/--#{libelle}--/, value.to_s)
|
||||
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
|
||||
|
|
|
@ -87,6 +87,30 @@ class Procedure < ApplicationRecord
|
|||
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
|
||||
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
|
||||
types_de_champ.reject(&:exclude_from_export?)
|
||||
end
|
||||
|
|
|
@ -6,26 +6,24 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
|||
|
||||
def tags_for_template
|
||||
tags = super
|
||||
tdc = @type_de_champ
|
||||
stable_id = @type_de_champ.stable_id
|
||||
tags.push(
|
||||
{
|
||||
libelle: "#{libelle}/primaire",
|
||||
id: "tdc#{stable_id}/primaire",
|
||||
description: "#{description} (menu primaire)",
|
||||
lambda: -> (champs) {
|
||||
champs
|
||||
.find { |champ| champ.type_de_champ == tdc }
|
||||
&.primary_value
|
||||
champs.find { |champ| champ.stable_id == stable_id }&.primary_value
|
||||
}
|
||||
}
|
||||
)
|
||||
tags.push(
|
||||
{
|
||||
libelle: "#{libelle}/secondaire",
|
||||
id: "tdc#{stable_id}/secondaire",
|
||||
description: "#{description} (menu secondaire)",
|
||||
lambda: -> (champs) {
|
||||
champs
|
||||
.find { |champ| champ.type_de_champ == tdc }
|
||||
&.secondary_value
|
||||
champs.find { |champ| champ.stable_id == stable_id }&.secondary_value
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -8,13 +8,14 @@ class TypesDeChamp::TypeDeChampBase
|
|||
end
|
||||
|
||||
def tags_for_template
|
||||
tdc = @type_de_champ
|
||||
stable_id = @type_de_champ.stable_id
|
||||
[
|
||||
{
|
||||
libelle: libelle,
|
||||
id: "tdc#{stable_id}",
|
||||
description: description,
|
||||
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
|
||||
create(:procedure,
|
||||
:published,
|
||||
libelle: 'Une magnifique démarche',
|
||||
types_de_champ: types_de_champ,
|
||||
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--')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when procedure has revisions' do
|
||||
let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon ancien libellé')] }
|
||||
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: 'mon nouveau libellé')
|
||||
dossier.champs.first.update(value: 'valeur')
|
||||
procedure.update!(draft_revision: procedure.create_new_revision, published_revision: procedure.draft_revision)
|
||||
end
|
||||
|
||||
context "when using the champ's original label" do
|
||||
let(:template) { '--mon ancien libellé--' }
|
||||
|
||||
it "replaces the tag" do
|
||||
is_expected.to eq('valeur')
|
||||
end
|
||||
end
|
||||
|
||||
context "when using the champ's revised label" do
|
||||
let(:template) { '--mon nouveau libellé--' }
|
||||
|
||||
it "replaces the tag" do
|
||||
is_expected.to eq('valeur')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'tags' do
|
||||
|
|
Loading…
Reference in a new issue