From 685eeae0ecd3dd153adedcdfb38768902f391e96 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Mon, 24 Jan 2022 11:26:32 +0000 Subject: [PATCH] Dates and times should follow GDS style guide --- Gemfile.lock | 14 ++++----- app/models/form/question.rb | 2 +- app/views/case_logs/_log_list.html.erb | 2 +- app/views/organisations/users.html.erb | 2 +- config/initializers/date_formats.rb | 41 ++++++++++++++++++++++++++ spec/models/form/question_spec.rb | 2 +- 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 config/initializers/date_formats.rb diff --git a/Gemfile.lock b/Gemfile.lock index a3ce16ddf..ca004c0f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/activeadmin/arbre.git - revision: e4970e683c81c99432b73f0ceb5dff7d3fe50413 + revision: 305c221a2131be8dd0d96955f9b0a328dfa4beba specs: arbre (1.4.0) activesupport (>= 3.0.0, < 7.1) @@ -20,7 +20,7 @@ GIT GIT remote: https://github.com/tagliala/activeadmin.git - revision: 8ccc35b8144482284c90b0af74a01e940765f7a6 + revision: f4fc57251399c0ed452d72ac4b07d0bdd3d047c6 branch: feature/railties-7 specs: activeadmin (2.9.0) @@ -106,7 +106,7 @@ GEM ast (2.4.2) bcrypt (3.1.16) bindex (0.8.1) - bootsnap (1.10.1) + bootsnap (1.10.2) msgpack (~> 1.2) builder (3.2.4) byebug (11.1.3) @@ -158,7 +158,7 @@ GEM activemodel (>= 6.1) railties (>= 6.1) view_component (~> 2.47.0) - govuk_design_system_formbuilder (3.0.0) + govuk_design_system_formbuilder (3.0.1) actionview (>= 6.1) activemodel (>= 6.1) activesupport (>= 6.1) @@ -211,7 +211,7 @@ GEM method_source (1.0.0) mini_mime (1.1.2) minitest (5.15.0) - msgpack (1.4.3) + msgpack (1.4.4) net-imap (0.2.3) digest net-protocol @@ -242,7 +242,7 @@ GEM parallel (1.21.0) parser (3.1.0.0) ast (~> 2.4.1) - pg (1.2.3) + pg (1.3.0) postcodes_io (0.4.0) excon (~> 0.39) pry (0.13.1) @@ -461,4 +461,4 @@ RUBY VERSION ruby 3.0.3p157 BUNDLED WITH - 2.3.3 + 2.3.4 diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 278eed917..5b2e9d5c3 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -35,7 +35,7 @@ class Form::Question def answer_label(case_log) return checkbox_answer_label(case_log) if type == "checkbox" - return case_log[id].strftime("%d %b %Y") if type == "date" + return case_log[id].to_formatted_s(:govuk_date) if type == "date" return case_log[id].to_s if case_log[id].present? diff --git a/app/views/case_logs/_log_list.html.erb b/app/views/case_logs/_log_list.html.erb index 1549fef37..a509a68e5 100644 --- a/app/views/case_logs/_log_list.html.erb +++ b/app/views/case_logs/_log_list.html.erb @@ -21,7 +21,7 @@ <%= log.tenant_code %> - <%= log.updated_at.strftime("%d %b %Y") %> + <%= log.updated_at.to_formatted_s(:govuk_date) %> <% end %> diff --git a/app/views/organisations/users.html.erb b/app/views/organisations/users.html.erb index 238e76339..48b8e6846 100644 --- a/app/views/organisations/users.html.erb +++ b/app/views/organisations/users.html.erb @@ -18,7 +18,7 @@ <%= body.row do |row| row.cell(text: simple_format(user_cell(user), {}, wrapper_tag: "div")) row.cell(text: simple_format(org_cell(user), {}, wrapper_tag: "div")) - row.cell(text: user.last_sign_in_at&.strftime("%d %b %Y") ) + row.cell(text: user.last_sign_in_at&.to_formatted_s(:govuk_date) ) end %> <% end %> <% end %> diff --git a/config/initializers/date_formats.rb b/config/initializers/date_formats.rb new file mode 100644 index 000000000..59bc215ce --- /dev/null +++ b/config/initializers/date_formats.rb @@ -0,0 +1,41 @@ +Time::DATE_FORMATS[:govuk_date] = "%-d %B %Y" +Time::DATE_FORMATS[:govuk_date_short_month] = "%-d %b %Y" +Date::DATE_FORMATS[:govuk_date] = "%-d %B %Y" +Date::DATE_FORMATS[:govuk_date_short_month] = "%-d %b %Y" + +Time::DATE_FORMATS[:month_and_year] = "%B %Y" +Date::DATE_FORMATS[:month_and_year] = "%B %Y" + +Time::DATE_FORMATS[:short_month_and_year] = "%b %Y" +Date::DATE_FORMATS[:short_month_and_year] = "%b %Y" + +Time::DATE_FORMATS[:day_and_month] = "%-d %B" +Date::DATE_FORMATS[:day_and_month] = "%-d %B" + +Time::DATE_FORMATS[:govuk_date_and_time] = lambda do |time| + format = if time >= time.midday && time <= time.midday.end_of_minute + "%e %B %Y at %l%P (midday)" + elsif time >= time.midnight && time <= time.midnight.end_of_minute + "%e %B %Y at %l%P (midnight)" + elsif time.min.zero? + "%e %B %Y at %l%P" + else + "%e %B %Y at %l:%M%P" + end + + time.strftime(format).squish +end + +Time::DATE_FORMATS[:govuk_time] = lambda do |time| + format = if time >= time.midday && time <= time.midday.end_of_minute + "%l%P (midday)" + elsif time >= time.midnight && time <= time.midnight.end_of_minute + "%l%P (midnight)" + elsif time.min.zero? + "%l%P" + else + "%l:%M%P" + end + + time.strftime(format).squish +end diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 27933f60a..32bea6032 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -118,7 +118,7 @@ RSpec.describe Form::Question, type: :model do it "displays a formatted answer label" do case_log.mrcdate = Time.zone.local(2021, 10, 11) - expect(subject.answer_label(case_log)).to eq("11 Oct 2021") + expect(subject.answer_label(case_log)).to eq("11 October 2021") end end