diff --git a/app/controllers/concerns/jobseekers/qualification_form_concerns.rb b/app/controllers/concerns/jobseekers/qualification_form_concerns.rb index 3eeace9ede..c79315b40a 100644 --- a/app/controllers/concerns/jobseekers/qualification_form_concerns.rb +++ b/app/controllers/concerns/jobseekers/qualification_form_concerns.rb @@ -6,21 +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" - elsif %w[confirm_destroy].include?(action_name) - "DeleteForm" - 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 4634f3591d..dad85a431b 100644 --- a/app/controllers/jobseekers/job_applications/qualifications_controller.rb +++ b/app/controllers/jobseekers/job_applications/qualifications_controller.rb @@ -10,7 +10,7 @@ def select_category end def submit_category - @category = submit_category_params[:category] + @category = category_param @form = Jobseekers::Qualifications::CategoryForm.new(submit_category_params) if @form.valid? diff --git a/app/controllers/jobseekers/profiles/qualifications_controller.rb b/app/controllers/jobseekers/profiles/qualifications_controller.rb index 907c3cee19..2d28756e6d 100644 --- a/app/controllers/jobseekers/profiles/qualifications_controller.rb +++ b/app/controllers/jobseekers/profiles/qualifications_controller.rb @@ -1,26 +1,33 @@ class Jobseekers::Profiles::QualificationsController < Jobseekers::ProfilesController include Jobseekers::QualificationFormConcerns - helper_method :form, :jobseeker_profile, :qualification, :secondary?, :qualification_form_param_key + helper_method :jobseeker_profile, :qualification, :qualification_form_param_key - before_action :set_category + before_action :set_form_and_category, except: %i[review confirm_destroy destroy select_category submit_category] + + def select_category + @form = Jobseekers::Qualifications::CategoryForm.new + end def submit_category - if form.valid? - redirect_to new_jobseekers_profile_qualification_path(qualification_params) + @category = category_param + @form = Jobseekers::Qualifications::CategoryForm.new(submit_category_params) + + if @form.valid? + redirect_to new_jobseekers_profile_qualification_path(submit_category_params) else - render :select_category + render :select_category, status: :unprocessable_entity end end def new; end def create - if form.valid? + if @form.valid? profile.qualifications.create(qualification_params) redirect_to review_jobseekers_profile_qualifications_path else - render :new + render :new, status: :unprocessable_entity end end @@ -29,11 +36,11 @@ def edit; end def review; end def update - if form.valid? + if @form.valid? qualification.update(qualification_params) redirect_to review_jobseekers_profile_qualifications_path else - render :edit + render :edit, status: :unprocessable_entity end end @@ -42,32 +49,34 @@ def destroy redirect_to review_jobseekers_profile_qualifications_path, success: t(".success") end - def confirm_destroy; end + def confirm_destroy + @category = qualification.category + @form = Jobseekers::Qualifications::DeleteForm.new + end 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", "confirm_destroy" - {} 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)) @@ -75,8 +84,9 @@ def qualification_params end end - def set_category - @category = action_name.in?(%w[edit update confirm_destroy]) ? qualification.category : category_param + def set_form_and_category + @category = action_name.in?(%w[edit update]) ? qualification.category : category_param + @form = category_form_class(@category).new(form_attributes) end def category_param @@ -86,8 +96,4 @@ def category_param def qualification @qualification ||= profile.qualifications.find(params[:id] || params[:qualification_id]) end - - def secondary? - @category.in?(Qualification::SECONDARY_QUALIFICATIONS) - end end diff --git a/app/views/jobseekers/profiles/qualifications/edit.html.slim b/app/views/jobseekers/profiles/qualifications/edit.html.slim index b4cb885701..a7a3ecbfc5 100644 --- a/app/views/jobseekers/profiles/qualifications/edit.html.slim +++ b/app/views/jobseekers/profiles/qualifications/edit.html.slim @@ -6,7 +6,7 @@ .govuk-grid-row .govuk-grid-column-two-thirds - = form_for form, url: jobseekers_profile_qualification_path(params[:id]), method: :patch do |f| + = form_for @form, url: jobseekers_profile_qualification_path(params[:id]), method: :patch do |f| = f.govuk_error_summary = f.govuk_fieldset legend: { text: t(".page_title"), tag: "h1", size: "l" } do diff --git a/app/views/jobseekers/profiles/qualifications/new.html.slim b/app/views/jobseekers/profiles/qualifications/new.html.slim index 3193f2a349..61e611f356 100644 --- a/app/views/jobseekers/profiles/qualifications/new.html.slim +++ b/app/views/jobseekers/profiles/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,11 +6,11 @@ h1.govuk-heading-xl = t(".heading.#{@category}") p = t(".hint") - = form_for form, url: jobseekers_profile_qualifications_path(profile, category: @category), method: :post do |f| + = form_for @form, url: jobseekers_profile_qualifications_path(profile, category: @category), method: :post 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_link_to t("buttons.cancel"), jobseekers_profile_path diff --git a/app/views/jobseekers/profiles/qualifications/select_category.html.slim b/app/views/jobseekers/profiles/qualifications/select_category.html.slim index 6b6fb615ae..8bb62ac9da 100644 --- a/app/views/jobseekers/profiles/qualifications/select_category.html.slim +++ b/app/views/jobseekers/profiles/qualifications/select_category.html.slim @@ -1,4 +1,4 @@ -- content_for :page_title_prefix, job_application_page_title_prefix(form, t(".page_title")) +- content_for :page_title_prefix, job_application_page_title_prefix(@form, t(".page_title")) - content_for :breadcrumbs do nav.govuk-breadcrumbs aria-label="Breadcrumbs" @@ -8,7 +8,7 @@ .govuk-grid-column-two-thirds span.govuk-caption-l = t(".caption") - = form_for form, url: submit_category_jobseekers_profile_qualifications_path, method: :post do |f| + = form_for @form, url: submit_category_jobseekers_profile_qualifications_path, method: :post do |f| = f.govuk_error_summary = render "shared/category_fieldset", form: f