Skip to content

Commit

Permalink
refactor profiles qualifications controller
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Jan 6, 2025
1 parent fbb0054 commit 57fc578
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
24 changes: 9 additions & 15 deletions app/controllers/concerns/jobseekers/qualification_form_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
54 changes: 30 additions & 24 deletions app/controllers/jobseekers/profiles/qualifications_controller.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -42,41 +49,44 @@ 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))
.permit(:category, :finished_studying, :finished_studying_details, :grade, :institution, :name, :subject, :year, qualification_results_attributes: %i[id subject grade awarding_body])
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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/views/jobseekers/profiles/qualifications/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
- 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}")
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
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 57fc578

Please sign in to comment.