Browse Source

Add older_than_previous_collection_year helper method

pull/1551/head
Jack S 3 years ago
parent
commit
98cbe4ad45
  1. 11
      app/models/log.rb
  2. 51
      spec/models/lettings_log_spec.rb
  3. 51
      spec/models/sales_log_spec.rb

11
app/models/log.rb

@ -1,4 +1,6 @@
class Log < ApplicationRecord class Log < ApplicationRecord
include CollectionTimeHelper
self.abstract_class = true self.abstract_class = true
belongs_to :owning_organisation, class_name: "Organisation", optional: true belongs_to :owning_organisation, class_name: "Organisation", optional: true
@ -87,6 +89,8 @@ class Log < ApplicationRecord
end end
def collection_period_open? def collection_period_open?
return false if older_than_previous_collection_year?
form.end_date > Time.zone.today form.end_date > Time.zone.today
end end
@ -133,6 +137,13 @@ class Log < ApplicationRecord
private 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) def plural_gender_for_person(person_num)
gender = public_send("sex#{person_num}".to_sym) gender = public_send("sex#{person_num}".to_sym)
return unless gender return unless gender

51
spec/models/lettings_log_spec.rb

@ -1,6 +1,7 @@
require "rails_helper" require "rails_helper"
require "shared/shared_examples_for_derived_fields" require "shared/shared_examples_for_derived_fields"
# rubocop:disable RSpec/MessageChain
# rubocop:disable RSpec/AnyInstance # rubocop:disable RSpec/AnyInstance
RSpec.describe LettingsLog do RSpec.describe LettingsLog do
let(:different_managing_organisation) { create(:organisation) } let(:different_managing_organisation) { create(:organisation) }
@ -3112,5 +3113,55 @@ RSpec.describe LettingsLog do
end end
end 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 end
# rubocop:enable RSpec/AnyInstance # rubocop:enable RSpec/AnyInstance
# rubocop:enable RSpec/MessageChain

51
spec/models/sales_log_spec.rb

@ -1,6 +1,7 @@
require "rails_helper" require "rails_helper"
require "shared/shared_examples_for_derived_fields" require "shared/shared_examples_for_derived_fields"
# rubocop:disable RSpec/MessageChain
# rubocop:disable RSpec/AnyInstance # rubocop:disable RSpec/AnyInstance
RSpec.describe SalesLog, type: :model do RSpec.describe SalesLog, type: :model do
let(:owning_organisation) { create(:organisation) } let(:owning_organisation) { create(:organisation) }
@ -607,5 +608,55 @@ RSpec.describe SalesLog, type: :model do
end end
end 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 end
# rubocop:enable RSpec/AnyInstance # rubocop:enable RSpec/AnyInstance
# rubocop:enable RSpec/MessageChain

Loading…
Cancel
Save