Browse Source

Merge branch 'main' into CLDC-510-income-ranges

pull/58/head
baarkerlounger 4 years ago
parent
commit
c77967a220
  1. 14
      app/models/case_log.rb
  2. 2
      spec/helpers/check_answers_helper_spec.rb
  3. 29
      spec/models/case_log_spec.rb

14
app/models/case_log.rb

@ -43,14 +43,26 @@ class CaseLogValidator < ActiveModel::Validator
return unless record.tenant_economic_status && record.weekly_net_income return unless record.tenant_economic_status && record.weekly_net_income
applicable_income_range = IncomeRange.find_by(economic_status: record.tenant_economic_status) applicable_income_range = IncomeRange.find_by(economic_status: record.tenant_economic_status)
if record.weekly_net_income > applicable_income_range.hard_max if record.weekly_net_income > applicable_income_range.hard_max
record.errors.add :net_income, "Net income cannot be greater than #{applicable_income_range.hard_max} given the tenant's working situation" record.errors.add :net_income, "Net income cannot be greater than #{applicable_income_range.hard_max} given the tenant's working situation"
end end
if record.weekly_net_income < applicable_income_range.hard_max if record.weekly_net_income < applicable_income_range.hard_max
record.errors.add :net_income, "Net income cannot be less than #{applicable_income_range.hard_min} given the tenant's working situation" record.errors.add :net_income, "Net income cannot be less than #{applicable_income_range.hard_min} given the tenant's working situation"
end end
end end
def validate_armed_forces_injured(record)
if (record.armed_forces == "Yes - a regular" || record.armed_forces == "Yes - a reserve") && record.armed_forces_injured.blank?
record.errors.add :armed_forces_injured, "You must answer the armed forces injury question if the tenant has served in the armed forces"
end
if (record.armed_forces == "No" || record.armed_forces == "Prefer not to say") && record.armed_forces_injured.present?
record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen"
end
end
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload # that have just been submitted. If we're submitting a log via API or Bulk Upload
@ -114,8 +126,6 @@ class CaseLog < ApplicationRecord
((net_income * 12) / 52.0).round(0) ((net_income * 12) / 52.0).round(0)
when "Yearly" when "Yearly"
(net_income / 12.0).round(0) (net_income / 12.0).round(0)
else
nil
end end
end end

2
spec/helpers/check_answers_helper_spec.rb

@ -11,7 +11,7 @@ RSpec.describe CheckAnswersHelper do
) )
end end
let(:case_log_with_met_radio_condition) do let(:case_log_with_met_radio_condition) do
FactoryBot.create(:case_log, armed_forces: "Yes - a regular") FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No")
end end
let(:subsection) { "income_and_benefits" } let(:subsection) { "income_and_benefits" }
let(:subsection_with_numeric_conditionals) { "household_characteristics" } let(:subsection_with_numeric_conditionals) { "household_characteristics" }

29
spec/models/case_log_spec.rb

@ -34,8 +34,9 @@ RSpec.describe Form, type: :model do
CaseLog.create!( CaseLog.create!(
tenant_economic_status: "Full-time - 30 hours or more", tenant_economic_status: "Full-time - 30 hours or more",
net_income: 5000, net_income: 5000,
net_income_frequency: "Weekly" net_income_frequency: "Weekly",
) }.to raise_error(ActiveRecord::RecordInvalid) )
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates net income minimum" do it "validates net income minimum" do
@ -43,12 +44,13 @@ RSpec.describe Form, type: :model do
CaseLog.create!( CaseLog.create!(
tenant_economic_status: "Full-time - 30 hours or more", tenant_economic_status: "Full-time - 30 hours or more",
net_income: 1, net_income: 1,
net_income_frequency: "Weekly" net_income_frequency: "Weekly",
) }.to raise_error(ActiveRecord::RecordInvalid) )
}.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
describe "reasonable preference validation" do context "reasonable preference validation" do
it "if given reasonable preference is yes a reason must be selected" do it "if given reasonable preference is yes a reason must be selected" do
expect { expect {
CaseLog.create!(reasonable_preference: "Yes", CaseLog.create!(reasonable_preference: "Yes",
@ -79,6 +81,7 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "other reason for leaving last settled home validation" do context "other reason for leaving last settled home validation" do
it "must be provided if main reason for leaving last settled home was given as other" do it "must be provided if main reason for leaving last settled home was given as other" do
expect { expect {
@ -94,6 +97,22 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "armed forces injured validation" do
it "must be anwered if tenant was a regular or reserve in armed forces" do
expect {
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_injured: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must be anwered if tenant was not a regular or reserve in armed forces" do
expect {
CaseLog.create!(armed_forces: "No",
armed_forces_injured: "Yes")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
end end
describe "status" do describe "status" do

Loading…
Cancel
Save