From aeb4bd2ff19b51cf58e83df86118620a701144c6 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 19 Mar 2024 11:03:29 +0100 Subject: [PATCH] add original-filename tag --- app/models/export_template.rb | 19 +++++++--- .../export_templates/_form.html.haml | 2 +- spec/models/export_template_spec.rb | 35 +++++++++++++------ 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/app/models/export_template.rb b/app/models/export_template.rb index 8dc01aa85..b90a81a0e 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -58,16 +58,17 @@ class ExportTemplate < ApplicationRecord end end - def tiptap_convert_pj(dossier, pj_stable_id) - if content_for_pj_id(pj_stable_id)["content"]&.first["content"] - render_attributes_for(content_for_pj_id(pj_stable_id), dossier) + def tiptap_convert_pj(dossier, pj_stable_id, attachment = nil) + if content_for_pj_id(pj_stable_id)["content"]&.first&.[]("content") + render_attributes_for(content_for_pj_id(pj_stable_id), dossier, attachment) end end - def render_attributes_for(content_for, dossier) + def render_attributes_for(content_for, dossier, attachment = nil) tiptap = TiptapService.new used_tags = tiptap.used_tags_and_libelle_for(content_for.deep_symbolize_keys) substitutions = tags_substitutions(used_tags, dossier, escape: false) + substitutions['original-filename'] = attachment.filename.base if attachment tiptap.to_path(content_for.deep_symbolize_keys, substitutions) end @@ -88,6 +89,14 @@ class ExportTemplate < ApplicationRecord tags_categorized.slice(:individual, :etablissement, :dossier).values.flatten end + def tags_for_pj + specific_tags.push({ + libelle: 'nom original du fichier', + id: 'original-filename', + maybe_null: false + }) + end + private def tiptap_content(key) @@ -134,7 +143,7 @@ class ExportTemplate < ApplicationRecord stable_id = TypeDeChamp.find(type_de_champ_id).stable_id tiptap_pj = content["pjs"].find { |pj| pj["stable_id"] == stable_id.to_s } if tiptap_pj - File.join(folder(dossier), tiptap_convert_pj(dossier, stable_id) + suffix(attachment, index, row_index)) + File.join(folder(dossier), tiptap_convert_pj(dossier, stable_id, attachment) + suffix(attachment, index, row_index)) else File.join(folder(dossier), "erreur_renommage", attachment.filename.to_s) end diff --git a/app/views/instructeurs/export_templates/_form.html.haml b/app/views/instructeurs/export_templates/_form.html.haml index b0f7676fd..9f76d9aee 100644 --- a/app/views/instructeurs/export_templates/_form.html.haml +++ b/app/views/instructeurs/export_templates/_form.html.haml @@ -45,7 +45,7 @@ = label_tag pj.libelle, nil, name: field_name(:export_template, "tiptap_pj_#{pj.stable_id}"), class: "fr-label" .tiptap-editor.fr-mt-1w{ data: { tiptap_target: 'editor' } } = hidden_field_tag field_name(:export_template, "tiptap_pj_#{pj.stable_id}"), "#{@export_template.content_for_pj(pj)}" , data: { tiptap_target: 'input', controller: 'turbo-input', turbo_input_url_value: preview_instructeur_export_templates_path } - .fr-mt-2w= render TagsButtonListComponent.new(tags: { nil => @export_template.specific_tags }) + .fr-mt-2w= render TagsButtonListComponent.new(tags: { nil => @export_template.tags_for_pj }) .fixed-footer .fr-container diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index cb8d343ed..6d314f136 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -26,12 +26,16 @@ describe ExportTemplate do }, "pjs" => [ - {path: {"type"=>"doc", "content"=>[{"type"=>"paragraph", "content"=>[{"type"=>"mention", "attrs"=>{"id"=>"dossier_number", "label"=>"numéro du dossier"}}, {"text"=>" _justif", "type"=>"text"}]}]}, stable_id: "3"}, - { path: - {"type"=>"doc", "content"=>[{"type"=>"paragraph", "content"=>[{"text"=>"cni_", "type"=>"text"}, {"type"=>"mention", "attrs"=>{"id"=>"dossier_number", "label"=>"numéro du dossier"}}, {"text"=>" ", "type"=>"text"}]}]}, - stable_id: "5"}, - { path: {"type"=>"doc", "content"=>[{"type"=>"paragraph", "content"=>[{"text"=>"pj_repet_", "type"=>"text"}, {"type"=>"mention", "attrs"=>{"id"=>"dossier_number", "label"=>"numéro du dossier"}}, {"text"=>" ", "type"=>"text"}]}]}, - stable_id: "10"} + { path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] }] }, stable_id: "3" }, + { + path: + { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "cni_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] }, + stable_id: "5" + }, + { + path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "pj_repet_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] }, + stable_id: "10" + } ] } end @@ -95,9 +99,9 @@ describe ExportTemplate do it 'returns tiptap content for pj' do expect(export_template.content_for_pj(type_de_champ_pj)).to eq({ - "type"=>"doc", - "content"=> [ - {"type"=>"paragraph", "content"=>[{"type"=>"mention", "attrs"=>{"id"=>"dossier_number", "label"=>"numéro du dossier"}}, {"text"=>" _justif", "type"=>"text"}]} + "type" => "doc", + "content" => [ + { "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] } ] }.to_json) end @@ -126,7 +130,7 @@ describe ExportTemplate do dossier.champs_public << champ_pj end it 'returns pj and custom name for pj' do - expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/#{dossier.id}_justif.png"]) + expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/superpj_justif.png"]) end end context 'pj repetable' do @@ -172,6 +176,17 @@ describe ExportTemplate do end end + describe '#tiptap_convert_pj' do + let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) } + let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj) } + let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) } + + it 'convert pj' do + attachment + expect(export_template.tiptap_convert_pj(dossier, type_de_champ_pj.stable_id, attachment)).to eq "superpj_justif" + end + end + describe '#valid?' do let(:subject) { build(:export_template, groupe_instructeur:, content:) } let(:ddd_text) { "DoSSIER" }