Skip to content

Commit

Permalink
Add DateHelper and add new method in LocaleHelper
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
georges1996 committed Oct 9, 2024
1 parent d04f2ee commit 1cb231d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/helpers/date_helper.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/helpers/locale_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/helpers/date_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1cb231d

Please sign in to comment.