Skip to content

Commit

Permalink
Remove loading answers from claim
Browse files Browse the repository at this point in the history
Removes the hacks we had to put into the journey session to check if it
contained the answer to a question.
We've left setting attributes from the claim on the reminders forms as
they still write some otp data to the claim / do some weired stuff with
writing data from the session to the form.
  • Loading branch information
rjlynch committed Jun 13, 2024
1 parent ec5af4e commit ae72a96
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 87 deletions.
18 changes: 1 addition & 17 deletions app/forms/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ def attributes_with_current_value
end

def load_current_value(attribute)
if journey_session.answered?(attribute)
return journey_session.answers.public_send(attribute)
end

# TODO: re-implement when the underlying claim and eligibility data sources
# are moved to an alternative place e.g. a session hash

# Some, but not all attributes are present directly on the claim record.
return claim.public_send(attribute) if claim.has_attribute?(attribute)

# At the moment, some attributes are unique to a policy eligibility record,
# so we need to loop through all the claims in the wrapper and check each
# eligibility individually; if the search fails, it should return `nil`.
claim.claims.each do |c|
return c.eligibility.public_send(attribute) if c.eligibility.has_attribute?(attribute)
end
nil
journey_session.answers.public_send(attribute) if journey_session.answers.has_attribute?(attribute)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def sent_one_time_password_at?
rescue Date::Error
false
end

def load_current_value(attribute)
# TODO: re-implement when the underlying claim and eligibility data sources
# are moved to an alternative place e.g. a session hash

# Some, but not all attributes are present directly on the claim record.
return claim.public_send(attribute) if claim.has_attribute?(attribute)

# At the moment, some attributes are unique to a policy eligibility record,
# so we need to loop through all the claims in the wrapper and check each
# eligibility individually; if the search fails, it should return `nil`.
claim.claims.each do |c|
return c.eligibility.public_send(attribute) if c.eligibility.has_attribute?(attribute)
end
nil
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def save
def i18n_form_namespace
"reminders.#{super}"
end

def load_current_value(attribute)
# TODO: re-implement when the underlying claim and eligibility data sources
# are moved to an alternative place e.g. a session hash

# Some, but not all attributes are present directly on the claim record.
return claim.public_send(attribute) if claim.has_attribute?(attribute)

# At the moment, some attributes are unique to a policy eligibility record,
# so we need to loop through all the claims in the wrapper and check each
# eligibility individually; if the search fails, it should return `nil`.
claim.claims.each do |c|
return c.eligibility.public_send(attribute) if c.eligibility.has_attribute?(attribute)
end
nil
end
end
end
end
Expand Down
26 changes: 0 additions & 26 deletions app/models/journeys/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@ def submitted?
claim.present?
end

# This method and the associated `before_save` callback are temporary
# methods while we're working with both a current claim and journey
# session.
# When setting default values in a form object we need to know if the
# answer was stored on the journey session or whether we should check the
# current claim. Values for answers may be `nil`, so we need to explicitly
# check that the question was answered.
# Once all forms has been migrated to use the journey session, this method,
# the before_save and after_initialize call backs and the
# SessionAnswer#answered attribute can be removed.
# This will be removed in
# https://dfedigital.atlassian.net.mcas.ms/browse/CAPT-1637
def answered?(attribute_name)
answers.answered.include?(attribute_name.to_s)
end

after_initialize do
answers.clear_changes_information
end

before_save do
unless answers.answered_changed? # Allow overwriting answered attributes
answers.answered += answers.changes.keys.map(&:to_s)
end
end

def logged_in_with_tid_and_has_recent_tps_school?
answers.trn_from_tid? && recent_tps_school.present?
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/journeys/session_answers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class SessionAnswers
attribute :student_loan_plan, :string
attribute :submitted_using_slc_data, :boolean
attribute :sent_one_time_password_at, :datetime
attribute :answered, default: []

def has_attribute?(name)
attribute_names.include?(name.to_s)
Expand Down Expand Up @@ -97,5 +96,9 @@ def qualifications_details_check?
def has_student_loan?
!!has_student_loan
end

def email_verified?
!!email_verified
end
end
end
16 changes: 3 additions & 13 deletions spec/forms/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def initialize(claim, journey_session)
:student_loans_session,
answers: {
first_name: "existing-name",
student_loan_repayment_amount: 2000,
answered: %w[first_name student_loan_repayment_amount]
student_loan_repayment_amount: 2000
}
)
end
Expand Down Expand Up @@ -108,8 +107,7 @@ def initialize(claim, journey_session)
:student_loans_session,
answers: {
first_name: "existing-name",
student_loan_repayment_amount: nil,
answered: %w[first_name student_loan_repayment_amount]
student_loan_repayment_amount: nil
}
)
end
Expand All @@ -122,15 +120,7 @@ def initialize(claim, journey_session)
end
end

context "when an existing value can be found on the claim or eligibility record" do
let(:claims) { [build(:claim, first_name: "existing-name", eligibility_attributes: {student_loan_repayment_amount: 100}, policy: Policies::StudentLoans)] }

it "initialises the attributes with values from the claim" do
expect(form).to have_attributes(first_name: "existing-name", student_loan_repayment_amount: 100)
end
end

context "when an existing value cannot be found on the claim nor eligibility nor journey session" do
context "when an existing value cannot be found on journey session" do
let(:claims) { [build(:claim, first_name: nil, policy: Policies::StudentLoans)] }

it "initialises the attributes with nil" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
context "when the subject is taught" do
before do
journey_session.answers.biology_taught = true
journey_session.answers.answered = ["biology_taught"]
end

it { expect(form.subject_taught_selected?(:biology_taught)).to eq(true) }
Expand All @@ -129,7 +128,6 @@
context "when the subject is not taught" do
before do
journey_session.answers.biology_taught = false
journey_session.answers.answered = ["biology_taught"]
end

it { expect(form.subject_taught_selected?(:biology_taught)).to eq(false) }
Expand Down
1 change: 1 addition & 0 deletions spec/lib/ineligibility_reason_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
build(
:additional_payments_answers,
:ecp_eligible,
:first_lup_claim_year,
:insufficient_teaching
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@
journey_session.answers.date_of_birth = dob
journey_session.answers.national_insurance_number = "JH001234D"

journey_session.answers.answered = %w[
first_name
surname
date_of_birth
national_insurance_number
]

expect(slug_sequence.slugs).not_to include("personal-details")
end

Expand All @@ -208,13 +201,6 @@
journey_session.answers.date_of_birth = 30.years.ago.to_date
journey_session.answers.national_insurance_number = nil

journey_session.answers.answered = %w[
first_name
surname
date_of_birth
national_insurance_number
]

expect(slug_sequence.slugs).to include("personal-details")
end

Expand All @@ -224,13 +210,6 @@
journey_session.answers.date_of_birth = 30.years.ago.to_date
journey_session.answers.national_insurance_number = "JH001234D"

journey_session.answers.answered = %w[
first_name
surname
date_of_birth
national_insurance_number
]

expect(slug_sequence.slugs).to include("personal-details")
end

Expand All @@ -240,13 +219,6 @@
journey_session.answers.date_of_birth = nil
journey_session.answers.national_insurance_number = "JH001234D"

journey_session.answers.answered = %w[
first_name
surname
date_of_birth
national_insurance_number
]

expect(slug_sequence.slugs).to include("personal-details")
end
end
Expand Down

0 comments on commit ae72a96

Please sign in to comment.