Skip to content

Commit

Permalink
Add a form object for selecting a claim school
Browse files Browse the repository at this point in the history
When someone is on the student loans journey and they already have a
school associated with their claim, we ask them to confirm this.

The form object here looks similar to the one for choosing the correct
school #2704.
  • Loading branch information
felixclack committed May 8, 2024
1 parent 6c773c6 commit 32c19f8
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 123 deletions.
19 changes: 1 addition & 18 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def show
return
end

if params[:slug] == "select-claim-school"
update_session_with_tps_school(current_claim.tps_school_for_student_loan_in_previous_financial_year)
elsif params[:slug] == "postcode-search" && postcode
if params[:slug] == "postcode-search" && postcode
redirect_to claim_path(current_journey_routing_name, "select-home-address", {"claim[postcode]": params[:claim][:postcode], "claim[address_line_1]": params[:claim][:address_line_1]}) and return unless invalid_postcode?
elsif params[:slug] == "select-home-address" && postcode
session[:claim_postcode] = params[:claim][:postcode]
Expand Down Expand Up @@ -82,8 +80,6 @@ def update
end

case params[:slug]
when "select-claim-school"
check_select_claim_school_params
when "still-teaching"
check_still_teaching_params
else
Expand Down Expand Up @@ -250,19 +246,6 @@ def no_eligible_itt_subject?
!current_claim.eligible_itt_subject
end

def update_session_with_tps_school(school)
if school
session[:tps_school_id] = school.id
session[:tps_school_name] = school.name
session[:tps_school_address] = school.address
end
end

def check_select_claim_school_params
updated_claim_params = SelectClaimSchoolForm.extract_params(claim_params, change_school: params[:additional_school])
current_claim.attributes = updated_claim_params
end

def check_still_teaching_params
updated_claim_params = StillTeachingForm.extract_params(claim_params)
current_claim.attributes = updated_claim_params
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Journeys
module TeacherStudentLoanReimbursement
class SelectClaimSchoolForm < Form
attribute :change_school, :boolean
attribute :claim_school_id, :string

delegate :address, :name, to: :claim_school, prefix: true, allow_nil: true
delegate :eligibility, to: :claim
delegate :claim_school_somewhere_else?, to: :eligibility

def save
claim.update(eligibility_attributes:)
claim.reset_eligibility_dependent_answers(["claim_school_id"])
true
end

def claim_school_id
@claim_school_id ||= permitted_params.fetch(:claim_school_id, claim_school&.id)
end

private

def claim_school
@claim_school ||= claim.tps_school_for_student_loan_in_previous_financial_year || claim.eligibility.claim_school
end

def change_school?
change_school || claim_school_id.nil? || claim_school_id == "somewhere_else"
end

def eligibility_attributes
return {claim_school_id: nil, claim_school_somewhere_else: true} if change_school?

{claim_school_id:, claim_school_somewhere_else: false}
end
end
end
end
38 changes: 0 additions & 38 deletions app/forms/select_claim_school_form.rb

This file was deleted.

3 changes: 2 additions & 1 deletion app/models/journeys/teacher_student_loan_reimbursement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module TeacherStudentLoanReimbursement
"subjects-taught" => SubjectsTaughtForm,
"leadership-position" => LeadershipPositionForm,
"mostly-performed-leadership-duties" => MostlyPerformedLeadershipDutiesForm,
"reset-claim" => ResetClaimForm
"reset-claim" => ResetClaimForm,
"select-claim-school" => SelectClaimSchoolForm
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<div class="govuk-button-group">
<% if current_claim.eligibility.claim_school_somewhere_else == false %>
<%= button_to "Enter another school", claim_path(current_journey_routing_name, "select-claim-school", additional_school: true), method: :patch, class: "govuk-button", role: :button, data: { module: "govuk-button" } %>
<%= button_to "Enter another school", claim_path(current_journey_routing_name, "select-claim-school", claim: { change_school: true }), method: :patch, class: "govuk-button", role: :button, data: { module: "govuk-button" } %>
<% else %>
<div><%= link_to "Enter another school", claim_path(current_journey_routing_name, "claim-school", additional_school: true), class: "govuk-button", role: "button", data: {module: "govuk-button"} %></div>
<% end %>
Expand Down
22 changes: 10 additions & 12 deletions app/views/student_loans/claims/select_claim_school.html.erb
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
<%= render("shared/error_summary", instance: current_claim.eligibility) if current_claim.eligibility.errors.any? %>
<%= render("shared/error_summary", instance: @form) if @form.errors.any? %>

<%= form_for current_claim, url: claim_path(current_journey_routing_name) do |form| %>
<%= form_group_tag current_claim do %>
<%= form_with model: @form, url: claim_path(current_journey_routing_name) do |form| %>
<%= form_group_tag @form do %>
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<h1 class="govuk-fieldset__heading">
<%= claim_school_question %>
</h1>
</legend>

<%= errors_tag current_claim.eligibility, :current_school %>
<%= errors_tag @form.eligibility, :current_school %>

<div class="govuk-radios">
<%= form.fields_for :eligibility, include_id: false do |fields| %>
<div class="govuk-radios__item">
<%= fields.radio_button(:claim_school_id, session[:tps_school_id], class: "govuk-radios__input") %>
<%= fields.label "claim_school_id_#{session[:tps_school_id]}", session[:tps_school_name], class: "govuk-label govuk-radios__label" %>
<%= form.radio_button(:claim_school_id, @form.claim_school_id, class: "govuk-radios__input") %>
<%= form.label "claim_school_id_#{@form.claim_school_id}", @form.claim_school_name, class: "govuk-label govuk-radios__label" %>
<div id="school-checker-item-hint" class="govuk-hint govuk-radios__hint">
<%= session[:tps_school_address] %>
<%= @form.claim_school_address %>
</div>
</div>
<div class="govuk-radios__divider">or</div>
<div class="govuk-radios__item">
<%= fields.radio_button(:claim_school_id, "somewhere_else", checked: current_claim.eligibility.claim_school_somewhere_else?, class: "govuk-radios__input") %>
<%= fields.label "claim_school_id_somewhere_else", "Somewhere else", class: "govuk-label govuk-radios__label" %>
<%= form.radio_button(:claim_school_id, "somewhere_else", checked: @form.claim_school_somewhere_else?, class: "govuk-radios__input") %>
<%= form.label "claim_school_id_somewhere_else", "Somewhere else", class: "govuk-label govuk-radios__label" %>
</div>
<% end %>
</div>
</div>
</fieldset>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,5 @@ def navigate_to_claim_school_page(tps:, school:)
# - Select qts year
expect(page).to have_text(I18n.t("student_loans.forms.qts_year.questions.qts_award_year"))
choose_qts_year
click_on "Continue"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def navigate_to_still_teaching_page
# - Select qts year
expect(page).to have_text(I18n.t("student_loans.forms.qts_year.questions.qts_award_year"))
choose_qts_year
click_on "Continue"

# - Claim school
choose(eligible_claim_school.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "rails_helper"

RSpec.describe Journeys::TeacherStudentLoanReimbursement::SelectClaimSchoolForm, type: :model do
before do
create(
:journey_configuration,
:student_loans,
current_academic_year: AcademicYear.new(2023)
)
end

describe "#save" do
subject(:save) { form.save }

let(:claim) { CurrentClaim.new(claims: [create(:claim, policy: Policies::StudentLoans)]) }
let(:journey) { Journeys::TeacherStudentLoanReimbursement }
let(:params) { ActionController::Parameters.new }
let(:form) { described_class.new(claim:, journey:, params:) }
let!(:school) { create(:school, :eligible_for_journey, journey:) }

context "when choosing a school" do
let(:params) do
ActionController::Parameters.new({
claim: {
claim_school_id: school.id
}
})
end

it "updates the claim with the correct school attributes" do
expect { save }.to change { claim.reload.eligibility.claim_school_id }.to(school.id)
end

it "resets the somewhere_else attribute" do
expect { save }.to change { claim.reload.eligibility.claim_school_somewhere_else }.to eq(false)
end
end

context "with an existing school association and wants to change school" do
let(:params) do
ActionController::Parameters.new({
claim: {
claim_school_id: nil
}
})
end

before do
claim.eligibility.update!(claim_school_id: school.id, claim_school_somewhere_else: false)
end

it "resets the school association" do
expect { save }.to change { claim.reload.eligibility.claim_school_id }.to(nil)
end

it "resets the somewhere_else attribute" do
expect { save }.to change { claim.reload.eligibility.claim_school_somewhere_else }.to eq(true)
end
end
end
end
51 changes: 0 additions & 51 deletions spec/forms/select_claim_school_form_spec.rb

This file was deleted.

0 comments on commit 32c19f8

Please sign in to comment.