Skip to content

Commit

Permalink
Make journey_session available in forms
Browse files Browse the repository at this point in the history
We'll soon be writing answers to the journey session as well as to the
form object. This commit makes the journey session available to, and
required in, the forms.
As we use a form object in the slug sequence and the slug sequence is
called by the page sequence we've had to pass the journey session
through those classes as well.
  • Loading branch information
rjlynch committed May 13, 2024
1 parent 68bab68 commit 71d8aba
Show file tree
Hide file tree
Showing 54 changed files with 417 additions and 77 deletions.
18 changes: 14 additions & 4 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def show
end

# TODO: Migrate the remaining slugs to form objects.
if @form ||= journey.form(claim: current_claim, params:)
if @form ||= journey.form(claim: current_claim, journey_session:, params:)
set_any_backlink_override
render current_template
return
Expand Down Expand Up @@ -63,7 +63,7 @@ def update
params[:claim][:hmrc_validation_attempt_count] = session[:hmrc_validation_attempt_count] || 0 if on_banking_page?

# TODO: Migrate the remaining slugs to form objects.
if (@form = journey.form(claim: current_claim, params:))
if (@form = journey.form(claim: current_claim, journey_session:, params:))
if @form.save
retrieve_student_loan_details
update_session_with_selected_policy
Expand Down Expand Up @@ -120,7 +120,12 @@ def next_slug

def redirect_to_existing_claim_journey
new_journey = Journeys.for_policy(current_claim.policy)
new_page_sequence = new_journey.page_sequence_for_claim(current_claim, session[:slugs], params[:slug])
new_page_sequence = new_journey.page_sequence_for_claim(
current_claim,
journey_session,
session[:slugs],
params[:slug]
)
redirect_to(claim_path(new_journey::ROUTING_NAME, slug: new_page_sequence.next_required_slug))
end

Expand Down Expand Up @@ -197,7 +202,12 @@ def claim_in_progress?
end

def page_sequence
@page_sequence ||= journey.page_sequence_for_claim(current_claim, session[:slugs], params[:slug])
@page_sequence ||= journey.page_sequence_for_claim(
current_claim,
journey_session,
session[:slugs],
params[:slug]
)
end

def prepend_view_path_for_journey
Expand Down
2 changes: 1 addition & 1 deletion app/forms/claim_school_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ClaimSchoolForm < Form
validates :claim_school_id, presence: {message: i18n_error_message(:select_a_school)}
validate :claim_school_must_exist, if: -> { claim_school_id.present? }

def initialize(claim:, journey:, params:)
def initialize(claim:, journey_session:, journey:, params:)
super

load_schools
Expand Down
2 changes: 1 addition & 1 deletion app/forms/current_school_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CurrentSchoolForm < Form
validates :current_school_id, presence: {message: i18n_error_message(:select_the_school_you_teach_at)}
validate :current_school_must_be_open, if: -> { current_school_id.present? }

def initialize(claim:, journey:, params:)
def initialize(claim:, journey_session:, journey:, params:)
super

load_schools
Expand Down
6 changes: 4 additions & 2 deletions app/forms/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Form

attr_accessor :claim
attr_accessor :journey
attr_accessor :journey_session
attr_accessor :params

delegate :persisted?, to: :claim
Expand All @@ -18,7 +19,8 @@ def self.i18n_error_message(path)
->(object, _) { object.i18n_errors_path(path) }
end

def initialize(claim:, journey:, params:)
# TODO RL: remove journey param and pull it from the journey_session
def initialize(claim:, journey_session:, journey:, params:)
super

assign_attributes(attributes_with_current_value)
Expand Down Expand Up @@ -67,7 +69,7 @@ def i18n_form_namespace
def page_sequence
@page_sequence ||= Journeys::PageSequence.new(
claim,
journey.slug_sequence.new(claim),
journey.slug_sequence.new(claim, journey_session),
nil,
params[:slug]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def passed_details_check_with_teacher_id?
def set_qualification!
QualificationForm.new(
journey: journey,
journey_session: journey_session,
claim: claim,
params: ActionController::Parameters.new(
claim: {
Expand Down
2 changes: 1 addition & 1 deletion app/forms/personal_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def future?
validates :national_insurance_number, presence: {message: "Enter a National Insurance number in the correct format"}
validate :ni_number_is_correct_format

def initialize(claim:, journey:, params:)
def initialize(claim:, journey_session:, journey:, params:)
super
assign_date_attributes
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ class SlugSequence
RESULTS_SLUGS
).freeze

attr_reader :claim
attr_reader :claim, :journey_session

# Really this is a combined CurrentClaim
def initialize(claim)
def initialize(claim, journey_session)
@claim = claim
@journey_session = journey_session
end

# Even though we are inside the ECP namespace, this method can modify the
Expand Down Expand Up @@ -179,6 +180,7 @@ def self.start_page_url
def personal_details_form
PersonalDetailsForm.new(
claim:,
journey_session:,
journey: Journeys::AdditionalPaymentsForTeaching,
params: ActionController::Parameters.new
)
Expand Down
13 changes: 9 additions & 4 deletions app/models/journeys/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ def slug_sequence
self::SlugSequence
end

def form(claim:, params:)
def form(claim:, journey_session:, params:)
form = SHARED_FORMS.merge(forms)[params[:slug]]

form&.new(journey: self, claim:, params:)
form&.new(journey: self, journey_session:, claim:, params:)
end

def forms
defined?(self::FORMS) ? self::FORMS : {}
end

def page_sequence_for_claim(claim, completed_slugs, current_slug)
PageSequence.new(claim, slug_sequence.new(claim), completed_slugs, current_slug)
def page_sequence_for_claim(claim, journey_session, completed_slugs, current_slug)
PageSequence.new(
claim,
slug_sequence.new(claim, journey_session),
completed_slugs,
current_slug
)
end

def answers_presenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ class SlugSequence
RESULTS_SLUGS
).freeze

attr_reader :claim
attr_reader :claim, :journey_session

def initialize(claim)
def initialize(claim, journey_session)
@claim = claim
@journey_session = journey_session
end

def slugs
Expand Down Expand Up @@ -132,6 +133,7 @@ def self.start_page_url
def personal_details_form
PersonalDetailsForm.new(
claim:,
journey_session:,
journey: Journeys::TeacherStudentLoanReimbursement,
params: ActionController::Parameters.new
)
Expand Down
5 changes: 4 additions & 1 deletion spec/forms/address_form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require "rails_helper"

RSpec.describe AddressForm, type: :model do
subject(:form) { described_class.new(claim:, journey:, params:) }
subject(:form) { described_class.new(claim:, journey:, params:, journey_session:) }

let(:journey) { Journeys::TeacherStudentLoanReimbursement }
let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end
let(:claim) { CurrentClaim.new(claims: [build(:claim, policy: Policies::StudentLoans)]) }
let(:slug) { "address" }
let(:params) { ActionController::Parameters.new({slug:, claim: claim_params}) }
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/bank_details_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@
CurrentClaim.new(claims: claims)
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:slug) { "personal-bank-account" }
let(:params) do
{banking_name:, bank_sort_code:, bank_account_number:, building_society_roll_number:, hmrc_validation_attempt_count:}
end

subject(:form) { described_class.new(claim: current_claim, journey: journey, params: ActionController::Parameters.new(slug:, claim: params)) }
subject(:form) do
described_class.new(
claim: current_claim,
journey_session: journey_session,
journey: journey,
params: ActionController::Parameters.new(slug:, claim: params)
)
end

let(:banking_name) { "Jo Bloggs" }
let(:bank_sort_code) { rand(100000..999999) }
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/bank_or_building_society_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@
CurrentClaim.new(claims:)
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:slug) { "bank-or-building-society-form" }
let(:claim_params) { {} }

subject(:form) { described_class.new(claim: current_claim, journey: journey, params: ActionController::Parameters.new({slug:, claim: claim_params})) }
subject(:form) do
described_class.new(
claim: current_claim,
journey_session: journey_session,
journey: journey,
params: ActionController::Parameters.new({slug:, claim: claim_params})
)
end

describe "validations" do
it { should allow_value(%w[personal_bank_account building_society]).for(:bank_or_building_society).with_message("Select if you want the money paid in to a personal bank account or building society") }
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/claim_school_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@
CurrentClaim.new(claims: claims)
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:slug) { "claim-school" }

subject(:form) { described_class.new(claim: current_claim, journey: journey, params: params) }
subject(:form) do
described_class.new(
claim: current_claim,
journey_session: journey_session,
journey: journey,
params: params
)
end

context "unpermitted claim param" do
let(:params) { ActionController::Parameters.new({slug: slug, claim: {nonsense_id: 1}}) }
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/current_school_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@
CurrentClaim.new(claims: claims)
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:slug) { "current-school" }

subject(:form) { described_class.new(claim: current_claim, journey: journey, params: params) }
subject(:form) do
described_class.new(
claim: current_claim,
journey_session: journey_session,
journey: journey,
params: params
)
end

context "unpermitted claim param" do
let(:params) { ActionController::Parameters.new({slug: slug, claim: {nonsense_id: 1}}) }
Expand Down
11 changes: 10 additions & 1 deletion spec/forms/email_address_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@
end
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:current_claim) { CurrentClaim.new(claims: claims) }

let(:params) do
ActionController::Parameters.new(claim: {email_address: email_address})
end

let(:form) do
described_class.new(journey: journey, claim: current_claim, params: params)
described_class.new(
journey: journey,
journey_session: journey_session,
claim: current_claim,
params: params
)
end

describe "validations" do
Expand Down
11 changes: 10 additions & 1 deletion spec/forms/email_verification_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@
)
end

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:form) do
described_class.new(journey: journey, claim: current_claim, params: params)
described_class.new(
journey: journey,
journey_session: journey_session,
claim: current_claim,
params: params
)
end

around do |example|
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/employed_directly_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@

let(:journey) { Journeys::AdditionalPaymentsForTeaching }

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:current_claim) do
claims = journey::POLICIES.map { |policy| create(:claim, policy:) }
CurrentClaim.new(claims:)
end

let(:slug) { "employed-directly" }

subject(:form) { described_class.new(claim: current_claim, journey:, params:) }
subject(:form) do
described_class.new(
claim: current_claim,
journey_session:,
journey:,
params:
)
end

context "unpermitted claim param" do
let(:params) { ActionController::Parameters.new({slug:, claim: {random_param: 1}}) }
Expand Down
13 changes: 12 additions & 1 deletion spec/forms/entire_term_contract_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@

let(:journey) { Journeys::AdditionalPaymentsForTeaching }

let(:journey_session) do
build(:journeys_session, journey: journey::ROUTING_NAME)
end

let(:current_claim) do
claims = journey::POLICIES.map { |policy| create(:claim, policy:) }
CurrentClaim.new(claims:)
end

let(:slug) { "entire-term-contract" }

subject(:form) { described_class.new(claim: current_claim, journey:, params:) }
subject(:form) do
described_class.new(
claim: current_claim,
journey:,
journey_session:,
params:
)
end

context "unpermitted claim param" do
let(:params) { ActionController::Parameters.new({slug:, claim: {random_param: 1}}) }
Expand Down
Loading

0 comments on commit 71d8aba

Please sign in to comment.