From fbb00540ea3d332dee9e7a2bfbf744cd1df44e2e Mon Sep 17 00:00:00 2001 From: starswan Date: Fri, 20 Dec 2024 15:20:30 +0000 Subject: [PATCH] refactor job application qualifications controller --- .../qualifications_controller.rb | 75 +++++++++---------- .../qualifications/qualification_form.rb | 4 + .../qualifications/secondary/common_form.rb | 4 + .../qualifications/edit.html.slim | 6 +- .../qualifications/new.html.slim | 6 +- .../qualifications/select_category.html.slim | 4 +- .../fields/_secondary_school.html.slim | 4 +- 7 files changed, 55 insertions(+), 48 deletions(-) diff --git a/app/controllers/jobseekers/job_applications/qualifications_controller.rb b/app/controllers/jobseekers/job_applications/qualifications_controller.rb index 659f900a59..4634f3591d 100644 --- a/app/controllers/jobseekers/job_applications/qualifications_controller.rb +++ b/app/controllers/jobseekers/job_applications/qualifications_controller.rb @@ -1,33 +1,54 @@ class Jobseekers::JobApplications::QualificationsController < Jobseekers::BaseController include Jobseekers::QualificationFormConcerns - helper_method :back_path, :form, :job_application, :qualification, :secondary?, :qualification_form_param_key + helper_method :back_path, :job_application, :qualification, :qualification_form_param_key - before_action :set_category + before_action :set_category_and_form, except: %i[destroy edit new select_category submit_category] + + def select_category + @form = Jobseekers::Qualifications::CategoryForm.new + end def submit_category - if form.valid? - redirect_to new_jobseekers_job_application_qualification_path(qualification_params) + @category = submit_category_params[:category] + @form = Jobseekers::Qualifications::CategoryForm.new(submit_category_params) + + if @form.valid? + redirect_to new_jobseekers_job_application_qualification_path(submit_category_params) else - render :select_category + render :select_category, status: :unprocessable_entity end end + def new + @category = category_param + @form = category_form_class(@category).new(category: @category) + end + + def edit + @category = qualification.category + edit_attributes = qualification + .slice(:category, :finished_studying, :finished_studying_details, :grade, :institution, :name, :subject, :year, :qualification_results) + .reject { |_, v| v.blank? && v != false } + + @form = category_form_class(@category).new(edit_attributes) + end + def create - if form.valid? + if @form.valid? job_application.qualifications.create(qualification_params) redirect_to back_path else - render :new + render :new, status: :unprocessable_entity end end def update - if form.valid? + if @form.valid? qualification.update(qualification_params) redirect_to back_path else - render :edit + render :edit, status: :unprocessable_entity end end @@ -38,37 +59,19 @@ def destroy private - def form - @form ||= category_form_class(@category).new(form_attributes) - end - - def form_attributes - case action_name - when "new" - { category: @category } - when "select_category" - {} - when "edit" - qualification - .slice(:category, :finished_studying, :finished_studying_details, :grade, :institution, :name, :subject, :year, :qualification_results) - .reject { |_, v| v.blank? && v != false } - when "create", "update", "submit_category" - qualification_params - end + def submit_category_params + key = ActiveModel::Naming.param_key(Jobseekers::Qualifications::CategoryForm) + (params[key] || params).permit(:category) end def qualification_params - case action_name - when "new", "select_category", "submit_category" - (params[qualification_form_param_key(@category)] || params).permit(:category) - when "create", "edit", "update" - params.require(qualification_form_param_key(@category)) - .permit(:category, :finished_studying, :finished_studying_details, :grade, :institution, :name, :subject, :year, qualification_results_attributes: %i[id subject grade awarding_body]) - end + params.require(qualification_form_param_key(@category)) + .permit(:category, :finished_studying, :finished_studying_details, :grade, :institution, :name, :subject, :year, qualification_results_attributes: %i[id subject grade awarding_body]) end - def set_category + def set_category_and_form @category = action_name.in?(%w[edit update]) ? qualification.category : category_param + @form = category_form_class(@category).new(qualification_params) end def category_param @@ -86,8 +89,4 @@ def job_application def qualification @qualification ||= job_application.qualifications.find(params[:id]) end - - def secondary? - @category.in?(Qualification::SECONDARY_QUALIFICATIONS) - end end diff --git a/app/form_models/jobseekers/qualifications/qualification_form.rb b/app/form_models/jobseekers/qualifications/qualification_form.rb index 996060a40e..4efe5eca43 100644 --- a/app/form_models/jobseekers/qualifications/qualification_form.rb +++ b/app/form_models/jobseekers/qualifications/qualification_form.rb @@ -7,4 +7,8 @@ class Jobseekers::Qualifications::QualificationForm validates :finished_studying_details, presence: true, if: -> { finished_studying == "false" } validates :year, numericality: { less_than_or_equal_to: proc { Time.current.year } }, if: -> { finished_studying == "true" } + + def secondary? + false + end end diff --git a/app/form_models/jobseekers/qualifications/secondary/common_form.rb b/app/form_models/jobseekers/qualifications/secondary/common_form.rb index fa45964a4d..1780329661 100644 --- a/app/form_models/jobseekers/qualifications/secondary/common_form.rb +++ b/app/form_models/jobseekers/qualifications/secondary/common_form.rb @@ -14,6 +14,10 @@ def initialize(attributes = nil) pad_qualification_results end + def secondary? + true + end + def qualification_results_attributes=(attrs) @qualification_results = attrs.map { |_, params| QualificationResultForm.new(params) } end diff --git a/app/views/jobseekers/job_applications/qualifications/edit.html.slim b/app/views/jobseekers/job_applications/qualifications/edit.html.slim index b08c8b94c5..85f0efea15 100644 --- a/app/views/jobseekers/job_applications/qualifications/edit.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/edit.html.slim @@ -1,14 +1,14 @@ -- content_for :page_title_prefix, job_application_page_title_prefix(form, t(".title")) +- content_for :page_title_prefix, job_application_page_title_prefix(@form, t(".title")) .govuk-grid-row .govuk-grid-column-two-thirds span.govuk-caption-l = t(".caption") h1.govuk-heading-xl = t(".heading.#{@category}") - = form_for form, url: jobseekers_job_application_qualification_path(job_application, qualification), method: :patch do |f| + = form_for @form, url: jobseekers_job_application_qualification_path(job_application, qualification), method: :patch do |f| = f.govuk_error_summary link_base_errors_to: :subject = render "jobseekers/qualifications/fields", f: f, category: @category - = f.govuk_submit t("buttons.save_qualification.#{secondary? ? 'other' : 'one'}") + = f.govuk_submit t("buttons.save_qualification.#{f.object.secondary? ? 'other' : 'one'}") = govuk_button_link_to t("buttons.cancel"), jobseekers_job_application_build_path(job_application, :qualifications), class: "govuk-button--secondary govuk-!-margin-left-3" diff --git a/app/views/jobseekers/job_applications/qualifications/new.html.slim b/app/views/jobseekers/job_applications/qualifications/new.html.slim index 2096c58345..a1ce47e765 100644 --- a/app/views/jobseekers/job_applications/qualifications/new.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/new.html.slim @@ -1,4 +1,4 @@ -- content_for :page_title_prefix, job_application_page_title_prefix(form, t(".title")) +- content_for :page_title_prefix, job_application_page_title_prefix(@form, t(".title")) .govuk-grid-row .govuk-grid-column-two-thirds @@ -6,10 +6,10 @@ h1.govuk-heading-xl = t(".heading.#{@category}") p = t(".hint") - = form_for form, url: jobseekers_job_application_qualifications_path(job_application, category: @category), method: :post do |f| + = form_for @form, url: jobseekers_job_application_qualifications_path(job_application, category: @category), method: :post do |f| = f.govuk_error_summary link_base_errors_to: :subject = render "jobseekers/qualifications/fields", category: @category, f: f - = f.govuk_submit t("buttons.save_qualification.#{secondary? ? 'other' : 'one'}") + = f.govuk_submit t("buttons.save_qualification.#{f.object.secondary? ? 'other' : 'one'}") = govuk_button_link_to t("buttons.cancel"), select_category_jobseekers_job_application_qualifications_path(job_application), class: "govuk-button--secondary govuk-!-margin-left-3" diff --git a/app/views/jobseekers/job_applications/qualifications/select_category.html.slim b/app/views/jobseekers/job_applications/qualifications/select_category.html.slim index cfbc624521..d238491e31 100644 --- a/app/views/jobseekers/job_applications/qualifications/select_category.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/select_category.html.slim @@ -1,11 +1,11 @@ -- content_for :page_title_prefix, job_application_page_title_prefix(form, t(".title")) +- content_for :page_title_prefix, job_application_page_title_prefix(@form, t(".title")) .govuk-grid-row .govuk-grid-column-two-thirds span.govuk-caption-l = t(".caption") h1.govuk-heading-xl = t(".heading") - = form_for form, url: submit_category_jobseekers_job_application_qualifications_path(job_application), method: :post do |f| + = form_for @form, url: submit_category_jobseekers_job_application_qualifications_path(job_application), method: :post do |f| = f.govuk_error_summary = render "shared/category_fieldset", form: f diff --git a/app/views/jobseekers/qualifications/fields/_secondary_school.html.slim b/app/views/jobseekers/qualifications/fields/_secondary_school.html.slim index 491ee2c838..71bd88266f 100644 --- a/app/views/jobseekers/qualifications/fields/_secondary_school.html.slim +++ b/app/views/jobseekers/qualifications/fields/_secondary_school.html.slim @@ -6,9 +6,9 @@ = f.govuk_number_field :year, label: { size: "s" }, aria: { required: true }, width: 4 = f.govuk_fieldset legend: { text: t("helpers.legend.jobseekers_qualifications_shared_legends.subjects") }, class: "subjects-and-grades", data: { controller: "manage-qualifications" } do - - form.qualification_results.each_with_index do |result, idx| + - f.object.qualification_results.each_with_index do |result, idx| = f.fields_for :qualification_results, result do |result_form| - .inline-fields-container.subject-row class="#{idx > form.highest_present_result_index ? "js-hidden" : ""}" data-manage-qualifications-target="row" + .inline-fields-container.subject-row class="#{idx > f.object.highest_present_result_index ? "js-hidden" : ""}" data-manage-qualifications-target="row" = result_form.govuk_text_field :subject, label: { text: "Subject #{idx + 1}" }, aria: { required: idx.zero? }, form_group: { classes: "govuk-!-width-two-thirds" } = result_form.govuk_text_field :grade, aria: { required: idx.zero? }, form_group: { classes: "govuk-!-width-one-third" }, label: { text: "Grade" } = result_form.govuk_text_field :awarding_body, aria: { required: false }, form_group: { classes: "govuk-!-width-one-third" }, label: { text: "Awarding Body (optional)" }