Skip to content

Commit

Permalink
Make use of inclusion & update spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Kizr committed Jun 21, 2024
1 parent 17ee0fb commit 2749278
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions app/wizards/placements/add_placement/steps/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion spec/wizards/placements/add_placement/steps/subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2749278

Please sign in to comment.