diff --git a/app/helpers/collection_time_helper.rb b/app/helpers/collection_time_helper.rb index 23014b8d1..b9dee7dcc 100644 --- a/app/helpers/collection_time_helper.rb +++ b/app/helpers/collection_time_helper.rb @@ -12,6 +12,11 @@ module CollectionTimeHelper Time.zone.local(collection_start_year(date), 4, 1) end + def date_mid_collection_year_formatted(date) + example_date = date.nil? ? Time.zone.today : collection_start_date(date).to_date + 5.months + example_date.to_formatted_s(:govuk_date_number_month) + end + def current_collection_start_date Time.zone.local(current_collection_start_year, 4, 1) end diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index 153679ace..167e02a31 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -3,7 +3,7 @@ <%= f.govuk_date_field question.id.to_sym, caption: caption(caption_text, page_header, conditional), legend: legend(question, page_header, conditional), - hint: { text: question.hint_text&.html_safe || "For example, 1 9 2022." }, + hint: { text: question.hint_text&.html_safe || "For example, #{date_mid_collection_year_formatted(lettings_log.startdate)}" }, width: 20, **stimulus_html_attributes(question) do %> <%= govuk_inset_text(text: question.unresolved_hint_text) if question.unresolved_hint_text.present? && @log.unresolved %> diff --git a/config/initializers/date_formats.rb b/config/initializers/date_formats.rb index 59bc215ce..71179f6d2 100644 --- a/config/initializers/date_formats.rb +++ b/config/initializers/date_formats.rb @@ -2,6 +2,7 @@ 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" +Date::DATE_FORMATS[:govuk_date_number_month] = "%-d %-m %Y" Time::DATE_FORMATS[:month_and_year] = "%B %Y" Date::DATE_FORMATS[:month_and_year] = "%B %Y" diff --git a/spec/helpers/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb index c2eb2fedb..0c187020e 100644 --- a/spec/helpers/collection_time_helper_spec.rb +++ b/spec/helpers/collection_time_helper_spec.rb @@ -4,13 +4,13 @@ RSpec.describe CollectionTimeHelper do let(:current_user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) } - around do |example| - Timecop.freeze(now) do - example.run + describe "Current collection start year" do + around do |example| + Timecop.freeze(now) do + example.run + end end - end - describe "Current collection start year" do context "when the date is after 1st of April" do let(:now) { Time.utc(2022, 8, 3) } @@ -77,4 +77,35 @@ RSpec.describe CollectionTimeHelper do end end end + + describe "#date_mid_collection_year_formatted" do + subject(:result) { date_mid_collection_year_formatted(input) } + + context "when called with nil" do + let(:input) { nil } + + it "returns the current date" do + today = Time.zone.today + expect(result).to eq("#{today.day} #{today.month} #{today.year}") + end + end + + context "when called with a date after the first of April" do + calendar_year = 2030 + let(:input) { Date.new(calendar_year, 7, 7) } + + it "returns the first of September from that year" do + expect(result).to eq("1 9 #{calendar_year}") + end + end + + context "when called with a date before April" do + calendar_year = 2040 + let(:input) { Date.new(calendar_year, 2, 7) } + + it "returns the first of September from the previous year" do + expect(result).to eq("1 9 #{calendar_year - 1}") + end + end + end end