From 98cbe4ad454101a03fa22830ef6a5275a8bd5483 Mon Sep 17 00:00:00 2001 From: Jack S Date: Mon, 17 Apr 2023 12:29:37 +0100 Subject: [PATCH] Add older_than_previous_collection_year helper method --- app/models/log.rb | 11 +++++++ spec/models/lettings_log_spec.rb | 51 ++++++++++++++++++++++++++++++++ spec/models/sales_log_spec.rb | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/app/models/log.rb b/app/models/log.rb index 96896bf3d..13f7a5b5b 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -1,4 +1,6 @@ class Log < ApplicationRecord + include CollectionTimeHelper + self.abstract_class = true belongs_to :owning_organisation, class_name: "Organisation", optional: true @@ -87,6 +89,8 @@ class Log < ApplicationRecord end def collection_period_open? + return false if older_than_previous_collection_year? + form.end_date > Time.zone.today end @@ -133,6 +137,13 @@ class Log < ApplicationRecord private + # Handle logs that are older than previous collection start date + def older_than_previous_collection_year? + return false unless startdate + + startdate < previous_collection_start_date + end + def plural_gender_for_person(person_num) gender = public_send("sex#{person_num}".to_sym) return unless gender diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 1e6fd3244..6f2949ba6 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1,6 +1,7 @@ require "rails_helper" require "shared/shared_examples_for_derived_fields" +# rubocop:disable RSpec/MessageChain # rubocop:disable RSpec/AnyInstance RSpec.describe LettingsLog do let(:different_managing_organisation) { create(:organisation) } @@ -3112,5 +3113,55 @@ RSpec.describe LettingsLog do end end end + + describe "#collection_period_open?" do + let(:log) { build(:lettings_log, startdate:) } + + context "when startdate is nil" do + let(:startdate) { nil } + + it "returns false" do + expect(log.collection_period_open?).to eq(true) + end + end + + context "when older_than_previous_collection_year" do + let(:previous_collection_start_date) { Time.zone.local(2050, 4, 1) } + let(:startdate) { previous_collection_start_date - 1.day } + + before do + allow(log).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + end + + it "returns true" do + expect(log.collection_period_open?).to eq(false) + end + end + + context "when form end date is in the future" do + let(:startdate) { nil } + + before do + allow(log).to receive_message_chain(:form, :end_date).and_return(Time.zone.now + 1.day) + end + + it "returns true" do + expect(log.collection_period_open?).to eq(true) + end + end + + context "when form end date is in the past" do + let(:startdate) { Time.zone.local(2020, 4, 1) } + + before do + allow(log).to receive_message_chain(:form, :end_date).and_return(Time.zone.now - 1.day) + end + + it "returns false" do + expect(log.collection_period_open?).to eq(false) + end + end + end end # rubocop:enable RSpec/AnyInstance +# rubocop:enable RSpec/MessageChain diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index e9acd4548..27736bbf5 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -1,6 +1,7 @@ require "rails_helper" require "shared/shared_examples_for_derived_fields" +# rubocop:disable RSpec/MessageChain # rubocop:disable RSpec/AnyInstance RSpec.describe SalesLog, type: :model do let(:owning_organisation) { create(:organisation) } @@ -607,5 +608,55 @@ RSpec.describe SalesLog, type: :model do end end end + + describe "#collection_period_open?" do + let(:log) { build(:sales_log, saledate:) } + + context "when saledate is nil" do + let(:saledate) { nil } + + it "returns false" do + expect(log.collection_period_open?).to eq(true) + end + end + + context "when older_than_previous_collection_year" do + let(:previous_collection_start_date) { Time.zone.local(2050, 4, 1) } + let(:saledate) { previous_collection_start_date - 1.day } + + before do + allow(log).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + end + + it "returns true" do + expect(log.collection_period_open?).to eq(false) + end + end + + context "when form end date is in the future" do + let(:saledate) { nil } + + before do + allow(log).to receive_message_chain(:form, :end_date).and_return(Time.zone.now + 1.day) + end + + it "returns true" do + expect(log.collection_period_open?).to eq(true) + end + end + + context "when form end date is in the past" do + let(:saledate) { Time.zone.local(2020, 4, 1) } + + before do + allow(log).to receive_message_chain(:form, :end_date).and_return(Time.zone.now - 1.day) + end + + it "returns false" do + expect(log.collection_period_open?).to eq(false) + end + end + end end # rubocop:enable RSpec/AnyInstance +# rubocop:enable RSpec/MessageChain