From d55a028719d79e7bb00e32c1efc75c4cc237a4b7 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 21 Apr 2022 11:36:18 +0100 Subject: [PATCH] Have form handler class decide the current form --- app/models/form_handler.rb | 4 ++++ app/models/local_authority.rb | 2 +- spec/models/form_handler_spec.rb | 9 +++++++++ spec/models/local_authority_spec.rb | 5 +++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 8228077ca..dfb2b7266 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -10,6 +10,10 @@ class FormHandler @forms[form] end + def current_form + forms[forms.keys.sort { |a, b| a.to_i <=> b.to_i }.last] + end + private def get_all_forms diff --git a/app/models/local_authority.rb b/app/models/local_authority.rb index f0be218e4..bced9a41f 100644 --- a/app/models/local_authority.rb +++ b/app/models/local_authority.rb @@ -1,5 +1,5 @@ class LocalAuthority def self.ons_code_mappings - FormHandler.instance.forms["2021_2022"].get_question("la", nil).answer_options + FormHandler.instance.current_form.get_question("la", nil).answer_options end end diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 4accfa6c5..fb793d210 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -21,6 +21,15 @@ RSpec.describe FormHandler do end end + describe "Current form" do + it "returns the latest form by date" do + form_handler = described_class.instance + form = form_handler.current_form + expect(form).to be_a(Form) + expect(form.start_date.year).to eq(2022) + end + end + it "loads the form once at boot time" do form_handler = described_class.instance expect(Form).not_to receive(:new).with(:any, test_form_name) diff --git a/spec/models/local_authority_spec.rb b/spec/models/local_authority_spec.rb index 23c5c8447..a2bceb751 100644 --- a/spec/models/local_authority_spec.rb +++ b/spec/models/local_authority_spec.rb @@ -2,6 +2,11 @@ require "rails_helper" RSpec.describe LocalAuthority, type: :model do describe "ons code mapping" do + let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") } + before do + allow(FormHandler.instance).to receive(:current_form).and_return(form) + end + 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")