Skip to content

Commit

Permalink
Validate course subject params before assigning course attributes
Browse files Browse the repository at this point in the history
  It's easier to separate the parameter validation from the course
  validation in the Subjects controller. This commit covers all subject
  types including physics, modern languages, primary, secondary and
  further education
  • Loading branch information
inulty-dfe committed May 30, 2024
1 parent b2b3a95 commit b091b2b
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions app/controllers/publish/courses/subjects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,44 @@ def continue

def update
authorize(provider)

if params[:course][:master_subject_id] == SecondarySubject.physics.id.to_s
course.update(master_subject_id: params[:course][:master_subject_id])
redirect_to(
engineers_teach_physics_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
course: { master_subject_id: SecondarySubject.physics.id.to_s, subjects_ids: selected_subject_ids }
if validate_subject_ids
if params[:course][:master_subject_id] == SecondarySubject.physics.id.to_s
course.update(master_subject_id: params[:course][:master_subject_id])
redirect_to(
engineers_teach_physics_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
course: { master_subject_id: SecondarySubject.physics.id.to_s, subjects_ids: selected_subject_ids }
)
)
)

elsif selected_subject_ids.include?(modern_languages_subject_id.to_s) && validate_subject_ids
course.update(master_subject_id: params[:course][:master_subject_id])
redirect_to(
modern_languages_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
course: { subjects_ids: selected_subject_ids }

elsif selected_subject_ids.include?(modern_languages_subject_id.to_s)
course.update(master_subject_id: params[:course][:master_subject_id])
redirect_to(
modern_languages_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
course: { subjects_ids: selected_subject_ids }
)
)
)

elsif course.errors.none? && course_subjects_form.save!
course_updated_message(section_key)
# TODO: move this to the form?
course.update(master_subject_id: params[:course][:master_subject_id])
course.update(name: course.generate_name)
course.update(campaign_name: nil) unless course.master_subject_id == SecondarySubject.physics.id

redirect_to(
details_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code

elsif course.errors.none? && course_subjects_form.save!
course_updated_message(section_key)
# TODO: move this to the form?
course.update(master_subject_id: params[:course][:master_subject_id])
course.update(name: course.generate_name)
course.update(campaign_name: nil) unless course.master_subject_id == SecondarySubject.physics.id

redirect_to(
details_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code
)
)
)
end
else
@errors = @course.errors.messages
course.master_subject_id = selected_master
Expand Down

0 comments on commit b091b2b

Please sign in to comment.