From d73116f0981a3cecc7b3315a997d83804aef6f4f Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:22:17 +0100 Subject: [PATCH] Memoise lettings forms in form handler (#1845) * feat: memoise lettings forms * feat: update tests --- app/models/form_handler.rb | 5 +++++ spec/models/form_spec.rb | 1 - spec/models/lettings_log_spec.rb | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 8cbd5a2e5..645efb1b4 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -75,6 +75,10 @@ class FormHandler end def lettings_forms + @lettings_forms ||= get_lettings_forms + end + + def get_lettings_forms forms = {} directories.each do |directory| Dir.glob("#{directory}/*.json").each do |form_path| @@ -154,6 +158,7 @@ class FormHandler def use_real_forms! @directories = ["config/forms"] + @lettings_forms = get_lettings_forms @forms = get_all_forms end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index c812f8172..5547d64ea 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -188,7 +188,6 @@ RSpec.describe Form, type: :model do around do |example| FormHandler.instance.use_real_forms! - example.run end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 489a31b9b..36c793c79 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1575,6 +1575,7 @@ RSpec.describe LettingsLog do it "clears underoccupation_benefitcap if log is no longer in 2021/22" do expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :underoccupation_benefitcap).to 2 Timecop.return + Singleton.__init__(FormHandler) expect { lettings_log.update!(startdate: Time.zone.local(2023, 1, 1)) }.to change(lettings_log, :underoccupation_benefitcap).from(2).to nil end @@ -2040,6 +2041,7 @@ RSpec.describe LettingsLog do before do Timecop.freeze(Time.zone.local(2022, 4, 2)) + Singleton.__init__(FormHandler) lettings_log.update!(startdate: Time.zone.local(2022, 4, 2), scheme:) Timecop.unfreeze end