From 918bd7a2147cc81f08631895b1e3368012629c2b Mon Sep 17 00:00:00 2001 From: Felix Clack Date: Mon, 13 May 2024 11:24:56 +0100 Subject: [PATCH] Store answers on the journey session 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. --- app/controllers/concerns/journey_concern.rb | 2 +- .../poor_performance_form.rb | 10 +++++++++- .../claims/poor_performance.html.erb | 1 + .../poor_performance_form_spec.rb | 7 ++++--- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/concerns/journey_concern.rb b/app/controllers/concerns/journey_concern.rb index ba66482304..b0523f701a 100644 --- a/app/controllers/concerns/journey_concern.rb +++ b/app/controllers/concerns/journey_concern.rb @@ -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 diff --git a/app/forms/journeys/additional_payments_for_teaching/poor_performance_form.rb b/app/forms/journeys/additional_payments_for_teaching/poor_performance_form.rb index efb76f4231..be23edc496 100644 --- a/app/forms/journeys/additional_payments_for_teaching/poor_performance_form.rb +++ b/app/forms/journeys/additional_payments_for_teaching/poor_performance_form.rb @@ -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 @@ -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 diff --git a/app/views/additional_payments/claims/poor_performance.html.erb b/app/views/additional_payments/claims/poor_performance.html.erb index 07206acecd..6a27091220 100644 --- a/app/views/additional_payments/claims/poor_performance.html.erb +++ b/app/views/additional_payments/claims/poor_performance.html.erb @@ -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 %>

<%= t("#{@form.i18n_namespace}.forms.poor_performance.questions.poor_performance") %>

diff --git a/spec/forms/journeys/additional_payments_for_teaching/poor_performance_form_spec.rb b/spec/forms/journeys/additional_payments_for_teaching/poor_performance_form_spec.rb index d3bddafcf1..720ea304ec 100644 --- a/spec/forms/journeys/additional_payments_for_teaching/poor_performance_form_spec.rb +++ b/spec/forms/journeys/additional_payments_for_teaching/poor_performance_form_spec.rb @@ -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) } @@ -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) @@ -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 }