From ac23d5fb4138885187f44b16c2dc1c96a9d2fed2 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 4 Jun 2024 10:19:15 +0200 Subject: [PATCH] convert date with dash for export renaming --- .../concerns/tags_substitution_concern.rb | 3 ++- app/models/export_template.rb | 1 + spec/factories/export_template.rb | 20 +++++++++++++++++++ spec/models/export_template_spec.rb | 8 ++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index 373ad3018..ae899dc04 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -307,7 +307,8 @@ module TagsSubstitutionConcern def format_date(date) if date.present? - date.strftime('%d/%m/%Y') + format = defined?(self.class::FORMAT_DATE) ? self.class::FORMAT_DATE : '%d/%m/%Y' + date.strftime(format) else '' end diff --git a/app/models/export_template.rb b/app/models/export_template.rb index febadd05a..550bb57cd 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -7,6 +7,7 @@ class ExportTemplate < ApplicationRecord validates_with ExportTemplateValidator DOSSIER_STATE = Dossier.states.fetch(:en_construction) + FORMAT_DATE = "%Y-%m-%d" def set_default_values content["default_dossier_directory"] = tiptap_json("dossier-") diff --git a/spec/factories/export_template.rb b/spec/factories/export_template.rb index d62754115..6785356af 100644 --- a/spec/factories/export_template.rb +++ b/spec/factories/export_template.rb @@ -64,5 +64,25 @@ FactoryBot.define do export_template.save end end + + trait :with_date_depot_for_export_pdf do + to_create do |export_template, _| + export_template.set_default_values + export_template.content["pdf_name"]["content"] = [ + { + "type" => "paragraph", + "content" => + [ + { "text" => "export_", "type" => "text" }, + { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, + { "text" => "-", "type" => "text" }, + { "type" => "mention", "attrs" => { "id" => "dossier_depose_at", "label" => "date de dépôt" } }, + { "text" => " ", "type" => "text" } + ] + } + ] + export_template.save + end + end end end diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index 443959bc1..d90de6744 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -190,6 +190,14 @@ describe ExportTemplate do it 'convert pdf_name' do expect(export_template.tiptap_convert(procedure.dossiers.first, "pdf_name")).to eq "mon_export_#{dossier.id}" end + + context 'for date' do + let(:export_template) { create(:export_template, :with_date_depot_for_export_pdf, groupe_instructeur:) } + let(:dossier) { create(:dossier, :en_construction, procedure:, depose_at: Date.parse("2024/03/30")) } + it 'convert date with dash' do + expect(export_template.tiptap_convert(dossier, "pdf_name")).to eq "export_#{dossier.id}-2024-03-30" + end + end end describe '#tiptap_convert_pj' do