Skip to content

Commit

Permalink
Add Awarding Body to qualification result (#7352)
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan authored Jan 14, 2025
1 parent b8f0543 commit 9b0ff39
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 131 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
@@ -1,72 +1,78 @@
class Jobseekers::JobApplications::QualificationsController < Jobseekers::BaseController
include Jobseekers::QualificationFormConcerns

helper_method :back_path, :category, :form, :job_application, :qualification, :secondary?, :qualification_form_param_key
helper_method :back_path, :job_application, :qualification_form_param_key

before_action :set_qualification, only: %i[edit update destroy]

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 = category_param
@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?
@category = category_param
@form = category_form_class(@category).new(qualification_params)

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?
qualification.update(qualification_params)
@category = @qualification.category
@form = category_form_class(@category).new(qualification_params)

if @form.valid?
@qualification.update(qualification_params)
redirect_to back_path
else
render :edit
render :edit, status: :unprocessable_entity
end
end

def destroy
qualification.destroy
@qualification.destroy
redirect_to back_path, success: t(".success")
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"
{}
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])
end
end

def category
@category ||= action_name.in?(%w[edit update]) ? qualification.category : category_param
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 category_param
Expand All @@ -81,11 +87,7 @@ def job_application
@job_application ||= current_jobseeker.job_applications.draft.find(params[:job_application_id])
end

def qualification
@qualification ||= job_application.qualifications.find(params[:id])
end

def secondary?
category.in?(Qualification::SECONDARY_QUALIFICATIONS)
def set_qualification
@qualification = job_application.qualifications.find(params[:id])
end
end
62 changes: 35 additions & 27 deletions app/controllers/jobseekers/profiles/qualifications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
class Jobseekers::Profiles::QualificationsController < Jobseekers::ProfilesController
include Jobseekers::QualificationFormConcerns

helper_method :category, :form, :jobseeker_profile, :qualification, :secondary?, :qualification_form_param_key
helper_method :jobseeker_profile, :qualification, :qualification_form_param_key

before_action :set_form_and_category, only: %i[new create edit update]

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 @@ -27,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 @@ -40,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"
{}
{ category: @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"
(params[qualification_form_param_key(category)] || params).permit(:category)
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])
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 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 @@ -84,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 @@ -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,7 +1,7 @@
class Jobseekers::Qualifications::Secondary::QualificationResultForm
include ActiveModel::Model

attr_accessor :id, :subject, :grade
attr_accessor :id, :subject, :grade, :awarding_body

validates :subject, presence: true
validates :grade, presence: true
Expand Down
16 changes: 9 additions & 7 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ def add_secondary_qualification_details(pdf, qualification)
pdf.move_down 5
pdf.text "Secondary Qualification", size: 12, style: :italic

secondary_qualification_data = [
["Name:", qualification.name],
["Grade:", qualification.grade],
["Date completed:", qualification.year],
].reject { |row| row[1].blank? }

render_table(pdf, secondary_qualification_data)
qualification.qualification_results.each do |result|
secondary_qualification_data = [
["Subject:", result.subject],
["Grade:", result.grade],
(["Awarding Body:", result.awarding_body] if result.awarding_body.present?),
["Date completed:", qualification.year],
].compact
render_table(pdf, secondary_qualification_data)
end
end

def add_general_qualification_details(pdf, qualification)
Expand Down
8 changes: 8 additions & 0 deletions app/helpers/qualifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ def qualifications_sort_and_group(qualifications)
def qualifications_group_category_other?(qualifications)
qualifications.all? { |qualification| qualification.category == "other" }
end

def display_secondary_qualification(res)
if res.awarding_body.blank?
"#{res.subject}#{res.grade}"
else
"#{res.subject}#{res.grade} (#{res.awarding_body})"
end
end
end
1 change: 1 addition & 0 deletions app/models/qualification_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def duplicate
self.class.new(
grade:,
subject:,
awarding_body:,
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- if qualification.secondary?
- summary_list.with_row do |row|
- row.with_key text: t("jobseekers.job_applications.qualifications.subjects_and_grades")
- row.with_value text: safe_join(qualification.qualification_results.map { |res| tag.div("#{res.subject} – #{res.grade}", class: "govuk-body govuk-!-margin-bottom-1") })
- row.with_value text: safe_join(qualification.qualification_results.map { |res| tag.div(display_secondary_qualification(res), class: "govuk-body govuk-!-margin-bottom-1") })

- qualification.display_attributes.each do |attribute|
- summary_list.with_row do |row|
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}")
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
= 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"
Loading

0 comments on commit 9b0ff39

Please sign in to comment.