From 21d61d335f3936ff128edecf16f9bcea24af0fbe Mon Sep 17 00:00:00 2001 From: starswan Date: Fri, 20 Dec 2024 12:34:54 +0000 Subject: [PATCH] remove secondary? method from controller(s) --- .../jobseekers/qualification_form_concerns.rb | 22 +++---- .../qualifications_controller.rb | 61 ++++++++++--------- .../profiles/qualifications_controller.rb | 31 +++++++--- .../qualifications/qualification_form.rb | 4 ++ .../qualifications/secondary/common_form.rb | 4 ++ .../qualifications/edit.html.slim | 2 +- .../qualifications/new.html.slim | 2 +- .../qualifications/new_school.html.slim | 2 +- .../qualifications/confirm_destroy.html.slim | 6 +- .../profiles/qualifications/new.html.slim | 2 +- 10 files changed, 78 insertions(+), 58 deletions(-) diff --git a/app/controllers/concerns/jobseekers/qualification_form_concerns.rb b/app/controllers/concerns/jobseekers/qualification_form_concerns.rb index 0ef34a0c84..c79315b40a 100644 --- a/app/controllers/concerns/jobseekers/qualification_form_concerns.rb +++ b/app/controllers/concerns/jobseekers/qualification_form_concerns.rb @@ -6,19 +6,15 @@ def qualification_form_param_key(category) end def category_form_class(category) - name = if %w[select_category submit_category].include?(action_name) - "CategoryForm" - else - case category - when "gcse", "a_level", "as_level" - "Secondary::CommonForm" - when "other_secondary" - "Secondary::OtherForm" - when "undergraduate", "postgraduate" - "DegreeForm" - when "other" - "OtherForm" - end + name = case category + when "gcse", "a_level", "as_level" + "Secondary::CommonForm" + when "other_secondary" + "Secondary::OtherForm" + when "undergraduate", "postgraduate" + "DegreeForm" + when "other" + "OtherForm" end "Jobseekers::Qualifications::#{name}".constantize end diff --git a/app/controllers/jobseekers/job_applications/qualifications_controller.rb b/app/controllers/jobseekers/job_applications/qualifications_controller.rb index c8ed3f6973..38dbdb7695 100644 --- a/app/controllers/jobseekers/job_applications/qualifications_controller.rb +++ b/app/controllers/jobseekers/job_applications/qualifications_controller.rb @@ -1,24 +1,44 @@ class Jobseekers::JobApplications::QualificationsController < Jobseekers::BaseController include Jobseekers::QualificationFormConcerns - helper_method :back_path, :job_application, :qualification, :secondary?, :qualification_form_param_key + helper_method :back_path, :job_application, :qualification, :qualification_form_param_key - before_action :set_category_and_form, except: %i[destroy] + before_action :set_category_and_form, except: %i[destroy edit new select_category submit_category] + + def select_category + # @category = category_param + @form = Jobseekers::Qualifications::CategoryForm.new + end def submit_category + @category = submit_category_params[:category] + @form = Jobseekers::Qualifications::CategoryForm.new(submit_category_params) + if @form.valid? - redirect_to new_jobseekers_job_application_qualification_path(qualification_params) + redirect_to new_jobseekers_job_application_qualification_path(submit_category_params) else render :select_category, status: :unprocessable_entity end end - def new; end + def new + @category = category_param + @form = category_form_class(@category).new(category: @category) + end def new_school @school_number = params[:school_number].to_i 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? job_application.qualifications.create(qualification_params) @@ -44,34 +64,19 @@ def destroy private - 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_and_form @category = action_name.in?(%w[edit update]) ? qualification.category : category_param - @form = category_form_class(@category).new(form_attributes) + @form = category_form_class(@category).new(qualification_params) end def category_param @@ -90,7 +95,7 @@ def qualification @qualification ||= job_application.qualifications.find(params[:id]) end - def secondary? - @category.in?(Qualification::SECONDARY_QUALIFICATIONS) - end + # def secondary? + # @category.in?(Qualification::SECONDARY_QUALIFICATIONS) + # end end diff --git a/app/controllers/jobseekers/profiles/qualifications_controller.rb b/app/controllers/jobseekers/profiles/qualifications_controller.rb index bf7cd63890..05176cdd56 100644 --- a/app/controllers/jobseekers/profiles/qualifications_controller.rb +++ b/app/controllers/jobseekers/profiles/qualifications_controller.rb @@ -1,13 +1,21 @@ class Jobseekers::Profiles::QualificationsController < Jobseekers::ProfilesController include Jobseekers::QualificationFormConcerns - helper_method :jobseeker_profile, :qualification, :secondary?, :qualification_form_param_key + helper_method :jobseeker_profile, :qualification, :qualification_form_param_key - before_action :set_form_and_category, except: %i[review confirm_destroy destroy] + before_action :set_form_and_category, except: %i[review confirm_destroy destroy select_category submit_category] + + def select_category + @category = category_param + @form = Jobseekers::Qualifications::CategoryForm.new + end def submit_category + @category = category_param + @form = Jobseekers::Qualifications::CategoryForm.new(submit_category_params) + if @form.valid? - redirect_to new_jobseekers_profile_qualification_path(qualification_params) + redirect_to new_jobseekers_profile_qualification_path(submit_category_params) else render :select_category, status: :unprocessable_entity end @@ -53,20 +61,23 @@ 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" + when "create", "update" qualification_params end 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", "confirm_destroy" + when "new", "confirm_destroy" (params[qualification_form_param_key(@category)] || params).permit(:category) when "create", "edit", "update" params.require(qualification_form_param_key(@category)) @@ -87,7 +98,7 @@ def qualification @qualification ||= profile.qualifications.find(params[:id] || params[:qualification_id]) end - def secondary? - @category.in?(Qualification::SECONDARY_QUALIFICATIONS) - 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 cd8075e0b5..49e4f4daa1 100644 --- a/app/form_models/jobseekers/qualifications/secondary/common_form.rb +++ b/app/form_models/jobseekers/qualifications/secondary/common_form.rb @@ -12,6 +12,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 0cbc7c74ce..57e4668d69 100644 --- a/app/views/jobseekers/job_applications/qualifications/edit.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/edit.html.slim @@ -9,5 +9,5 @@ = render "jobseekers/qualifications/fields", f: f, category: @category, form: @form - = f.govuk_submit t("buttons.save_qualification.#{secondary? ? 'other' : 'one'}") + = f.govuk_submit t("buttons.save_qualification.#{qualification.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 8c2bb988ea..a98c6de6ff 100644 --- a/app/views/jobseekers/job_applications/qualifications/new.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/new.html.slim @@ -14,5 +14,5 @@ = turbo_frame_tag "secondary_school_1" do p = govuk_button_link_to t(".add_another_school"), new_school_jobseekers_job_application_qualifications_path(job_application, category: @category, school_number: 1) - = 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/new_school.html.slim b/app/views/jobseekers/job_applications/qualifications/new_school.html.slim index 2462734b5d..874ea0e154 100644 --- a/app/views/jobseekers/job_applications/qualifications/new_school.html.slim +++ b/app/views/jobseekers/job_applications/qualifications/new_school.html.slim @@ -14,5 +14,5 @@ = turbo_frame_tag "secondary_school_#{@school_number + 1}" do p = govuk_button_link_to "Add Another School", new_school_jobseekers_job_application_qualifications_path(job_application, category: @category, school_number: @school_number + 1) - = 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/profiles/qualifications/confirm_destroy.html.slim b/app/views/jobseekers/profiles/qualifications/confirm_destroy.html.slim index ea3320811d..8d3be71b6e 100644 --- a/app/views/jobseekers/profiles/qualifications/confirm_destroy.html.slim +++ b/app/views/jobseekers/profiles/qualifications/confirm_destroy.html.slim @@ -7,7 +7,7 @@ .govuk-grid-row .govuk-grid-column-two-thirds - - if secondary? + - if qualification.secondary? span.govuk-caption-l = t(".secondary.caption") h1.govuk-heading-l = t(".secondary.page_title") - else @@ -16,7 +16,7 @@ = form_for form, url: jobseekers_profile_qualification_path(params[:qualification_id]), method: :delete do |f| = govuk_summary_list do |summary_list| - - if secondary? + - if qualification.secondary? - summary_list.with_row do |row| - row.with_key text: "Type" - row.with_value text: t("helpers.label.jobseekers_qualifications_shared_labels.category_options.#{qualification.category}") @@ -40,7 +40,7 @@ - row.with_key text: "Grade" - row.with_value text: qualification.grade - - if secondary? + - if qualification.secondary? = f.govuk_submit t(".secondary.caption") - else = f.govuk_submit t(".other.caption") diff --git a/app/views/jobseekers/profiles/qualifications/new.html.slim b/app/views/jobseekers/profiles/qualifications/new.html.slim index ca89de155c..4d7a556171 100644 --- a/app/views/jobseekers/profiles/qualifications/new.html.slim +++ b/app/views/jobseekers/profiles/qualifications/new.html.slim @@ -11,6 +11,6 @@ = render "jobseekers/qualifications/fields", f: f, category: @category, form: @form - = f.govuk_submit t("buttons.save_qualification.#{secondary? ? 'other' : 'one'}") + = f.govuk_submit t("buttons.save_qualification.#{f.object.secondary? ? 'other' : 'one'}") = govuk_link_to t("buttons.cancel"), jobseekers_profile_path