Browse Source

Add supcharg validations and refactor

pull/388/head
Kat 3 years ago
parent
commit
24a82fcccd
  1. 82
      app/models/validations/financial_validations.rb
  2. 96
      spec/models/validations/financial_validations_spec.rb

82
app/models/validations/financial_validations.rb

@ -59,56 +59,35 @@ module Validations::FinancialValidations
end
end
SCHARGE_RANGES = {
this_landlord: {
general_needs: {
min: 0,
max: 55,
error: I18n.t("validations.financial.rent.scharge.this_landlord.general_needs"),
CHARGE_MAXIMUMS = {
scharge: {
this_landlord: {
general_needs: 55,
supported_housing: 280,
},
supported_housing: {
min: 0,
max: 280,
error: I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing"),
other_landlord: {
general_needs: 45,
supported_housing: 165,
},
},
other_landlord: {
general_needs: {
min: 0,
max: 45,
error: I18n.t("validations.financial.rent.scharge.other_landlord.general_needs"),
pscharge: {
this_landlord: {
general_needs: 30,
supported_housing: 200,
},
supported_housing: {
min: 0,
max: 165,
error: I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing"),
other_landlord: {
general_needs: 35,
supported_housing: 75,
},
},
}.freeze
PSCHARGE_RANGES = {
this_landlord: {
general_needs: {
min: 0,
max: 30,
error: I18n.t("validations.financial.rent.pscharge.this_landlord.general_needs"),
},
supported_housing: {
min: 0,
max: 200,
error: I18n.t("validations.financial.rent.pscharge.this_landlord.supported_housing"),
},
},
other_landlord: {
general_needs: {
min: 0,
max: 35,
error: I18n.t("validations.financial.rent.pscharge.other_landlord.general_needs"),
supcharg: {
this_landlord: {
general_needs: 40,
supported_housing: 465,
},
supported_housing: {
min: 0,
max: 75,
error: I18n.t("validations.financial.rent.pscharge.other_landlord.supported_housing"),
other_landlord: {
general_needs: 60,
supported_housing: 120,
},
},
}.freeze
@ -128,19 +107,16 @@ module Validations::FinancialValidations
private
def validate_charges(record)
scharge_range = SCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
pscharge_range = PSCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
if scharge_range.present? && !weekly_value_in_range(record, "scharge", scharge_range[:min], scharge_range[:max])
record.errors.add :scharge, scharge_range[:error]
end
%i[scharge pscharge supcharg].each do |charge|
maximum = CHARGE_MAXIMUMS.dig(charge, LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
if pscharge_range.present? && !weekly_value_in_range(record, "pscharge", pscharge_range[:min], pscharge_range[:max])
record.errors.add :pscharge, pscharge_range[:error]
if maximum.present? && !weekly_value_in_range(record, charge, maximum)
record.errors.add charge, I18n.t("validations.financial.rent.#{charge}.#{LANDLORD_VALUES[record.landlord]}.#{NEEDSTYPE_VALUES[record.needstype]}")
end
end
end
def weekly_value_in_range(record, field, min, max)
record[field].present? && record.weekly_value(record[field]).present? && record.weekly_value(record[field]).between?(min, max)
def weekly_value_in_range(record, field, max)
record[field].present? && record.weekly_value(record[field]).present? && record.weekly_value(record[field]).between?(0, max)
end
end

96
spec/models/validations/financial_validations_spec.rb

@ -231,6 +231,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 61 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 41 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 200 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 81 },
}].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -264,6 +276,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 59 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 39 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 120 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 79 },
}].each do |test_case|
it "does allow charges inside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -304,6 +328,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 400.80 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 466 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 3100 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 990 },
}].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -337,6 +373,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 400 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 464 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 2000 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 880 },
}].each do |test_case|
it "does allow charges inside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -379,6 +427,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 71 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 61 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 300 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 122 },
}].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -412,6 +472,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 69 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 59.99 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 240 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 119 },
}].each do |test_case|
it "does allow charges inside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -452,6 +524,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 151 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 121 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 620 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 241 },
}].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]
@ -485,6 +569,18 @@ RSpec.describe Validations::FinancialValidations do
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "pscharge", value: 149 },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: 119 },
},
{
period: { label: "monthly", value: 4 },
charge: { field: "supcharg", value: 480 },
},
{
period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 239 },
}].each do |test_case|
it "does allow charges inside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value]

Loading…
Cancel
Save