Skip to content

Commit

Permalink
Prevent induction period start dates before Sept 2021
Browse files Browse the repository at this point in the history
There's a chance we'll want to allow some earlier dates when importing
so maybe we should relax this rule for the migration.
  • Loading branch information
peteryates committed Jan 19, 2025
1 parent 53f0094 commit b1407da
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/models/induction_period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class InductionPeriod < ApplicationRecord
inclusion: { in: %w[fip cip diy],
message: "Choose an induction programme" }

validate :started_on_from_september_2021_onwards, if: -> { started_on.present? }
validate :started_on_not_in_future, if: -> { started_on.present? }
validate :finished_on_not_in_future, if: -> { finished_on.present? }
validate :start_date_after_qts_date
Expand All @@ -34,6 +35,12 @@ class InductionPeriod < ApplicationRecord

private

def started_on_from_september_2021_onwards
return if started_on >= Date.new(2021, 9, 1)

errors.add(:started_on, "Enter a start date after 1 September 2021")
end

def started_on_not_in_future
return if started_on <= Date.current

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
let!(:older_period) do
FactoryBot.create(:induction_period,
teacher:,
started_on: 4.years.ago,
finished_on: 3.years.ago)
started_on: 3.years.ago,
finished_on: 2.years.ago)
end

it "displays all past periods in chronological order" do
render_inline(component)
dates = page.all(".govuk-summary-list__value").map(&:text)
expect(dates).to include(4.years.ago.to_date.to_fs(:govuk))
expect(dates).to include(3.years.ago.to_date.to_fs(:govuk))
expect(dates).to include(2.years.ago.to_date.to_fs(:govuk))
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/induction_period_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@

it { is_expected.to validate_inclusion_of(:induction_programme).in_array(%w[fip cip diy]).with_message("Choose an induction programme") }

describe '#started_on_from_september_2021_onwards' do
context 'when started_on before September 2021' do
subject { FactoryBot.build(:induction_period, started_on:) }
let(:started_on) { Date.new(2021, 8, 31) }
before { subject.valid? }

it 'has a suitable error message' do
expect(subject.errors.messages[:started_on]).to include("Enter a start date after 1 September 2021")
end
end

it { is_expected.not_to allow_values(Date.new(2021, 8, 31)).for(:started_on) }
it { is_expected.to allow_values(Date.new(2021, 9, 1), Date.new(2021, 9, 2)).for(:started_on) }
end

describe "started_on_not_in_future" do
let(:induction_period) { FactoryBot.create(:induction_period, finished_on: nil) }

Expand Down

0 comments on commit b1407da

Please sign in to comment.