From fb29d30826bc2fd7d1d0742e436c6279755cda38 Mon Sep 17 00:00:00 2001 From: pedong Date: Thu, 31 Jan 2019 17:11:53 +0100 Subject: [PATCH] [fix #3342] show date with format letter --- app/helpers/application_helper.rb | 16 +++++ .../shared/dossiers/_champ_row.html.haml | 4 ++ spec/helpers/application_helper_spec.rb | 58 +++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 28dc0af2a..10a3cc258 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -119,4 +119,20 @@ module ApplicationHelper {} end end + + def try_format_date(date) + begin + Date.parse(date).strftime("%d %B %Y") + rescue + date + end + end + + def try_format_datetime(datetime) + begin + Time.zone.parse(datetime).strftime("%d %B %Y %R") + rescue + datetime + end + end end diff --git a/app/views/shared/dossiers/_champ_row.html.haml b/app/views/shared/dossiers/_champ_row.html.haml index 361c6e786..62f6feb87 100644 --- a/app/views/shared/dossiers/_champ_row.html.haml +++ b/app/views/shared/dossiers/_champ_row.html.haml @@ -30,6 +30,10 @@ = render partial: "shared/champs/siret/show", locals: { champ: c, profile: profile } - when TypeDeChamp.type_champs.fetch(:textarea) = render partial: "shared/champs/textarea/show", locals: { champ: c } + - when TypeDeChamp.type_champs.fetch(:date) + = try_format_date(c.to_s) + - when TypeDeChamp.type_champs.fetch(:datetime) + = try_format_datetime(c.to_s) - else = sanitize(c.to_s) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 30a7becef..6998f17d2 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -17,4 +17,62 @@ describe ApplicationHelper do it { is_expected.to be_nil } end end + + describe "#try_format_date" do + subject { try_format_date(date) } + + describe 'try formatting 2019-01-24' do + let(:date) { "2019-01-24" } + it { is_expected.to eq("24 January 2019") } + end + + describe 'try formatting 24/01/2019' do + let(:date) { "24/01/2019" } + it { is_expected.to eq("24 January 2019") } + end + + describe 'try formatting 2019-01-32' do + let(:date) { "2019-01-32" } + it { is_expected.to eq("2019-01-32") } + end + + describe 'try formatting a blank string' do + let(:date) { "" } + it { is_expected.to eq("") } + end + + describe 'try formatting a nil string' do + let(:date) { nil } + it { is_expected.to be_nil } + end + end + + describe "#try_format_datetime" do + subject { try_format_datetime(datetime) } + + describe 'try formatting 31/01/2019 11:25' do + let(:datetime) { "31/01/2019 11:25" } + it { is_expected.to eq("31 January 2019 11:25") } + end + + describe 'try formatting 2019-01-31 11:25' do + let(:datetime) { "2019-01-31 11:25" } + it { is_expected.to eq("31 January 2019 11:25") } + end + + describe 'try formatting 2019-01-32 11:25' do + let(:datetime) { "2019-01-32 11:25" } + it { is_expected.to eq("2019-01-32 11:25") } + end + + describe 'try formatting a blank string' do + let(:datetime) { "" } + it { is_expected.to eq("") } + end + + describe 'try formatting a nil string' do + let(:datetime) { nil } + it { is_expected.to be_nil } + end + end end