Skip to content

Commit

Permalink
Merge pull request #2873 from DFE-Digital/reset-dependent-answers
Browse files Browse the repository at this point in the history
Remove unused method reset_dependent_answers
  • Loading branch information
felixclack authored Jun 19, 2024
2 parents af5bd07 + 7ebf7ad commit bb07c2e
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 291 deletions.
4 changes: 2 additions & 2 deletions app/forms/personal_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def save
national_insurance_number:
)

reset_depenent_answers_attributes
reset_dependent_answers_attributes

journey_session.save!
end
Expand Down Expand Up @@ -139,7 +139,7 @@ def has_valid_nino?
errors.exclude?(:national_insurance_number)
end

def reset_depenent_answers_attributes
def reset_dependent_answers_attributes
journey_session.answers.assign_attributes(
has_student_loan: nil,
student_loan_plan: nil
Expand Down
23 changes: 0 additions & 23 deletions app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ class Claim < ApplicationRecord
}.freeze
DECISION_DEADLINE = 12.weeks
DECISION_DEADLINE_WARNING_POINT = 2.weeks
ATTRIBUTE_DEPENDENCIES = {
"national_insurance_number" => ["has_student_loan", "student_loan_plan", "eligibility.student_loan_repayment_amount"],
"date_of_birth" => ["has_student_loan", "student_loan_plan", "eligibility.student_loan_repayment_amount"],
"bank_or_building_society" => ["banking_name", "bank_account_number", "bank_sort_code", "building_society_roll_number"],
"provide_mobile_number" => ["mobile_number", "mobile_verified"],
"mobile_number" => ["mobile_verified"],
"email_address" => ["email_verified"]
}.freeze

# Use AcademicYear as custom ActiveRecord attribute type
attribute :academic_year, AcademicYear::Type.new
Expand Down Expand Up @@ -348,21 +340,6 @@ def self.filtered_params
FILTER_PARAMS.select { |_, v| v }.keys
end

def reset_dependent_answers
ATTRIBUTE_DEPENDENCIES.each do |attribute_name, dependent_attribute_names|
dependent_attribute_names.each do |dependent_attribute_name|
next unless changed.include?(attribute_name)

target_model, dependent_attribute_name = dependent_attribute_name.split(".") if dependent_attribute_name.include?(".")
target_model ||= "itself"

next unless send(target_model).has_attribute?(dependent_attribute_name)

send(target_model).write_attribute(dependent_attribute_name, nil)
end
end
end

def policy
eligibility&.policy
end
Expand Down
25 changes: 0 additions & 25 deletions app/models/policies/early_career_payments/eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ class Eligibility < ApplicationRecord
include EligibilityCheckable

AMENDABLE_ATTRIBUTES = [:award_amount].freeze
ATTRIBUTE_DEPENDENCIES = {
"employed_as_supply_teacher" => ["has_entire_term_contract", "employed_directly"],
"qualification" => ["eligible_itt_subject", "teaching_subject_now"],
"eligible_itt_subject" => ["teaching_subject_now"],
"itt_academic_year" => ["eligible_itt_subject"]
}.freeze

IGNORED_ATTRIBUTES = [
"eligible_degree_subject"
Expand Down Expand Up @@ -79,25 +73,6 @@ def assign_attributes(*args, **kwargs)
all_attributes_ignored = (args.first.keys - IGNORED_ATTRIBUTES).empty?
raise unless all_attributes_ignored
end

def reset_dependent_answers(reset_attrs = [])
attrs = ineligible? ? changed.concat(reset_attrs) : changed

dependencies = ATTRIBUTE_DEPENDENCIES.dup

# If some data was derived from DQT we do not want to reset these.
if claim.qualifications_details_check
dependencies.delete("qualification")
dependencies.delete("eligible_itt_subject")
dependencies.delete("itt_academic_year")
end

dependencies.each do |attribute_name, dependent_attribute_names|
dependent_attribute_names.each do |dependent_attribute_name|
write_attribute(dependent_attribute_name, nil) if attrs.include?(attribute_name)
end
end
end
end
end
end
26 changes: 0 additions & 26 deletions app/models/policies/levelling_up_premium_payments/eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class Eligibility < ApplicationRecord

AMENDABLE_ATTRIBUTES = [:award_amount].freeze

ATTRIBUTE_DEPENDENCIES = {
"employed_as_supply_teacher" => ["has_entire_term_contract", "employed_directly"],
"qualification" => ["eligible_itt_subject", "teaching_subject_now"],
"eligible_itt_subject" => ["teaching_subject_now", "eligible_degree_subject"],
"itt_academic_year" => ["eligible_itt_subject"]
}.freeze

FIRST_ITT_AY = "2017/2018"
LAST_POLICY_YEAR = "2024/2025"

Expand Down Expand Up @@ -59,25 +52,6 @@ class Eligibility < ApplicationRecord
none_of_the_above: 4,
computing: 5
}, _prefix: :itt_subject

def reset_dependent_answers(reset_attrs = [])
attrs = ineligible? ? changed.concat(reset_attrs) : changed

dependencies = ATTRIBUTE_DEPENDENCIES.dup

# If some data was derived from DQT we do not want to reset these.
if claim.qualifications_details_check
dependencies.delete("qualification")
dependencies.delete("eligible_itt_subject")
dependencies.delete("itt_academic_year")
end

dependencies.each do |attribute_name, dependent_attribute_names|
dependent_attribute_names.each do |dependent_attribute_name|
write_attribute(dependent_attribute_name, nil) if attrs.include?(attribute_name)
end
end
end
end
end
end
14 changes: 0 additions & 14 deletions app/models/policies/student_loans/eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class Eligibility < ApplicationRecord
:languages_taught
].freeze
AMENDABLE_ATTRIBUTES = %i[student_loan_repayment_amount].freeze
ATTRIBUTE_DEPENDENCIES = {
"claim_school_id" => ["taught_eligible_subjects", *SUBJECT_ATTRIBUTES, "employment_status", "current_school_id"],
"had_leadership_position" => ["mostly_performed_leadership_duties"]
}.freeze

self.table_name = "student_loans_eligibilities"

Expand Down Expand Up @@ -74,16 +70,6 @@ def award_amount
def eligible_itt_subject
end

def reset_dependent_answers(reset_attrs = [])
attrs = ineligible? ? changed.concat(reset_attrs) : changed

ATTRIBUTE_DEPENDENCIES.each do |attribute_name, dependent_attribute_names|
dependent_attribute_names.each do |dependent_attribute_name|
write_attribute(dependent_attribute_name, nil) if attrs.include?(attribute_name)
end
end
end

def submit!
end

Expand Down
73 changes: 0 additions & 73 deletions spec/models/claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,79 +563,6 @@
end
end

describe "#reset_dependent_answers" do
let(:claim) { create(:claim, :submittable, policy:, bank_or_building_society: "building_society") }
let(:policy) { Policies::LevellingUpPremiumPayments }

it "redetermines the bank_details when the value of bank_or_building_society changes" do
expect { claim.reset_dependent_answers }.not_to change { claim.attributes }

claim.bank_or_building_society = "personal_bank_account"
claim.banking_name = "Mr David McCorkindale-Travers"
claim.bank_sort_code = "984530"
claim.bank_account_number = "66320109"

expect(claim.banking_name).to eq "Mr David McCorkindale-Travers"
expect(claim.bank_account_number).to eq "66320109"
expect(claim.bank_sort_code).to eq "984530"
expect(claim.building_society_roll_number).to be_nil
end

it "resets mobile_number when the value of provide_mobile_number changes" do
expect { claim.reset_dependent_answers }.not_to change { claim.attributes }

claim.provide_mobile_number = false
claim.reset_dependent_answers

expect(claim.provide_mobile_number).to eq false
expect(claim.mobile_number).to be_nil
end

it "resets mobile_verified when the value of mobile_number changes" do
expect { claim.reset_dependent_answers }.not_to change { claim.attributes }

claim.mobile_number = "07425999124"
claim.reset_dependent_answers

expect(claim.mobile_number).to eq "07425999124"
expect(claim.mobile_verified).to be_nil
end

[Policies::StudentLoans, Policies::EarlyCareerPayments, Policies::LevellingUpPremiumPayments].each do |policy|
context "when the policy is #{policy}" do
let(:policy) { policy }

before do
claim.has_student_loan = true
claim.student_loan_plan = StudentLoan::PLAN_1
claim.eligibility.student_loan_repayment_amount = 100 if policy == Policies::StudentLoans
end

it "resets student loan attributes when the value of national_insurance_number changes" do
expect { claim.reset_dependent_answers }.not_to change { claim.attributes }

claim.national_insurance_number = "AB123456X"
claim.reset_dependent_answers

expect(claim.has_student_loan).to be_nil
expect(claim.student_loan_plan).to be_nil
expect(claim.eligibility.student_loan_repayment_amount).to be_nil if policy == Policies::StudentLoans
end

it "resets student loan attributes when the value of date_of_birth changes" do
expect { claim.reset_dependent_answers }.not_to change { claim.attributes }

claim.date_of_birth = "1/1/1991"
claim.reset_dependent_answers

expect(claim.has_student_loan).to be_nil
expect(claim.student_loan_plan).to be_nil
expect(claim.eligibility.student_loan_repayment_amount).to be_nil if policy == Policies::StudentLoans
end
end
end
end

describe ".filtered_params" do
it "returns a list of sensitive params to be filtered" do
expect(Claim.filtered_params).to match_array([
Expand Down
68 changes: 0 additions & 68 deletions spec/models/early_career_payments/eligibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,74 +100,6 @@
end
end

describe "#reset_dependent_answers" do
let!(:journey_configuration) { create(:journey_configuration, :additional_payments) }
let!(:claim) { build_stubbed(:claim, :with_student_loan, eligibility: eligibility) }

let(:eligibility) do
build_stubbed(
:early_career_payments_eligibility,
:eligible,
employed_as_supply_teacher: true,
has_entire_term_contract: false,
employed_directly: false,
qualification: :undergraduate_itt,
eligible_itt_subject: :none_of_the_above,
teaching_subject_now: false
)
end

it "resets 'eligible_itt_subject' when value of 'qualification' changes" do
eligibility.qualification = :undergraduate_itt
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.qualification = :postgraduate_itt
expect { eligibility.reset_dependent_answers }
.to change { eligibility.eligible_itt_subject }
.from("none_of_the_above").to(nil)
end

it "resets 'teaching_subject_now' when value of 'qualification' changes" do
eligibility.qualification = :undergraduate_itt
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.qualification = :postgraduate_itt
expect { eligibility.reset_dependent_answers }
.to change { eligibility.teaching_subject_now }
.from(false).to(nil)
end

it "resets 'teaching_subject_now' when value of 'eligible_itt_subject' changes" do
eligibility.eligible_itt_subject = :none_of_the_above
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.eligible_itt_subject = :foreign_languages
expect { eligibility.reset_dependent_answers }
.to change { eligibility.teaching_subject_now }
.from(false).to(nil)
end

it "resets 'has_entire_term_contract' when the value of 'employed_as_supply_teacher' changes" do
eligibility.employed_as_supply_teacher = true
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.employed_as_supply_teacher = false
expect { eligibility.reset_dependent_answers }
.to change { eligibility.has_entire_term_contract }
.from(false).to(nil)
end

it "resets 'employed_directly' when the value of 'employed_as_supply_teacher' changes" do
eligibility.employed_as_supply_teacher = true
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.employed_as_supply_teacher = false
expect { eligibility.reset_dependent_answers }
.to change { eligibility.employed_directly }
.from(false).to(nil)
end
end

describe "#trainee_teacher?" do
let(:eligibility) { build_stubbed(:early_career_payments_eligibility, nqt_in_academic_year_after_itt: false) }

Expand Down
60 changes: 0 additions & 60 deletions spec/models/student_loans/eligibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,66 +278,6 @@
end
end

describe "#reset_dependent_answers" do
let(:eligibility) do
create(
:student_loans_eligibility,
:eligible,
claim_school: build(:school, :student_loans_eligible),
current_school: build(:school, :student_loans_eligible),
taught_eligible_subjects: true,
chemistry_taught: true,
physics_taught: true,
computing_taught: true,
languages_taught: true,
employment_status: :different_school,
had_leadership_position: true,
claim: build(:claim)
)
end

it "resets employment_status when the value of claim_school changes" do
eligibility.claim_school = eligibility.claim_school # standard:disable Lint/SelfAssignment
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.claim_school = eligibility.current_school
expect { eligibility.reset_dependent_answers }
.to change { eligibility.employment_status }
.from("different_school").to(nil)
end

it "resets the current_school_id to nil when the value of claim_school changes" do
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.claim_school = eligibility.current_school
expect { eligibility.reset_dependent_answers }
.to change { eligibility.current_school_id }
.from(eligibility.current_school.id).to(nil)
end

it "resets the subject fields when the value of the claim_school changes" do
eligibility.claim_school = eligibility.current_school
eligibility.reset_dependent_answers

expect(eligibility.taught_eligible_subjects).to eq(nil)
expect(eligibility.physics_taught).to eq(nil)
expect(eligibility.chemistry_taught).to eq(nil)
expect(eligibility.physics_taught).to eq(nil)
expect(eligibility.computing_taught).to eq(nil)
expect(eligibility.languages_taught).to eq(nil)
end

it "resets mostly_performed_leadership_duties when the value of had_leadership_position changes" do
eligibility.had_leadership_position = true
expect { eligibility.reset_dependent_answers }.not_to change { eligibility.attributes }

eligibility.had_leadership_position = false
expect { eligibility.reset_dependent_answers }
.to change { eligibility.mostly_performed_leadership_duties }
.from(false).to(nil)
end
end

context "when saving in the “submit” context" do
it "is valid when all attributes are present" do
expect(build(:student_loans_eligibility, :eligible)).to be_valid(:submit)
Expand Down

0 comments on commit bb07c2e

Please sign in to comment.