From d9883c13a465384dff383a51ca61ef7ff7fde5c2 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 21 Apr 2022 11:22:22 +0100 Subject: [PATCH] Display LA name rather than code --- app/models/form.rb | 2 +- app/models/local_authority.rb | 5 +++++ app/models/organisation.rb | 8 ++++++-- spec/models/local_authority_spec.rb | 10 ++++++++++ spec/models/organisation_spec.rb | 8 ++++++-- 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 app/models/local_authority.rb create mode 100644 spec/models/local_authority_spec.rb diff --git a/app/models/form.rb b/app/models/form.rb index 3f1a83224..6beee8ec9 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -26,7 +26,7 @@ class Form def get_question(id, case_log) all_questions = questions.select { |q| q.id == id.to_s.underscore } - routed_question = all_questions.find { |q| q.page.routed_to?(case_log) } + routed_question = all_questions.find { |q| q.page.routed_to?(case_log) } if case_log routed_question || all_questions[0] end diff --git a/app/models/local_authority.rb b/app/models/local_authority.rb new file mode 100644 index 000000000..f0be218e4 --- /dev/null +++ b/app/models/local_authority.rb @@ -0,0 +1,5 @@ +class LocalAuthority + def self.ons_code_mappings + FormHandler.instance.forms["2021_2022"].get_question("la", nil).answer_options + end +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index a5151be71..85cf76028 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -37,7 +37,11 @@ class Organisation < ApplicationRecord end def local_authorities - organisation_las.pluck(:ons_code) + organisation_las.pluck(:ons_code).map { |ons_code| ons_code } + end + + def local_authority_names + local_authorities.map { |ons_code| LocalAuthority.ons_code_mappings[ons_code] } end def display_attributes @@ -46,7 +50,7 @@ class Organisation < ApplicationRecord { name: "address", value: address_string, editable: true }, { name: "telephone_number", value: phone, editable: true }, { name: "type", value: "Org type", editable: false }, - { name: "local_authorities_operated_in", value: local_authorities, editable: false, format: :bullet }, + { name: "local_authorities_operated_in", value: local_authority_names, editable: false, format: :bullet }, { name: "holds_own_stock", value: holds_own_stock.to_s.humanize, editable: false }, { name: "other_stock_owners", value: other_stock_owners, editable: false }, { name: "managing_agents", value: managing_agents, editable: false }, diff --git a/spec/models/local_authority_spec.rb b/spec/models/local_authority_spec.rb new file mode 100644 index 000000000..23c5c8447 --- /dev/null +++ b/spec/models/local_authority_spec.rb @@ -0,0 +1,10 @@ +require "rails_helper" + +RSpec.describe LocalAuthority, type: :model do + describe "ons code mapping" do + it "maps ONS code to local authority names" do + expect(described_class.ons_code_mappings).to be_a(Hash) + expect(described_class.ons_code_mappings["E07000178"]).to eq("Oxford") + end + end +end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 2c93414e3..a89ba1abf 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -31,11 +31,15 @@ RSpec.describe Organisation, type: :model do context "when the organisation only operates in specific local authorities" do before do - FactoryBot.create(:organisation_la, organisation_id: organisation.id) + FactoryBot.create(:organisation_la, organisation_id: organisation.id, ons_code: "E07000178") end it "has local authorities associated" do - expect(organisation.organisation_las.pluck(:ons_code)).to eq(%w[E07000041]) + expect(organisation.local_authorities).to eq(%w[E07000178]) + end + + it "maps the ons codes to LA names for display" do + expect(organisation.local_authority_names).to eq(%w[Oxford]) end end