Skip to content

Commit

Permalink
remove secondary? method from controller(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Dec 20, 2024
1 parent 860725f commit 21d61d3
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 58 deletions.
22 changes: 9 additions & 13 deletions app/controllers/concerns/jobseekers/qualification_form_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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
31 changes: 21 additions & 10 deletions app/controllers/jobseekers/profiles/qualifications_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion app/views/jobseekers/profiles/qualifications/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 21d61d3

Please sign in to comment.