From 16c9c3d218b3c376fb026ef9070bf9c2c5f00c87 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 10 Mar 2023 12:25:53 +0000 Subject: [PATCH] Format local authorities for locations --- app/helpers/locations_helper.rb | 25 +++++++++---------------- spec/helpers/locations_helper_spec.rb | 4 ++-- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 579d3bdb5..092776b23 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -27,11 +27,11 @@ module LocationsHelper base_attributes = [ { name: "Postcode", value: location.postcode, attribute: "postcode" }, { name: "Location name", value: location.name, attribute: "name" }, - { name: "Local authority", value: location_admin_districts(location), attribute: "local_authority" }, + { name: "Local authority", value: formatted_local_authority_timeline(location, "name"), attribute: "local_authority" }, { name: "Number of units", value: location.units, attribute: "units" }, { name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" }, { name: "Mobility standards", value: location.mobility_type, attribute: "mobility_standards" }, - { name: "Location code", value: location_codes(location), attribute: "location_code" }, + { name: "Location code", value: formatted_local_authority_timeline(location, "code"), attribute: "location_code" }, { name: "Availability", value: location_availability(location), attribute: "availability" }, ] @@ -46,7 +46,7 @@ module LocationsHelper [ { name: "Postcode", value: location.postcode, attribute: "postcode" }, { name: "Location name", value: location.name, attribute: "name" }, - { name: "Local authority", value: location_admin_districts(location), attribute: "local_authority" }, + { name: "Local authority", value: formatted_local_authority_timeline(location, "name"), attribute: "local_authority" }, { name: "Number of units", value: location.units, attribute: "units" }, { name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" }, { name: "Mobility standards", value: location.mobility_type, attribute: "mobility_standards" }, @@ -108,21 +108,14 @@ private [inner.deactivation_date, inner.reactivation_date].all? { |date| date.between?(outer.deactivation_date, outer.reactivation_date) } end - def location_codes(location) + def formatted_local_authority_timeline(location, field) sorted_linked_authorities = location.linked_local_authorities.sort_by(&:start_date) - return sorted_linked_authorities.first.code if sorted_linked_authorities.count == 1 + return sorted_linked_authorities.first[field] if sorted_linked_authorities.count == 1 sorted_linked_authorities.map { |linked_local_authority| - "#{linked_local_authority.code} (#{linked_local_authority.start_date.year})" - }.join(", ") - end - - def location_admin_districts(location) - sorted_linked_authorities = location.linked_local_authorities.sort_by(&:start_date) - return sorted_linked_authorities.first.name if sorted_linked_authorities.count == 1 - - sorted_linked_authorities.map { |linked_local_authority| - "#{linked_local_authority.name} (#{linked_local_authority.start_date.year})" - }.join(", ") + formatted_start_date = linked_local_authority.start_date&.to_formatted_s(:govuk_date) + formatted_end_date = linked_local_authority.end_date&.to_formatted_s(:govuk_date) + "#{linked_local_authority[field]} (#{formatted_start_date} - #{formatted_end_date || 'present'})" + }.join("\n") end end diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 13c3ed903..7c65cb536 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -162,11 +162,11 @@ RSpec.describe LocationsHelper do attributes = [ { attribute: "postcode", name: "Postcode", value: location.postcode }, { attribute: "name", name: "Location name", value: location.name }, - { attribute: "local_authority", name: "Local authority", value: "Eden (2021), Cumberland (2023)" }, + { attribute: "local_authority", name: "Local authority", value: "Eden (1 April 2021 - 31 March 2023)\nCumberland (1 April 2023 - present)" }, { attribute: "units", name: "Number of units", value: location.units }, { attribute: "type_of_unit", name: "Most common unit", value: location.type_of_unit }, { attribute: "mobility_standards", name: "Mobility standards", value: location.mobility_type }, - { attribute: "location_code", name: "Location code", value: "E07000030 (2021), E06000063 (2023)" }, + { attribute: "location_code", name: "Location code", value: "E07000030 (1 April 2021 - 31 March 2023)\nE06000063 (1 April 2023 - present)" }, { attribute: "availability", name: "Availability", value: "Active from 1 April 2022" }, { attribute: "status", name: "Status", value: :active }, ]