Browse Source

Validate that earnings and incfreq must be provided together

pull/273/head
baarkerlounger 3 years ago committed by MadeTech Dushan
parent
commit
5189ebda5c
  1. 18
      app/models/validations/financial_validations.rb
  2. 2
      config/locales/en.yml
  3. 24
      spec/models/validations/financial_validations.rb
  4. 1
      spec/models/validations/property_validations_spec.rb

18
app/models/validations/financial_validations.rb

@ -21,14 +21,22 @@ module Validations::FinancialValidations
end
def validate_net_income(record)
return unless record.ecstat1 && record.weekly_net_income
if record.ecstat1 && record.weekly_net_income
if record.weekly_net_income > record.applicable_income_range.hard_max
record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max)
end
if record.weekly_net_income < record.applicable_income_range.hard_min
record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min)
end
end
if record.weekly_net_income > record.applicable_income_range.hard_max
record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max)
if record.earnings.present? && record.incfreq.blank?
record.errors.add :incfreq, I18n.t("validations.financial.earnings.freq_missing")
end
if record.weekly_net_income < record.applicable_income_range.hard_min
record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min)
if record.incfreq.present? && record.earnings.blank?
record.errors.add :earnings, I18n.t("validations.financial.earnings.earnings_missing")
end
end

2
config/locales/en.yml

@ -68,6 +68,8 @@ en:
earnings:
under_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation"
over_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation"
freq_missing: "How often the household receives this amount must be provided"
earnings_missing: "How much income the household has in total must be provided"
household:
reasonpref:

24
spec/models/validations/financial_validations.rb

@ -0,0 +1,24 @@
require "rails_helper"
RSpec.describe Validations::FinancialValidations do
subject(:financial_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::FinancialValidations } }
let(:record) { FactoryBot.create(:case_log) }
describe "earnings and income frequency" do
it "when earnings are provided it validates that income frequency must be provided" do
record.earnings = 500
record.incfreq = nil
financial_validator.validate_net_income(record)
expect(record.errors["incfreq"]).to include(match I18n.t("validations.financial.earnings.freq_missing"))
end
it "when income frequency is provided it validates that earnings must be provided" do
record.earnings = nil
record.incfreq = "Weekly"
financial_validator.validate_net_income(record)
expect(record.errors["earnings"]).to include(match I18n.t("validations.financial.earnings.earnings_missing"))
end
end
end

1
spec/models/validations/property_validations_spec.rb

@ -1,5 +1,4 @@
require "rails_helper"
require_relative "../../request_helper"
RSpec.describe Validations::PropertyValidations do
subject(:property_validator) { property_validator_class.new }

Loading…
Cancel
Save