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