Skip to content

Commit

Permalink
if one eligible_itt_subject show better error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Nov 27, 2024
1 parent e5ad214 commit e28fbd5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@ class EligibleIttSubjectForm < Form

attribute :eligible_itt_subject, :string

validates :eligible_itt_subject, inclusion: {in: :available_options, message: i18n_error_message(:inclusion)}
validates :eligible_itt_subject,
inclusion: {
in: :available_options,
message: i18n_error_message(:inclusion)
},
unless: :boolean_answer?

validates :eligible_itt_subject,
inclusion: {
in: :available_options,
message: ->(object, _data) {
"Select yes if you did your #{qualification_to_substring(object.answers.qualification.to_sym)} in #{object.available_subjects.first}"
}
},
if: :boolean_answer?

def self.qualification_to_substring(qualification_symbol)
{
undergraduate_itt: "undergraduate initial teacher training (ITT)",
postgraduate_itt: "postgraduate initial teacher training (ITT)",
assessment_only: "assessment",
overseas_recognition: "teaching qualification"
}[qualification_symbol]
end

def available_subjects
@available_subjects ||= subject_symbols.map(&:to_s)
Expand Down Expand Up @@ -52,6 +75,10 @@ def save
def eligible_itt_subject_changed?
journey_session.answers.eligible_itt_subject != eligible_itt_subject
end

def boolean_answer?
!available_subjects.many?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

let(:itt_academic_year) { AcademicYear.new(2020) }

let(:form) do
subject(:form) do
described_class.new(
journey: journey,
journey_session: journey_session,
Expand All @@ -40,15 +40,37 @@
end

describe "validations" do
subject { form }

let(:params) { ActionController::Parameters.new }

it do
is_expected.to validate_inclusion_of(:eligible_itt_subject)
.in_array(form.available_options)
.with_message("Select a subject")
end

context "when single subject available" do
before do
allow(JourneySubjectEligibilityChecker).to receive(:selectable_subject_symbols).and_return([:mathematics])
end

let(:answers) do
build(
:additional_payments_answers,
attributes_for(
:additional_payments_answers,
:with_qualification,
itt_academic_year: itt_academic_year,
current_school_id: create(:school, :early_career_payments_eligible).id
)
)
end

it "returns contextual error message" do
expect(subject).to validate_inclusion_of(:eligible_itt_subject)
.in_array(["mathematics"])
.with_message("Select yes if you did your postgraduate initial teacher training (ITT) in mathematics")
end
end
end

describe "#available_subjects" do
Expand Down

0 comments on commit e28fbd5

Please sign in to comment.