Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract display date and time methods to a helper #3572

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/helpers/date_time_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DateTimeHelper
def self.display_date(timestamp, locale: I18n.locale, format: "%-d %B %Y")
I18n.l(Time.zone.parse(timestamp), format:, locale:) if timestamp
end

def self.display_date_and_time(date, rollback_midnight: false)
time = Time.zone.parse(date)
date_format = "%-e %B %Y"
time_format = "%l:%M%P"

if rollback_midnight && (time.strftime(time_format) == "12:00am")
# 12am, 12:00am and "midnight on" can all be misinterpreted
# Use 11:59pm on the day before to remove ambiguity
# 12am on 10 January becomes 11:59pm on 9 January
time -= 1.second
end
I18n.l(time, format: "#{time_format} on #{date_format}").gsub(":00", "").gsub("12pm", "midday").gsub("12am on ", "").strip
end
end
18 changes: 2 additions & 16 deletions app/presenters/call_for_evidence_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def closing_date_time
end

def opening_date
display_date_and_time(opening_date_time)
DateTimeHelper.display_date_and_time(opening_date_time)
end

def opening_date_midnight?
Time.zone.parse(opening_date_time).strftime("%l:%M%P") == "12:00am"
end

def closing_date
display_date_and_time(closing_date_time, rollback_midnight: true)
DateTimeHelper.display_date_and_time(closing_date_time, rollback_midnight: true)
end

def open?
Expand Down Expand Up @@ -102,20 +102,6 @@ def add_margin?

private

def display_date_and_time(date, rollback_midnight: false)
time = Time.zone.parse(date)
date_format = "%-e %B %Y"
time_format = "%l:%M%P"

if rollback_midnight && (time.strftime(time_format) == "12:00am")
# 12am, 12:00am and "midnight on" can all be misinterpreted
# Use 11:59pm on the day before to remove ambiguity
# 12am on 10 January becomes 11:59pm on 9 January
time -= 1.second
end
I18n.l(time, format: "#{time_format} on #{date_format}").gsub(":00", "").gsub("12pm", "midday").gsub("12am on ", "").strip
end

def ways_to_respond
content_item["details"]["ways_to_respond"]
end
Expand Down
18 changes: 2 additions & 16 deletions app/presenters/consultation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def closing_date_time
end

def opening_date
display_date_and_time(opening_date_time)
DateTimeHelper.display_date_and_time(opening_date_time)
end

def opening_date_midnight?
Time.zone.parse(opening_date_time).strftime("%l:%M%P") == "12:00am"
end

def closing_date
display_date_and_time(closing_date_time, rollback_midnight: true)
DateTimeHelper.display_date_and_time(closing_date_time, rollback_midnight: true)
end

def open?
Expand Down Expand Up @@ -116,20 +116,6 @@ def add_margin?

private

def display_date_and_time(date, rollback_midnight: false)
time = Time.zone.parse(date)
date_format = "%-e %B %Y"
time_format = "%l:%M%P"

if rollback_midnight && (time.strftime(time_format) == "12:00am")
# 12am, 12:00am and "midnight on" can all be misinterpreted
# Use 11:59pm on the day before to remove ambiguity
# 12am on 10 January becomes 11:59pm on 9 January
time -= 1.second
end
I18n.l(time, format: "#{time_format} on #{date_format}").gsub(":00", "").gsub("12pm", "midday").gsub("12am on ", "").strip
end

def ways_to_respond
content_item["details"]["ways_to_respond"]
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/content_item/last_updated.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ContentItem
module LastUpdated
def last_updated
display_date(content_item["public_updated_at"]) if content_item["public_updated_at"]
DateTimeHelper.display_date(content_item["public_updated_at"]) if content_item["public_updated_at"]
end
end
end
4 changes: 2 additions & 2 deletions app/presenters/content_item/manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def updated_metadata(updated_at)
current_path = view_context.request.path

if (hmrc? || manual?) && current_path == "#{base_path}/updates"
update_at_text = display_date(updated_at).to_s
update_at_text = DateTimeHelper.display_date(updated_at).to_s
else
updates_link = view_context.link_to(I18n.t("manuals.see_all_updates"), "#{base_path}/updates")
update_at_text = "#{display_date(updated_at)} - #{updates_link}"
update_at_text = "#{DateTimeHelper.display_date(updated_at)} - #{updates_link}"
end

{ I18n.t("manuals.updated") => update_at_text }
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/content_item/manual_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def parsed_content_item
end

def published
display_date(manual_content_item["first_published_at"])
DateTimeHelper.display_date(manual_content_item["first_published_at"])
end

def other_metadata
Expand Down
6 changes: 3 additions & 3 deletions app/presenters/content_item/updatable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module ContentItem
module Updatable
def published
display_date(first_public_at)
DateTimeHelper.display_date(first_public_at)
end

def updated
display_date(public_updated_at) if any_updates?
DateTimeHelper.display_date(public_updated_at) if any_updates?
end

def history
Expand All @@ -28,7 +28,7 @@ def change_history
changes = content_item["details"]["change_history"] || []
changes.map do |item|
{
display_time: display_date(item["public_timestamp"]),
display_time: DateTimeHelper.display_date(item["public_timestamp"]),
note: item["note"],
timestamp: item["public_timestamp"],
}
Expand Down
6 changes: 1 addition & 5 deletions app/presenters/content_item/withdrawable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ def withdrawal_notice_context

def withdrawal_notice_time
view_context.tag.time(
english_display_date(withdrawal_notice["withdrawn_at"]),
DateTimeHelper.display_date(withdrawal_notice["withdrawn_at"], locale: :en),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is a pain isn't it! I'll make a card on the navigation backlog to log that we need a translation for

def withdrawal_notice_title
"This #{withdrawal_notice_context.downcase} was withdrawn on #{withdrawal_notice_time}".html_safe
end

datetime: withdrawal_notice["withdrawn_at"],
)
end

def english_display_date(timestamp, format = "%-d %B %Y")
I18n.l(Time.zone.parse(timestamp), format:, locale: :en) if timestamp
end
end
end
4 changes: 0 additions & 4 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ def voting_is_open?
Time.zone.now < polls_closing_time
end

def display_date(timestamp, format = "%-d %B %Y")
I18n.l(Time.zone.parse(timestamp), format:, locale:) if timestamp
end

def sorted_locales(translations)
translations.sort_by { |t| t["locale"] == I18n.default_locale.to_s ? "" : t["locale"] }
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/html_publication_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def format_sub_type
end

def last_changed
timestamp = display_date(public_timestamp)
timestamp = DateTimeHelper.display_date(public_timestamp)

# This assumes that a translation doesn't need the date to come beforehand.
if content_item["details"]["first_published_version"]
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/specialist_document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def facets_with_values
end

def friendly_facet_date(dates)
dates.map { |date| display_date(date) }
dates.map { |date| DateTimeHelper.display_date(date) }
end

def friendly_facet_text(facet, values)
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/speech_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def location

def delivered_on
delivered_on_date = content_item["details"]["delivered_on"]
view_context.tag.time(display_date(delivered_on_date), datetime: delivered_on_date)
view_context.tag.time(DateTimeHelper.display_date(delivered_on_date), datetime: delivered_on_date)
end

def speech_type_explanation
Expand Down
2 changes: 1 addition & 1 deletion test/presenters/content_item/manual_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(schema_name = "manual")
"title" => "Super title",
"base_path" => "/a/base/path",
"public_updated_at" => "2022-03-23T08:30:20.000+00:00",
"first_published_at" => "2022-03-23T08:30:20.000+00:00",
"schema_name" => schema_name,
"details" => {
"body" => "body",
Expand Down Expand Up @@ -61,7 +62,6 @@ def initialize(schema_name = "manual")

test "returns extra publisher metadata" do
item = DummyContentItem.new
item.stubs(:display_date).returns("23 March 2022")

view_context = mock
view_context.stubs(:request).returns(ActionDispatch::TestRequest.create)
Expand Down
10 changes: 4 additions & 6 deletions test/presenters/content_item/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(schema_name = "manual")
"title" => "Super title",
"base_path" => "/a/base/path",
"public_updated_at" => "2022-03-23T08:30:20.000+00:00",
"first_published_at" => "2000-03-23T08:30:20.000+00:00",
"schema_name" => schema_name,
"details" => {
"body" => "body",
Expand All @@ -32,12 +33,11 @@ def initialize(schema_name = "manual")

test "returns see_updates_link true if published" do
item = DummyContentItem.new
item.stubs(:display_date).returns("23 March 2000")

expected_publisher_metadata = {
from: ["<a class=\"govuk-link\" href=\"/blah\">blah</a>"],
first_published: "23 March 2000",
last_updated: nil,
last_updated: "23 March 2022",
see_updates_link: true,
}

Expand All @@ -46,14 +46,12 @@ def initialize(schema_name = "manual")

test "does not return see_updates_link if pending" do
item = DummyContentItem.new
item.stubs(:display_date).returns("23 March 3000")

item.content_item["details"]["display_date"] = "23 March 3000"

expected_publisher_metadata = {
from: ["<a class=\"govuk-link\" href=\"/blah\">blah</a>"],
first_published: "23 March 3000",
last_updated: nil,
first_published: "23 March 2000",
last_updated: "23 March 2022",
}

assert_equal expected_publisher_metadata, item.publisher_metadata
Expand Down
25 changes: 9 additions & 16 deletions test/presenters/content_item/updatable_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
require "test_helper"

module ContentItemUpdatableStubs
def display_date(date)
date
end
end

module ContentItemUpdatableWithUpdates
def any_updates?
true
Expand All @@ -16,7 +10,6 @@ class ContentItemUpdatableTest < ActiveSupport::TestCase
def setup
@updatable = Object.new
@updatable.extend(ContentItem::Updatable)
@updatable.extend(ContentItemUpdatableStubs)
end

test "#history returns an empty array when there is no change history" do
Expand Down Expand Up @@ -49,7 +42,7 @@ def content_item
end

assert @updatable.history.any?
assert_equal @updatable.updated, "2002-02-02"
assert_equal "2 February 2002", @updatable.updated
end

test "#history returns no updates when first_public_at matches public_updated_at" do
Expand Down Expand Up @@ -133,14 +126,14 @@ def content_item
end
end

assert_equal @updatable.history,
[
{
display_time: "2016-02-29T09:24:10.000+00:00",
note: "notes",
timestamp: "2016-02-29T09:24:10.000+00:00",
},
]
expected_history = [
{
display_time: "29 February 2016",
note: "notes",
timestamp: "2016-02-29T09:24:10.000+00:00",
},
]
assert_equal expected_history, @updatable.history
end

test "#history returns a reverse chronologically sorted array of hashes when there is change history" do
Expand Down
4 changes: 0 additions & 4 deletions test/presenters/content_item/withdrawable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ def schema_name
"news_article"
end

def display_date(date)
date
end

def content_item
{
"title" => "Proportion of residents who do any walking or cycling (at local authority level) (CW010)",
Expand Down
Loading