Browse Source

Have form handler class decide the current form

pull/484/head
baarkerlounger 3 years ago
parent
commit
d55a028719
  1. 4
      app/models/form_handler.rb
  2. 2
      app/models/local_authority.rb
  3. 9
      spec/models/form_handler_spec.rb
  4. 5
      spec/models/local_authority_spec.rb

4
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

2
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

9
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)

5
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")

Loading…
Cancel
Save