From 2749278096011303784353a9e592e2e4500ebf51 Mon Sep 17 00:00:00 2001 From: Ben Abraham <16797406+Kizr@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:43:21 +0100 Subject: [PATCH] Make use of inclusion & update spec --- .../placements/add_placement/steps/subject.rb | 7 ++++-- .../add_placement/steps/subject_spec.rb | 22 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/wizards/placements/add_placement/steps/subject.rb b/app/wizards/placements/add_placement/steps/subject.rb index 1cd651237..162873e33 100644 --- a/app/wizards/placements/add_placement/steps/subject.rb +++ b/app/wizards/placements/add_placement/steps/subject.rb @@ -8,10 +8,13 @@ class Placements::AddPlacement::Steps::Subject validates :school, presence: true validates :phase, presence: true - validates :subject_id, presence: true + validates :subject_id, presence: true, inclusion: { in: ->(step) { step.subjects_for_selection.ids } }, if: ->(step) { step.phase.present? } def subjects_for_selection - phase == "Primary" ? Subject.parent_subjects.primary : Subject.parent_subjects.secondary + { + "Primary" => Subject.parent_subjects.primary, + "Secondary" => Subject.parent_subjects.secondary, + }.fetch phase end def wizard_attributes diff --git a/spec/wizards/placements/add_placement/steps/subject_spec.rb b/spec/wizards/placements/add_placement/steps/subject_spec.rb index c91326bff..b2df2e731 100644 --- a/spec/wizards/placements/add_placement/steps/subject_spec.rb +++ b/spec/wizards/placements/add_placement/steps/subject_spec.rb @@ -8,7 +8,27 @@ describe "validations" do it { is_expected.to validate_presence_of(:school) } it { is_expected.to validate_presence_of(:phase) } - it { is_expected.to validate_presence_of(:subject_id) } + + describe "subject_id" do + it "is not required if phase is not present" do + step = described_class.new(phase: nil) + + expect(step).not_to validate_presence_of(:subject_id) + end + + it "is required if phase is present" do + step = described_class.new(phase: "Primary") + + expect(step).to validate_presence_of(:subject_id) + end + + it "is invalid if the subject is not in the selection" do + step = described_class.new(phase: "Primary", subject_id: 1) + + expect(step).not_to be_valid + expect(step.errors[:subject_id]).to include "is not included in the list" + end + end end describe "#subjects_for_selection" do