Skip to content

Commit

Permalink
don't create 6 forms as we don't need them
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Dec 19, 2024
1 parent 6917c1e commit a0fa175
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 55 deletions.
3 changes: 1 addition & 2 deletions app/assets/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import 'core-js/modules/es.weak-set';
import '@stimulus/polyfills';
import { initAll } from 'govuk-frontend';
import { Application } from '@hotwired/stimulus';
// eslint-disable-next-line no-unused-vars,import/no-unresolved,import/no-extraneous-dependencies
import * as Turbo from '@hotwired/turbo';
import '@hotwired/turbo-rails';
import Rails from 'rails-ujs';

// view components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def qualification_form_param_key(category)
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def submit_category
end
end

def new
end

def new_school
@school_number = params[:school_number].to_i
end

def create
if @form.valid?
job_application.qualifications.create(qualification_params)
Expand Down
11 changes: 7 additions & 4 deletions app/controllers/jobseekers/profiles/qualifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Jobseekers::Profiles::QualificationsController < Jobseekers::ProfilesContr

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

before_action :set_form_and_category, except: %i[review destroy]
before_action :set_form_and_category, except: %i[review confirm_destroy destroy]

def submit_category
if @form.valid?
Expand Down Expand Up @@ -42,15 +42,18 @@ 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_attributes
case action_name
when "new"
{ category: @category }
when "select_category", "confirm_destroy"
when "select_category"
{}
when "edit"
qualification
Expand All @@ -72,7 +75,7 @@ def qualification_params
end

def set_form_and_category
@category = action_name.in?(%w[edit update confirm_destroy]) ? qualification.category : category_param
@category = action_name.in?(%w[edit update]) ? qualification.category : category_param
@form = category_form_class(@category).new(form_attributes)
end

Expand Down
13 changes: 2 additions & 11 deletions app/form_models/jobseekers/qualifications/secondary/common_form.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Jobseekers::Qualifications::Secondary
class CommonForm < ::Jobseekers::Qualifications::QualificationForm
MAXIMUM_NUMBER_OF_RESULTS = 6

attr_accessor :qualification_results

validate :at_least_one_qualification_result
Expand All @@ -28,10 +26,6 @@ def read_attribute_for_validation(attr)
super unless /^qualification_results_attributes_/.match?(attr)
end

def highest_present_result_index
@highest_present_result_index ||= qualification_results.map.with_index { |result, index| index unless result.empty? }.compact.last || 0
end

private

def at_least_one_qualification_result
Expand All @@ -53,12 +47,9 @@ def all_qualification_results_valid
end

def pad_qualification_results
# Ensures the number of QualificationResults present in the form are the highest of:
# - however many are already there (in case we lower the maximum number in the future), or
# - the maximum number we want to have
# by padding the qualification results with empty objects
@qualification_results ||= []
@qualification_results += [QualificationResultForm.new] * [MAXIMUM_NUMBER_OF_RESULTS - qualification_results.count, 0].max
# we just need 1 empty qualification result form that we can clone
@qualification_results += [QualificationResultForm.new]
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@

= render "jobseekers/qualifications/fields", category: @category, f: f, form: @form

= turbo_frame_tag "secondary_school_1" do
/ p = govuk_button_link_to "Add Another School", new_secondary_school_jobseekers_job_application_qualifications_path(job_application, category: @category), data: { turbo_frame: "secondary_school" }
p = govuk_button_link_to "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'}")
= 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
@@ -0,0 +1,18 @@
- 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|
= f.govuk_error_summary link_base_errors_to: :subject

= turbo_frame_tag "secondary_school_#{@school_number}" do
= render "jobseekers/qualifications/fields", category: @category, f: f, form: @form
= 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'}")
= 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 @@ -6,15 +6,20 @@
= 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.fields_for :qualification_results, form.qualification_results.first do |result_form|
.inline-fields-container.subject-row data-manage-qualifications-target="row"
= result_form.govuk_text_field :subject, label: { text: "Subject 1" }, aria: { required: true }, form_group: { classes: "govuk-!-width-one-half" }
= result_form.govuk_text_field :grade, aria: { required: true }, form_group: { classes: "govuk-!-width-one-quarter" }, label: { text: "Grade" }
= result_form.govuk_text_field :awarding_body, aria: { required: false }, form_group: { classes: "govuk-!-width-one-quarter" }, label: { text: "Awarding Body (optional)" }
- form.qualification_results[1..].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"
= 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)" }
.inline-fields-container.subject-row class="#{result.empty? ? "js-hidden" : ""}" data-manage-qualifications-target="row"
= result_form.govuk_text_field :subject, label: { text: "Subject #{idx + 2}" }, aria: { required: false }, form_group: { classes: "govuk-!-width-one-half" }
= result_form.govuk_text_field :grade, aria: { required: false }, form_group: { classes: "govuk-!-width-one-quarter" }, label: { text: "Grade" }
= result_form.govuk_text_field :awarding_body, aria: { required: false }, form_group: { classes: "govuk-!-width-one-quarter" }, label: { text: "Awarding Body (optional)" }
.govuk-form-group.button_to
button.govuk-button.js-action.govuk-button--secondary class="govuk-!-margin-bottom-0" data-action="manage-qualifications#deleteRow" type="button"
= "#{t('buttons.delete_subject')} #{idx + 1}"
= "#{t('buttons.delete_subject')} #{idx + 2}"

span class="govuk-!-margin-bottom-6 govuk-!-margin-top-0 js-action button_to"
button.govuk-button.govuk-button--secondary id="add_subject" data-action="manage-qualifications#addRow" type="button"
Expand Down
7 changes: 7 additions & 0 deletions config/locales/jobseekers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ en:
other: Add a qualification or course
title: Add a qualification — Application
hint: You do not need to add qualified teacher status (QTS) here. You can add this to your application in the professional status section.
new_secondary_school:
<<: *qualifications_shared
title: Add a qualification — Application
hint: You do not need to add qualified teacher status (QTS) here. You can add this to your application in the professional status section.
heading:
gcse: Add GCSEs
other_secondary: Add secondary qualifications
select_category:
<<: *qualifications_shared
heading: Add a qualification
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
resources :qualifications, only: %i[new create edit update destroy], controller: "job_applications/qualifications" do
collection do
get :select_category
get :new_school
post :submit_category
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
RSpec.describe "Jobseekers can add qualifications to their job application", :js do
let(:jobseeker) { create(:jobseeker) }
let(:vacancy) { create(:vacancy, organisations: [build(:school)]) }
let(:job_application) { create(:job_application, :status_draft, jobseeker: jobseeker, vacancy: vacancy) }
let(:job_application) { create(:job_application, :status_draft, jobseeker: jobseeker, vacancy: vacancy, qualifications: qualifications) }

before { login_as(jobseeker, scope: :jobseeker) }

after { logout }

context "adding a qualification" do
let(:qualifications) { [] }

before do
visit jobseekers_job_application_build_path(job_application, :qualifications)
click_on I18n.t("buttons.add_qualification")
Expand Down Expand Up @@ -84,16 +86,18 @@
end

context "when editing a qualification" do
before do
visit jobseekers_job_application_build_path(job_application, :qualifications)
end

context "when the qualification does not have qualification results" do
let!(:qualification) do
create(:qualification,
category: "undergraduate",
institution: "Life University",
job_application: job_application)
let(:qualifications) do
build_list(:qualification, 1,
category: "undergraduate",
institution: "Life University")
end

it "allows jobseekers to edit the qualification" do
visit jobseekers_job_application_build_path(job_application, :qualifications)
click_on I18n.t("buttons.change")
expect(page).to have_link(I18n.t("buttons.cancel"), href: jobseekers_job_application_build_path(job_application, :qualifications))
fill_in "Awarding body", with: "University of Life"
Expand All @@ -106,15 +110,15 @@
end

context "when the qualification has qualification results" do
let!(:qualification) do
create(:qualification,
category: "other_secondary",
institution: "John Mason School",
job_application: job_application)
let(:qualifications) do
build_list(:qualification, 1,
qualification_results: build_list(:qualification_result, 5),
category: "other_secondary",
institution: "John Mason School")
end
let(:qualification) { job_application.qualifications.first }

it "allows jobseekers to edit the qualification and its results" do
visit jobseekers_job_application_build_path(job_application, :qualifications)
expect(qualification.qualification_results.count).to eq(5)
click_on I18n.t("buttons.change")
fill_in "jobseekers_qualifications_secondary_other_form[qualification_results_attributes][0][subject]", with: "Hard Knocks"
Expand All @@ -131,26 +135,27 @@
end
end

it "has an 'add another subject' link" do
create(:qualification,
category: "gcse",
results_count: 1,
job_application: job_application)

visit jobseekers_job_application_build_path(job_application, :qualifications)
context "with a GCSE" do
let(:qualifications) do
build_list(:qualification, 1,
qualification_results: build_list(:qualification_result, 1),
category: "gcse")
end

expect(page).to have_css(".detail-component", count: 1)
subject_list = page.find("dt.govuk-summary-list__key", text: "Subjects and grades").sibling("dd")
expect(subject_list).to have_css(".govuk-body", count: 1)
it "has an 'add another subject' link" do
expect(page).to have_css(".detail-component", count: 1)
subject_list = page.find("dt.govuk-summary-list__key", text: "Subjects and grades").sibling("dd")
expect(subject_list).to have_css(".govuk-body", count: 1)

click_on "Add another subject"
fill_in "Subject 2", with: "A second subject"
fill_in "jobseekers_qualifications_secondary_common_form[qualification_results_attributes][1][grade]", with: "B"
click_on "Save qualifications"
click_on "Add another subject"
fill_in "Subject 2", with: "A second subject"
fill_in "jobseekers_qualifications_secondary_common_form[qualification_results_attributes][1][grade]", with: "B"
click_on "Save qualifications"

expect(page).to have_css(".detail-component", count: 1)
subject_list = page.find("dt.govuk-summary-list__key", text: "Subjects and grades").sibling("dd")
expect(subject_list).to have_css(".govuk-body", count: 2)
expect(page).to have_css(".detail-component", count: 1)
subject_list = page.find("dt.govuk-summary-list__key", text: "Subjects and grades").sibling("dd")
expect(subject_list).to have_css(".govuk-body", count: 2)
end
end
end

Expand Down

0 comments on commit a0fa175

Please sign in to comment.