feat(tags): expose used_type_de_champ_tags
This commit is contained in:
parent
745d0809a2
commit
4d48055158
5 changed files with 82 additions and 38 deletions
|
@ -143,14 +143,7 @@ class AttestationTemplate < ApplicationRecord
|
||||||
private
|
private
|
||||||
|
|
||||||
def used_tags
|
def used_tags
|
||||||
delimiters_regex = /--(?<capture>((?!--).)*)--/
|
used_tags_for(title) + used_tags_for(body)
|
||||||
|
|
||||||
# We can't use flat_map as scan will return 3 levels of array,
|
|
||||||
# using flat_map would give us 2, whereas flatten will
|
|
||||||
# give us 1, which is what we want
|
|
||||||
[normalize_tags(title), normalize_tags(body)]
|
|
||||||
.map { |str| str.scan(delimiters_regex) }
|
|
||||||
.flatten
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_pdf(dossier)
|
def build_pdf(dossier)
|
||||||
|
|
|
@ -139,6 +139,10 @@ module TagsSubstitutionConcern
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
SHARED_TAG_LIBELLES = (DOSSIER_TAGS + DOSSIER_TAGS_FOR_MAIL + INDIVIDUAL_TAGS + ENTREPRISE_TAGS + ROUTAGE_TAGS).map { |tag| tag[:libelle] }
|
||||||
|
|
||||||
|
TAG_DELIMITERS_REGEX = /--(?<capture>((?!--).)*)--/
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
if procedure.for_individual?
|
if procedure.for_individual?
|
||||||
identity_tags = INDIVIDUAL_TAGS
|
identity_tags = INDIVIDUAL_TAGS
|
||||||
|
@ -154,6 +158,33 @@ module TagsSubstitutionConcern
|
||||||
filter_tags(identity_tags + dossier_tags + champ_public_tags + champ_private_tags + routage_tags)
|
filter_tags(identity_tags + dossier_tags + champ_public_tags + champ_private_tags + routage_tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def used_type_de_champ_tags(text)
|
||||||
|
used_tags_for(text, with_libelle: true).filter_map do |(tag, libelle)|
|
||||||
|
if !tag.in?(SHARED_TAG_LIBELLES)
|
||||||
|
if tag.start_with?('tdc')
|
||||||
|
[libelle, tag.gsub('tdc', '').to_i]
|
||||||
|
else
|
||||||
|
[tag]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def used_tags_for(text, with_libelle: false)
|
||||||
|
text, tags = normalize_tags(text)
|
||||||
|
text
|
||||||
|
.scan(TAG_DELIMITERS_REGEX)
|
||||||
|
.flatten
|
||||||
|
.map do |tag_str|
|
||||||
|
if with_libelle
|
||||||
|
tag = tags.find { |tag| tag[:id] == tag_str }
|
||||||
|
[tag_str, tag ? tag[:libelle] : nil]
|
||||||
|
else
|
||||||
|
tag_str
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def format_date(date)
|
def format_date(date)
|
||||||
|
@ -197,7 +228,7 @@ module TagsSubstitutionConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def champ_public_tags(dossier: nil)
|
def champ_public_tags(dossier: nil)
|
||||||
types_de_champ = dossier&.types_de_champ || procedure.active_revision.types_de_champ_public
|
types_de_champ = (dossier || procedure.active_revision).types_de_champ_public
|
||||||
types_de_champ_tags(types_de_champ, Dossier::SOUMIS)
|
types_de_champ_tags(types_de_champ, Dossier::SOUMIS)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -219,7 +250,7 @@ module TagsSubstitutionConcern
|
||||||
return ''
|
return ''
|
||||||
end
|
end
|
||||||
|
|
||||||
text = normalize_tags(text)
|
text, _ = normalize_tags(text)
|
||||||
|
|
||||||
tags_and_datas = [
|
tags_and_datas = [
|
||||||
[champ_public_tags(dossier: dossier), dossier.champs],
|
[champ_public_tags(dossier: dossier), dossier.champs],
|
||||||
|
@ -262,8 +293,8 @@ module TagsSubstitutionConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_tags(text)
|
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)
|
tags = types_de_champ_tags(procedure.types_de_champ_public_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) }
|
[filter_tags(tags).reduce(text) { |text, tag| normalize_tag(text, tag) }, tags]
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_tag(text, tag)
|
def normalize_tag(text, tag)
|
||||||
|
|
|
@ -435,6 +435,10 @@ class Dossier < ApplicationRecord
|
||||||
validates :individual, presence: true, if: -> { revision.procedure.for_individual? }
|
validates :individual, presence: true, if: -> { revision.procedure.for_individual? }
|
||||||
validates :groupe_instructeur, presence: true, if: -> { !brouillon? }
|
validates :groupe_instructeur, presence: true, if: -> { !brouillon? }
|
||||||
|
|
||||||
|
def types_de_champ_public
|
||||||
|
types_de_champ
|
||||||
|
end
|
||||||
|
|
||||||
EXPORT_BATCH_SIZE = 2000
|
EXPORT_BATCH_SIZE = 2000
|
||||||
|
|
||||||
def self.downloadable_sorted_batch
|
def self.downloadable_sorted_batch
|
||||||
|
|
|
@ -163,35 +163,21 @@ class Procedure < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def types_de_champ_for_tags
|
def types_de_champ_for_tags
|
||||||
if brouillon?
|
TypeDeChamp
|
||||||
draft_types_de_champ
|
.fillable
|
||||||
else
|
.joins(:revisions)
|
||||||
TypeDeChamp
|
.where(procedure_revisions: brouillon? ? { id: draft_revision_id } : { procedure_id: id })
|
||||||
.public_only
|
.where(revision_types_de_champ: { parent_id: nil })
|
||||||
.fillable
|
.order(:created_at)
|
||||||
.joins(:revisions)
|
.distinct(:id)
|
||||||
.where(procedure_revisions: { procedure_id: id })
|
end
|
||||||
.where.not(procedure_revisions: { id: draft_revision_id })
|
|
||||||
.where(revision_types_de_champ: { parent_id: nil })
|
def types_de_champ_public_for_tags
|
||||||
.order(:created_at)
|
types_de_champ_for_tags.public_only
|
||||||
.uniq
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def types_de_champ_private_for_tags
|
def types_de_champ_private_for_tags
|
||||||
if brouillon?
|
types_de_champ_for_tags.private_only
|
||||||
draft_types_de_champ_private
|
|
||||||
else
|
|
||||||
TypeDeChamp
|
|
||||||
.private_only
|
|
||||||
.fillable
|
|
||||||
.joins(:revisions)
|
|
||||||
.where(procedure_revisions: { procedure_id: id })
|
|
||||||
.where.not(procedure_revisions: { id: draft_revision_id })
|
|
||||||
.where(revision_types_de_champ: { parent_id: nil })
|
|
||||||
.order(:created_at)
|
|
||||||
.uniq
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
has_many :administrateurs_procedures, dependent: :delete_all
|
has_many :administrateurs_procedures, dependent: :delete_all
|
||||||
|
|
|
@ -467,4 +467,34 @@ describe TagsSubstitutionConcern, type: :model do
|
||||||
it { is_expected.to include(include({ libelle: 'public' })) }
|
it { is_expected.to include(include({ libelle: 'public' })) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'used_tags_for' do
|
||||||
|
let(:text) { 'hello world --public--, --numéro du dossier--, --yolo--' }
|
||||||
|
subject { template_concern.used_tags_for(text) }
|
||||||
|
|
||||||
|
let(:types_de_champ) do
|
||||||
|
[
|
||||||
|
build(:type_de_champ, libelle: 'public'),
|
||||||
|
build(:type_de_champ_header_section, libelle: 'entête de section'),
|
||||||
|
build(:type_de_champ_explication, libelle: 'explication')
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq(["tdc#{types_de_champ.first.stable_id}", 'numéro du dossier', 'yolo']) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'used_type_de_champ_tags' do
|
||||||
|
let(:text) { 'hello world --public--, --numéro du dossier--, --yolo--' }
|
||||||
|
subject { template_concern.used_type_de_champ_tags(text) }
|
||||||
|
|
||||||
|
let(:types_de_champ) do
|
||||||
|
[
|
||||||
|
build(:type_de_champ, libelle: 'public'),
|
||||||
|
build(:type_de_champ_header_section, libelle: 'entête de section'),
|
||||||
|
build(:type_de_champ_explication, libelle: 'explication')
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq([["public", types_de_champ.first.stable_id], ['yolo']]) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue