Browse Source

Display all local authorities for location

pull/1402/head
Kat 3 years ago
parent
commit
c3767be7b2
  1. 24
      app/helpers/locations_helper.rb
  2. 8
      app/models/location.rb
  3. 46
      spec/helpers/locations_helper_spec.rb
  4. 14
      spec/models/location_spec.rb

24
app/helpers/locations_helper.rb

@ -27,11 +27,11 @@ module LocationsHelper
base_attributes = [ base_attributes = [
{ name: "Postcode", value: location.postcode, attribute: "postcode" }, { name: "Postcode", value: location.postcode, attribute: "postcode" },
{ name: "Location name", value: location.name, attribute: "name" }, { 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: "Number of units", value: location.units, attribute: "units" },
{ name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" }, { name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" },
{ name: "Mobility standards", value: location.mobility_type, attribute: "mobility_standards" }, { 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" }, { name: "Availability", value: location_availability(location), attribute: "availability" },
] ]
@ -46,7 +46,7 @@ module LocationsHelper
[ [
{ name: "Postcode", value: location.postcode, attribute: "postcode" }, { name: "Postcode", value: location.postcode, attribute: "postcode" },
{ name: "Location name", value: location.name, attribute: "name" }, { 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: "Number of units", value: location.units, attribute: "units" },
{ name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" }, { name: "Most common unit", value: location.type_of_unit, attribute: "type_of_unit" },
{ name: "Mobility standards", value: location.mobility_type, attribute: "mobility_standards" }, { 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) } [inner.deactivation_date, inner.reactivation_date].all? { |date| date.between?(outer.deactivation_date, outer.reactivation_date) }
end 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 end

8
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?) self.confirmed = [postcode, location_admin_district, location_code, units, type_of_unit, mobility_type].all?(&:present?)
end 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 private
PIO = PostcodeService.new PIO = PostcodeService.new

46
spec/helpers/locations_helper_spec.rb

@ -152,6 +152,52 @@ RSpec.describe LocationsHelper do
expect(display_location_attributes(location)).to eq(attributes) expect(display_location_attributes(location)).to eq(attributes)
end 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 "when viewing availability" do
context "with no deactivations" 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 it "displays previous collection start date as availability date if created_at is earlier than collection start date" do

14
spec/models/location_spec.rb

@ -1,12 +1,19 @@
require "rails_helper" require "rails_helper"
RSpec.describe Location, type: :model do 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 describe "#new" do
let(:location) { FactoryBot.build(:location) } let(:location) { FactoryBot.build(:location) }
before do before do
stub_request(:get, /api.postcodes.io/) stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) .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 end
it "belongs to an organisation" do it "belongs to an organisation" do
@ -18,6 +25,13 @@ RSpec.describe Location, type: :model do
location.save! location.save!
expect(location.location_code).to eq("E08000003") expect(location.location_code).to eq("E08000003")
end 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 end
describe "#postcode" do describe "#postcode" do

Loading…
Cancel
Save