Skip to content

Commit

Permalink
refactor job application qualifications controller
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Dec 20, 2024
1 parent c869ebd commit fbb0054
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
- 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_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"
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)" }
Expand Down

0 comments on commit fbb0054

Please sign in to comment.