Browse Source

Update available_from methods (#1026)

* Update available_from methods

* Move collection start date logic to form handler
pull/1045/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
a91c8da62e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/models/form_handler.rb
  2. 4
      app/models/location.rb
  3. 2
      app/models/scheme.rb
  4. 4
      spec/helpers/locations_helper_spec.rb
  5. 44
      spec/models/location_spec.rb
  6. 34
      spec/models/scheme_spec.rb

5
app/models/form_handler.rb

@ -49,6 +49,11 @@ class FormHandler
today < window_end_date ? today.year - 1 : today.year
end
def collection_start_date(date)
window_end_date = Time.zone.local(date.year, 4, 1)
date < window_end_date ? Time.zone.local(date.year - 1, 4, 1) : Time.zone.local(date.year, 4, 1)
end
def current_collection_start_date
Time.zone.local(current_collection_start_year, 4, 1)
end

4
app/models/location.rb

@ -370,7 +370,9 @@ class Location < ApplicationRecord
end
def available_from
startdate || [created_at, FormHandler.instance.current_collection_start_date].min
return startdate if startdate.present?
FormHandler.instance.collection_start_date(created_at)
end
def status

2
app/models/scheme.rb

@ -210,7 +210,7 @@ class Scheme < ApplicationRecord
end
def available_from
[created_at, FormHandler.instance.current_collection_start_date].min
FormHandler.instance.collection_start_date(created_at)
end
def status

4
spec/helpers/locations_helper_spec.rb

@ -154,11 +154,11 @@ RSpec.describe LocationsHelper do
context "when viewing availability" do
context "with no deactivations" do
it "displays created_at as availability date if startdate is not present" do
it "displays previous collection start date as availability date if created_at is earlier than collection start date" do
location.update!(startdate: nil)
availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from #{location.created_at.to_formatted_s(:govuk_date)}")
expect(availability_attribute).to eq("Active from 1 April 2021")
end
it "displays current collection start date as availability date if created_at is later than collection start date" do

44
spec/models/location_spec.rb

@ -208,4 +208,48 @@ RSpec.describe Location, type: :model do
end
end
end
describe "available_from" do
context "when there is a startdate" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 6)) }
it "returns the startdate" do
expect(location.available_from).to eq(Time.zone.local(2022, 4, 6))
end
end
context "when there is no start date" do
context "and the location was created at the start of the 2022/23 collection window" do
let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2022, 4, 6), startdate: nil) }
it "returns the beginning of 22/23 collection window" do
expect(location.available_from).to eq(Time.zone.local(2022, 4, 1))
end
end
context "and the location was created at the end of the 2022/23 collection window" do
let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2023, 2, 6), startdate: nil) }
it "returns the beginning of 22/23 collection window" do
expect(location.available_from).to eq(Time.zone.local(2022, 4, 1))
end
end
context "and the location was created at the start of the 2021/22 collection window" do
let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2021, 4, 6), startdate: nil) }
it "returns the beginning of 21/22 collection window" do
expect(location.available_from).to eq(Time.zone.local(2021, 4, 1))
end
end
context "and the location was created at the end of the 2021/22 collection window" do
let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2022, 2, 6), startdate: nil) }
it "returns the beginning of 21/22 collection window" do
expect(location.available_from).to eq(Time.zone.local(2021, 4, 1))
end
end
end
end
end

34
spec/models/scheme_spec.rb

@ -190,4 +190,38 @@ RSpec.describe Scheme, type: :model do
expect(all_schemes[2].status).to eq(:incomplete)
end
end
describe "available_from" do
context "when the scheme was created at the start of the 2022/23 collection window" do
let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2022, 4, 6)) }
it "returns the beginning of 22/23 collection window" do
expect(scheme.available_from).to eq(Time.zone.local(2022, 4, 1))
end
end
context "when the scheme was created at the end of the 2022/23 collection window" do
let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2023, 2, 6)) }
it "returns the beginning of 22/23 collection window" do
expect(scheme.available_from).to eq(Time.zone.local(2022, 4, 1))
end
end
context "when the scheme was created at the start of the 2021/22 collection window" do
let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2021, 4, 6)) }
it "returns the beginning of 21/22 collection window" do
expect(scheme.available_from).to eq(Time.zone.local(2021, 4, 1))
end
end
context "when the scheme was created at the end of the 2021/22 collection window" do
let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2022, 2, 6)) }
it "returns the beginning of 21/22 collection window" do
expect(scheme.available_from).to eq(Time.zone.local(2021, 4, 1))
end
end
end
end

Loading…
Cancel
Save