From 858fe6f7055bd8c8708f919950425e013dfd5879 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Fri, 20 May 2022 12:09:36 +0100 Subject: [PATCH] Perf (#592) * Memoize start year * Reset * Recalculate rather than reset --- app/models/case_log.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a03f377fd..0ec02b529 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -22,6 +22,7 @@ class CaseLog < ApplicationRecord has_paper_trail validates_with CaseLogValidator + before_validation :recalculate_start_year!, if: :startdate_changed? before_validation :process_postcode_changes!, if: :postcode_full_changed? before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed? before_validation :reset_invalidated_dependent_fields! @@ -66,10 +67,16 @@ class CaseLog < ApplicationRecord end def collection_start_year + return @start_year if @start_year return unless startdate window_end_date = Time.zone.local(startdate.year, 4, 1) - startdate < window_end_date ? startdate.year - 1 : startdate.year + @start_year = startdate < window_end_date ? startdate.year - 1 : startdate.year + end + + def recalculate_start_year! + @start_year = nil + collection_start_year end def form_name