From c3767be7b2de96f6404e75f96a1eafdaa5883b80 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 10 Mar 2023 11:17:00 +0000 Subject: [PATCH] Display all local authorities for location --- app/helpers/locations_helper.rb | 24 ++++++++++++-- app/models/location.rb | 8 +++++ spec/helpers/locations_helper_spec.rb | 46 +++++++++++++++++++++++++++ spec/models/location_spec.rb | 14 ++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 8d6a46c26..579d3bdb5 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.location_admin_district, attribute: "local_authority" }, + { name: "Local authority", value: location_admin_districts(location), 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.location_code, attribute: "location_code" }, + { name: "Location code", value: location_codes(location), 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.location_admin_district, attribute: "local_authority" }, + { name: "Local authority", value: location_admin_districts(location), 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" }, @@ -107,4 +107,22 @@ private [inner.deactivation_date, inner.reactivation_date].all? { |date| date.between?(outer.deactivation_date, outer.reactivation_date) } end + + def location_codes(location) + sorted_linked_authorities = location.linked_local_authorities.sort_by(&:start_date) + return sorted_linked_authorities.first.code 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(", ") + end end diff --git a/app/models/location.rb b/app/models/location.rb index 0f90aedf5..17318147c 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -130,6 +130,14 @@ class Location < ApplicationRecord self.confirmed = [postcode, location_admin_district, location_code, units, type_of_unit, mobility_type].all?(&:present?) end + def linked_local_authorities + la = LocalAuthority.find_by(code: location_code) + return [] unless la + + all_las = [la] + la.linked_local_authorities + LocalAuthority.where(id: all_las.map(&:id)) + end + private PIO = PostcodeService.new diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 4d58a7e8f..13c3ed903 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -152,6 +152,52 @@ RSpec.describe LocationsHelper do expect(display_location_attributes(location)).to eq(attributes) end + context "when location has different local authorities for different years" do + before do + LocalAuthorityLink.create!(local_authority_id: LocalAuthority.find_by(code: "E07000030").id, linked_local_authority_id: LocalAuthority.find_by(code: "E06000063").id) + location.update!(location_code: "E07000030") + end + + it "returns correct display attributes" 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: "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: "availability", name: "Availability", value: "Active from 1 April 2022" }, + { attribute: "status", name: "Status", value: :active }, + ] + + expect(display_location_attributes(location)).to eq(attributes) + end + end + + context "when location has no local authority" do + before do + LocalAuthorityLink.create!(local_authority_id: LocalAuthority.find_by(code: "E07000030").id, linked_local_authority_id: LocalAuthority.find_by(code: "E06000063").id) + location.update!(location_code: nil) + end + + it "returns correct display attributes" do + attributes = [ + { attribute: "postcode", name: "Postcode", value: location.postcode }, + { attribute: "name", name: "Location name", value: location.name }, + { attribute: "local_authority", name: "Local authority", value: "" }, + { 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: "" }, + { attribute: "availability", name: "Availability", value: "Active from 1 April 2022" }, + { attribute: "status", name: "Status", value: :incomplete }, + ] + + expect(display_location_attributes(location)).to eq(attributes) + end + end + context "when viewing availability" do context "with no deactivations" do it "displays previous collection start date as availability date if created_at is earlier than collection start date" do diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 8d1284fa5..ceb87121f 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -1,12 +1,19 @@ require "rails_helper" RSpec.describe Location, type: :model do + before do + LocalAuthorityLink.create(local_authority_id: LocalAuthority.find_by(code: "E07000030").id, linked_local_authority_id: LocalAuthority.find_by(code: "E06000063").id) + end + describe "#new" do let(:location) { FactoryBot.build(:location) } before do stub_request(:get, /api.postcodes.io/) .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) + + stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {}) end it "belongs to an organisation" do @@ -18,6 +25,13 @@ RSpec.describe Location, type: :model do location.save! expect(location.location_code).to eq("E08000003") end + + it "infers and returns the list of local authorities" do + location.update!(postcode: "CA10 1AA") + expect(location.linked_local_authorities.count).to eq(2) + expect(location.linked_local_authorities.active(Time.zone.local(2022, 4, 1)).first.code).to eq("E07000030") + expect(location.linked_local_authorities.active(Time.zone.local(2023, 4, 1)).first.code).to eq("E06000063") + end end describe "#postcode" do