From 2e8ecd75a919041caaf70056a0154171b70a7e11 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 3 Oct 2024 12:51:49 +0100 Subject: [PATCH] Dynamically display collection resources for years --- app/controllers/start_controller.rb | 66 ++++---- app/helpers/collection_resources_helper.rb | 35 ++++ .../layouts/_collection_resources.html.erb | 153 ++++++------------ .../collection_resources_helper_spec.rb | 92 +++++++++++ spec/requests/start_controller_spec.rb | 4 +- 5 files changed, 204 insertions(+), 146 deletions(-) diff --git a/app/controllers/start_controller.rb b/app/controllers/start_controller.rb index dd4232b7b..0c54013b2 100644 --- a/app/controllers/start_controller.rb +++ b/app/controllers/start_controller.rb @@ -1,5 +1,5 @@ class StartController < ApplicationController - include CollectionResourcesHelper + include CollectionTimeHelper def index if current_user @@ -8,62 +8,50 @@ class StartController < ApplicationController end end - def download_24_25_sales_form - download_resource("2024_25_sales_paper_form.pdf", "2024-25 Sales paper form.pdf") - end - - def download_23_24_sales_form - download_resource("2023_24_sales_paper_form.pdf", "2023-24 Sales paper form.pdf") - end + FormHandler.instance.years_of_available_lettings_forms.each do |year| + short_underscored_year = "#{year % 100}_#{(year + 1) % 100}" + underscored_year = "#{year}_#{(year + 1) % 100}" + dasherised_year = underscored_year.dasherize - def download_24_25_lettings_form - download_resource("2024_25_lettings_paper_form.pdf", "2024-25 Lettings paper form.pdf") - end + define_method "download_#{short_underscored_year}_lettings_form" do + download_resource("#{underscored_year}_lettings_paper_form.pdf", "#{dasherised_year}-lettings-paper-form.pdf") + end - def download_23_24_lettings_form - download_resource("2023_24_lettings_paper_form.pdf", "2023-24 Lettings paper form.pdf") - end + define_method "download_#{short_underscored_year}_lettings_bulk_upload_template" do + download_resource("bulk-upload-lettings-template-#{dasherised_year}.xlsx", "#{dasherised_year}-lettings-bulk-upload-template.xlsx") + end - def download_24_25_lettings_bulk_upload_template - download_resource("bulk-upload-lettings-template-2024-25.xlsx", "2024-25-lettings-bulk-upload-template.xlsx") + define_method "download_#{short_underscored_year}_lettings_bulk_upload_specification" do + download_resource("bulk-upload-lettings-specification-#{dasherised_year}.xlsx", "#{dasherised_year}-lettings-bulk-upload-specification.xlsx") + end end - def download_24_25_lettings_bulk_upload_specification - download_resource("bulk-upload-lettings-specification-2024-25.xlsx", "2024-25-lettings-bulk-upload-specification.xlsx") - end + FormHandler.instance.years_of_available_sales_forms.each do |year| + short_underscored_year = "#{year % 100}_#{(year + 1) % 100}" + underscored_year = "#{year}_#{(year + 1) % 100}" + dasherised_year = underscored_year.dasherize - def download_24_25_sales_bulk_upload_template - download_resource("bulk-upload-sales-template-2024-25.xlsx", "2024-25-sales-bulk-upload-template.xlsx") - end + define_method "download_#{short_underscored_year}_sales_form" do + download_resource("#{underscored_year}_sales_paper_form.pdf", "#{dasherised_year}-sales-paper-form.pdf") + end - def download_24_25_sales_bulk_upload_specification - download_resource("bulk-upload-sales-specification-2024-25.xlsx", "2024-25-sales-bulk-upload-specification.xlsx") - end + define_method "download_#{short_underscored_year}_sales_bulk_upload_template" do + download_resource("bulk-upload-sales-template-#{dasherised_year}.xlsx", "#{dasherised_year}-sales-bulk-upload-template.xlsx") + end - def download_23_24_lettings_bulk_upload_template - download_resource("bulk-upload-lettings-template-2023-24.xlsx", "2023-24-lettings-bulk-upload-template.xlsx") + define_method "download_#{short_underscored_year}_sales_bulk_upload_specification" do + download_resource("bulk-upload-sales-specification-#{dasherised_year}.xlsx", "#{dasherised_year}-sales-bulk-upload-specification.xlsx") + end end def download_23_24_lettings_bulk_upload_legacy_template download_resource("bulk-upload-lettings-legacy-template-2023-24.xlsx", "2023-24-lettings-bulk-upload-legacy-template.xlsx") end - def download_23_24_lettings_bulk_upload_specification - download_resource("bulk-upload-lettings-specification-2023-24.xlsx", "2023-24-lettings-bulk-upload-specification.xlsx") - end - - def download_23_24_sales_bulk_upload_template - download_resource("bulk-upload-sales-template-2023-24.xlsx", "2023-24-sales-bulk-upload-template.xlsx") - end - def download_23_24_sales_bulk_upload_legacy_template download_resource("bulk-upload-sales-legacy-template-2023-24.xlsx", "2023-24-sales-bulk-upload-legacy-template.xlsx") end - def download_23_24_sales_bulk_upload_specification - download_resource("bulk-upload-sales-specification-2023-24.xlsx", "2023-24-sales-bulk-upload-specification.xlsx") - end - private def download_resource(filename, download_filename) diff --git a/app/helpers/collection_resources_helper.rb b/app/helpers/collection_resources_helper.rb index 5ab539cde..2c4389c38 100644 --- a/app/helpers/collection_resources_helper.rb +++ b/app/helpers/collection_resources_helper.rb @@ -1,4 +1,6 @@ module CollectionResourcesHelper + include CollectionTimeHelper + HUMAN_READABLE_CONTENT_TYPE = { "application/pdf": "PDF", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "Microsoft Excel", "application/vnd.ms-excel": "Microsoft Excel (Old Format)", @@ -19,4 +21,37 @@ module CollectionResourcesHelper file_type = HUMAN_READABLE_CONTENT_TYPE[metadata["content_type"].to_sym] || "Unknown File Type" [file_type, file_size, file_pages].compact.join(", ") end + + def displayed_collection_resource_years + return [previous_collection_start_year, current_collection_start_year] if FormHandler.instance.in_edit_crossover_period? + + [current_collection_start_year] + end + + def editable_collection_resource_years + return [previous_collection_start_year, current_collection_start_year] if FormHandler.instance.in_edit_crossover_period? + return [current_collection_start_year, next_collection_start_year] if Time.zone.today >= Time.zone.local(Time.zone.today.year, 1, 1) && Time.zone.today < Time.zone.local(Time.zone.today.year, 4, 1) + + [current_collection_start_year] + end + + def year_range_format(year) + "#{year % 100}/#{(year + 1) % 100}" + end + + def text_year_range_format(year) + "#{year} to #{year + 1}" + end + + def underscored_file_year_format(year) + "#{year}_#{(year + 1) % 100}" + end + + def dasherised_file_year_format(year) + underscored_file_year_format(year).dasherize + end + + def short_underscored_year_range_format(year) + "#{year % 100}_#{(year + 1) % 100}" + end end diff --git a/app/views/layouts/_collection_resources.html.erb b/app/views/layouts/_collection_resources.html.erb index 48976088a..c49dfe00a 100644 --- a/app/views/layouts/_collection_resources.html.erb +++ b/app/views/layouts/_collection_resources.html.erb @@ -5,113 +5,56 @@ <% else %>

Collection resources

<% end %> -

Use the 2024 to 2025 forms for lettings that start and sales that complete between 1 April 2024 and 31 March 2025.

-<% if FormHandler.instance.lettings_form_for_start_year(2023) && FormHandler.instance.lettings_form_for_start_year(2023).edit_end_date > Time.zone.today %> -

Use the 2023 to 2024 forms for lettings that start and sales that complete between 1 April 2023 and 31 March 2024.

+<% collection_resource_years.each do |collection_start_year| %> +

Use the <%= collection_start_year %> to <%= collection_start_year + 1 %> forms for lettings that start and sales that complete between 1 April <%= collection_start_year %> and 31 March <%= collection_start_year + 1 %>.

<% end %>
-<%= govuk_tabs(title: "Collection resources", classes: %w[app-tab__small-headers]) do |c| %> - <% if FormHandler.instance.lettings_form_for_start_year(2024) && FormHandler.instance.lettings_form_for_start_year(2024).edit_end_date > Time.zone.today %> - <% c.with_tab(label: "Lettings 24/25") do %> - <%= render DocumentListComponent.new( - items: [ - { - name: "Download the lettings log for tenants (2024 to 2025)", - href: download_24_25_lettings_form_path, - metadata: file_type_size_and_pages("2024_25_lettings_paper_form.pdf", number_of_pages: 8), - }, - { - name: "Download the lettings bulk upload template (2024 to 2025)", - href: download_24_25_lettings_bulk_upload_template_path, - metadata: file_type_size_and_pages("bulk-upload-lettings-template-2024-25.xlsx"), - }, - { - name: "Download the lettings bulk upload specification (2024 to 2025)", - href: download_24_25_lettings_bulk_upload_specification_path, - metadata: file_type_size_and_pages("bulk-upload-lettings-specification-2024-25.xlsx"), - }, - ], - label: "Lettings 2024 to 2025", - ) %> - <% end %> - <% c.with_tab(label: "Sales 24/25") do %> - <%= render DocumentListComponent.new( - items: [ - { - name: "Download the sales log for buyers (2024 to 2025)", - href: download_24_25_sales_form_path, - metadata: file_type_size_and_pages("2024_25_sales_paper_form.pdf", number_of_pages: 8), - }, - { - name: "Download the sales bulk upload template (2024 to 2025)", - href: download_24_25_sales_bulk_upload_template_path, - metadata: file_type_size_and_pages("bulk-upload-sales-template-2024-25.xlsx"), - }, - { - name: "Download the sales bulk upload specification (2024 to 2025)", - href: download_24_25_sales_bulk_upload_specification_path, - metadata: file_type_size_and_pages("bulk-upload-sales-specification-2024-25.xlsx"), - }, - ], - label: "Sales 2024 to 2025", - ) %> + <%= govuk_tabs(title: "Collection resources", classes: %w[app-tab__small-headers]) do |c| %> + <% collection_resource_years.each do |collection_start_year| %> + <% c.with_tab(label: "Lettings #{year_range_format(collection_start_year)}") do %> + <%= render DocumentListComponent.new( + items: [ + { + name: "Download the lettings log for tenants (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_lettings_form_path"), + metadata: file_type_size_and_pages("#{underscored_file_year_format(collection_start_year)}_lettings_paper_form.pdf", number_of_pages: 8), + }, + { + name: "Download the lettings bulk upload template (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_lettings_bulk_upload_template_path"), + metadata: file_type_size_and_pages("bulk-upload-lettings-template-#{dasherised_file_year_format(collection_start_year)}.xlsx"), + }, + { + name: "Download the lettings bulk upload specification (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_lettings_bulk_upload_specification_path"), + metadata: file_type_size_and_pages("bulk-upload-lettings-specification-#{dasherised_file_year_format(collection_start_year)}.xlsx"), + }, + ], + label: "Lettings #{text_year_range_format(collection_start_year)}", + ) %> + <% end %> + <% c.with_tab(label: "Sales #{year_range_format(collection_start_year)}") do %> + <%= render DocumentListComponent.new( + items: [ + { + name: "Download the sales log for buyers (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_sales_form_path"), + metadata: file_type_size_and_pages("#{underscored_file_year_format(collection_start_year)}_sales_paper_form.pdf", number_of_pages: 8), + }, + { + name: "Download the sales bulk upload template (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_sales_bulk_upload_template_path"), + metadata: file_type_size_and_pages("bulk-upload-sales-template-#{dasherised_file_year_format(collection_start_year)}.xlsx"), + }, + { + name: "Download the sales bulk upload specification (#{text_year_range_format(collection_start_year)})", + href: send("download_#{short_underscored_year_range_format(collection_start_year)}_sales_bulk_upload_specification_path"), + metadata: file_type_size_and_pages("bulk-upload-sales-specification-#{dasherised_file_year_format(collection_start_year)}.xlsx"), + }, + ], + label: "Sales #{text_year_range_format(collection_start_year)}", + ) %> + <% end %> <% end %> <% end %> - <% if FormHandler.instance.lettings_form_for_start_year(2023) && FormHandler.instance.lettings_form_for_start_year(2023).edit_end_date > Time.zone.today %> - <% c.with_tab(label: "Lettings 23/24") do %> - <%= render DocumentListComponent.new( - items: [ - { - name: "Download the lettings log for tenants (2023 to 2024)", - href: download_23_24_lettings_form_path, - metadata: file_type_size_and_pages("2023_24_lettings_paper_form.pdf", number_of_pages: 8), - }, - { - name: "Download the lettings bulk upload template (2023 to 2024) – New question ordering", - href: download_23_24_lettings_bulk_upload_template_path, - metadata: file_type_size_and_pages("bulk-upload-lettings-template-2023-24.xlsx"), - }, - { - name: "Download the lettings bulk upload template (2023 to 2024) – Legacy version", - href: download_23_24_lettings_bulk_upload_legacy_template_path, - metadata: file_type_size_and_pages("bulk-upload-lettings-legacy-template-2023-24.xlsx"), - }, - { - name: "Download the lettings bulk upload specification (2023 to 2024)", - href: download_23_24_lettings_bulk_upload_specification_path, - metadata: file_type_size_and_pages("bulk-upload-lettings-specification-2023-24.xlsx"), - }, - ], - label: "Lettings 2023 to 2024", - ) %> - <% end %> - <% c.with_tab(label: "Sales 23/24") do %> - <%= render DocumentListComponent.new( - items: [ - { - name: "Download the sales log for buyers (2023 to 2024)", - href: download_23_24_sales_form_path, - metadata: file_type_size_and_pages("2023_24_sales_paper_form.pdf", number_of_pages: 8), - }, - { - name: "Download the sales bulk upload template (2023 to 2024) – New question ordering", - href: download_23_24_sales_bulk_upload_template_path, - metadata: file_type_size_and_pages("bulk-upload-sales-template-2023-24.xlsx"), - }, - { - name: "Download the sales bulk upload template (2023 to 2024) – Legacy version", - href: download_23_24_sales_bulk_upload_legacy_template_path, - metadata: file_type_size_and_pages("bulk-upload-sales-legacy-template-2023-24.xlsx"), - }, - { - name: "Download the sales bulk upload specification (2023 to 2024)", - href: download_23_24_sales_bulk_upload_specification_path, - metadata: file_type_size_and_pages("bulk-upload-sales-specification-2023-24.xlsx"), - }, - ], - label: "Sales 2023 to 2024", - ) %> - <% end %> - <% end %> -<% end %>
diff --git a/spec/helpers/collection_resources_helper_spec.rb b/spec/helpers/collection_resources_helper_spec.rb index 4d39a0c2d..ce2f8fc64 100644 --- a/spec/helpers/collection_resources_helper_spec.rb +++ b/spec/helpers/collection_resources_helper_spec.rb @@ -31,4 +31,96 @@ RSpec.describe CollectionResourcesHelper do end end end + + describe "#editable_collection_resource_years" do + context "when in crossover period" do + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) + allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) + end + + it "returns previous and current years" do + expect(editable_collection_resource_years).to eq([2023, 2024]) + end + end + + context "when not in crossover period" do + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) + end + + context "and after 1st January" do + before do + allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 2, 1)) + end + + it "returns current and next years" do + expect(editable_collection_resource_years).to eq([2024, 2025]) + end + end + + context "and before 1st January" do + before do + allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 12, 1)) + end + + it "returns current year" do + expect(editable_collection_resource_years).to eq([2024]) + end + end + end + end + + describe "#displayed_collection_resource_years" do + context "when in crossover period" do + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) + allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) + end + + it "returns previous and current years" do + expect(displayed_collection_resource_years).to eq([2023, 2024]) + end + end + + context "when not in crossover period" do + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) + end + + it "returns current year" do + expect(displayed_collection_resource_years).to eq([2024]) + end + end + end + + describe "#year_range_format" do + it "returns formatted year range" do + expect(year_range_format(2023)).to eq("23/24") + end + end + + describe "#text_year_range_format" do + it "returns formatted text year range" do + expect(text_year_range_format(2023)).to eq("2023 to 2024") + end + end + + describe "#underscored_file_year_format" do + it "returns formatted dasherised file year" do + expect(underscored_file_year_format(2023)).to eq("2023_24") + end + end + + describe "#dasherised_file_year_format" do + it "returns formatted dasherised file year" do + expect(dasherised_file_year_format(2023)).to eq("2023-24") + end + end + + describe "#short_underscored_year_range_format" do + it "returns formatted short underscored year range" do + expect(short_underscored_year_range_format(2023)).to eq("23_24") + end + end end diff --git a/spec/requests/start_controller_spec.rb b/spec/requests/start_controller_spec.rb index 40c45a9f9..f7503f2bd 100644 --- a/spec/requests/start_controller_spec.rb +++ b/spec/requests/start_controller_spec.rb @@ -342,10 +342,10 @@ RSpec.describe StartController, type: :request do context "and 2023 collection window is closed for editing" do before do - allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 1)) + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 12, 1)) end - it "displays correct resources for 2023/24 and 2024/25 collection years" do + it "displays correct resources" do get root_path expect(page).to have_content("Lettings 24/25") expect(page).not_to have_content("Lettings 23/24")