Skip to content

Commit

Permalink
Store answers on the journey session
Browse files Browse the repository at this point in the history
As part of the move to using the Journeys::Session model for persisting
answers, we want to store the poor performance answers.

I opted to pass the ID of the session into the form as a param after
considering the alternative of passing it in the constructor of the form
object.

This seemed like the least intrusive way, as it doesn't require any
changes to the interface of the form object.
  • Loading branch information
felixclack committed May 13, 2024
1 parent 68bab68 commit 918bd7a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/journey_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module JourneyConcern
extend ActiveSupport::Concern

included do
helper_method :current_journey_routing_name, :journey, :journey_configuration, :current_claim
helper_method :current_journey_routing_name, :journey, :journey_configuration, :current_claim, :journey_session
end

def current_journey_routing_name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Journeys
module AdditionalPaymentsForTeaching
class PoorPerformanceForm < Form
attribute :journey_session_id, :string
attribute :subject_to_formal_performance_action, :boolean
attribute :subject_to_disciplinary_action, :boolean

Expand All @@ -10,7 +11,14 @@ class PoorPerformanceForm < Form
def save
return false unless valid?

update!(eligibility_attributes: attributes)
update!(eligibility_attributes: attributes.except("journey_session_id"))
journey_session.update(answers: {eligibility_attributes: attributes.except("journey_session_id")})
end

private

def journey_session
@journey_session ||= Journeys::Session.find(journey_session_id)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"subject_to_disciplinary_action": "claim_subject_to_disciplinary_action_true"
}) if @form.errors.any? %>
<%= form_for @form, url: claim_path(current_journey_routing_name) do |form| %>
<%= form.hidden_field :journey_session_id, value: journey_session.id %>
<h1 class="govuk-heading-l">
<%= t("#{@form.i18n_namespace}.forms.poor_performance.questions.poor_performance") %>
</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
let(:claim) { CurrentClaim.new(claims: [ecp_claim, lupp_claim]) }
let(:slug) { "poor-performance" }
let(:params) { ActionController::Parameters.new({slug:, claim: claim_params}) }
let(:claim_params) { {subject_to_formal_performance_action: "true", subject_to_disciplinary_action: "false"} }
let(:journeys_session) { create(:journeys_session) }
let(:claim_params) { {subject_to_formal_performance_action: "true", subject_to_disciplinary_action: "false", journey_session_id: journeys_session.id} }

it { expect(form).to be_a(Form) }

Expand Down Expand Up @@ -61,7 +62,7 @@

context "#save" do
context "valid params" do
let(:claim_params) { {subject_to_formal_performance_action: "true", subject_to_disciplinary_action: "false"} }
let(:claim_params) { {subject_to_formal_performance_action: "true", subject_to_disciplinary_action: "false", journey_session_id: journeys_session.id} }

it "updates the attributes on the claim" do
expect { form.save }.to change { claim.eligibility.subject_to_formal_performance_action }.to(true)
Expand All @@ -70,7 +71,7 @@
end

context "invalid params" do
let(:claim_params) { {subject_to_formal_performance_action: "", subject_to_disciplinary_action: "false"} }
let(:claim_params) { {subject_to_formal_performance_action: "", subject_to_disciplinary_action: "false", journey_session_id: journeys_session.id} }

it "does not update the attributes on the claim" do
expect { form.save }.to not_change { claim.eligibility.subject_to_formal_performance_action }
Expand Down

0 comments on commit 918bd7a

Please sign in to comment.