From 5257d20ba0563c608d0980514953bddd88070848 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 16 Oct 2017 17:39:34 +0200 Subject: [PATCH] =?UTF-8?q?[fix=20=20#845]=20Attestation:=20affiche=20les?= =?UTF-8?q?=20dates=20au=20format=20fran=C3=A7ais?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/attestation_template.rb | 7 +++--- app/models/champ.rb | 13 ++++++++++++ spec/models/attestation_template_spec.rb | 27 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 3e84c0108..829638d5e 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -124,12 +124,11 @@ class AttestationTemplate < ApplicationRecord def replace_type_de_champ_tags(text, types_de_champ, dossier_champs) types_de_champ.inject(text) do |acc, tag| - value = dossier_champs - .select { |champ| champ.libelle == tag[:libelle] } + champ = dossier_champs + .select { |dossier_champ| dossier_champ.libelle == tag[:libelle] } .first - .value - acc.gsub("--#{tag[:libelle]}--", value.to_s) + acc.gsub("--#{tag[:libelle]}--", champ.to_s) end end diff --git a/app/models/champ.rb b/app/models/champ.rb index 950a6b70b..ea4043483 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -57,6 +57,19 @@ class Champ < ActiveRecord::Base JSON.parse(Carto::GeoAPI::Driver.pays).inject([]) { |acc, liste| acc.push(liste['nom']) } end + def to_s + if value.present? + case type_champ + when 'date' + Date.parse(value).strftime('%d/%m/%Y') + else + value.to_s + end + else + '' + end + end + private def format_date_to_iso diff --git a/spec/models/attestation_template_spec.rb b/spec/models/attestation_template_spec.rb index 6d7dffb7c..4f0768d54 100644 --- a/spec/models/attestation_template_spec.rb +++ b/spec/models/attestation_template_spec.rb @@ -255,5 +255,32 @@ describe AttestationTemplate, type: :model do end end end + + context 'when the procedure has 2 types de champ date and datetime' do + let(:types_de_champ) do + [create(:type_de_champ_public, libelle: 'date', type_champ: 'date'), + create(:type_de_champ_public, libelle: 'datetime', type_champ: 'datetime')] + end + + context 'and the are used in the template title' do + let(:template_title) { 'title --date-- --datetime--' } + + context 'and its value in the dossier are not nil' do + before :each do + dossier.champs + .select { |champ| champ.type_champ == 'date' } + .first + .value = '2017-04-15' + + dossier.champs + .select { |champ| champ.type_champ == 'datetime' } + .first + .value = '13/09/2017 09:00' + end + + it { expect(view_args[:title]).to eq('title 15/04/2017 13/09/2017 09:00') } + end + end + end end end