From 1cb231d3b639b2bfe1033185108dbd60faf3e807 Mon Sep 17 00:00:00 2001 From: George Schena Date: Fri, 4 Oct 2024 07:37:52 +0100 Subject: [PATCH] Add DateHelper and add new method in LocaleHelper - Add new DateHelper to deal with the last updated date on help pages - Add page_text_direction to LocaleHelper Commit audit trail: - app/helpers/locale_helper.rb https://github.com/alphagov/government-frontend/blob/af54031948650448b965ac274c3248d23658c4f6/app/helpers/application_helper.rb - app/helpers/date_helper.rb https://github.com/alphagov/government-frontend/blob/af54031948650448b965ac274c3248d23658c4f6/app/presenters/content_item_presenter.rb --- app/helpers/date_helper.rb | 5 +++++ app/helpers/locale_helper.rb | 4 ++++ spec/helpers/date_helper_spec.rb | 15 +++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 app/helpers/date_helper.rb create mode 100644 spec/helpers/date_helper_spec.rb diff --git a/app/helpers/date_helper.rb b/app/helpers/date_helper.rb new file mode 100644 index 0000000000..9e50c365d6 --- /dev/null +++ b/app/helpers/date_helper.rb @@ -0,0 +1,5 @@ +module DateHelper + def display_date(timestamp, format = "%-d %B %Y") + I18n.l(Time.zone.parse(timestamp), format:, locale: "en") if timestamp + end +end diff --git a/app/helpers/locale_helper.rb b/app/helpers/locale_helper.rb index b1be3dd5ac..ac2e667537 100644 --- a/app/helpers/locale_helper.rb +++ b/app/helpers/locale_helper.rb @@ -2,4 +2,8 @@ module LocaleHelper def lang_attribute(locale) "lang=#{locale}" unless I18n.default_locale.to_s == locale.to_s end + + def page_text_direction + I18n.t("i18n.direction", locale: I18n.locale, default: "ltr") + end end diff --git a/spec/helpers/date_helper_spec.rb b/spec/helpers/date_helper_spec.rb new file mode 100644 index 0000000000..7a95fcf27e --- /dev/null +++ b/spec/helpers/date_helper_spec.rb @@ -0,0 +1,15 @@ +RSpec.describe DateHelper do + include DateHelper + + let(:timestamp) { "2024-10-03 19:30:22 +0100" } + + describe "#display_date" do + it "returns a formatted date" do + expect(display_date(timestamp)).to eq("3 October 2024") + end + + it "returns nil if passed nil" do + expect(display_date(nil)).to be_nil + end + end +end