Browse Source

Only add errors if charges exist

pull/427/head
Kat 3 years ago
parent
commit
8547551309
  1. 2
      app/models/validations/financial_validations.rb
  2. 31
      spec/models/validations/financial_validations_spec.rb

2
app/models/validations/financial_validations.rb

@ -122,7 +122,7 @@ private
%i[scharge pscharge supcharg].each do |charge| %i[scharge pscharge supcharg].each do |charge|
maximum = CHARGE_MAXIMUMS.dig(charge, LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype]) maximum = CHARGE_MAXIMUMS.dig(charge, LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
if maximum.present? && !weekly_value_in_range(record, charge, 0, maximum) if maximum.present? && !weekly_value_in_range(record, charge, 0, maximum) && record[charge].present?
record.errors.add charge, I18n.t("validations.financial.rent.#{charge}.#{LANDLORD_VALUES[record.landlord]}.#{NEEDSTYPE_VALUES[record.needstype]}") record.errors.add charge, I18n.t("validations.financial.rent.#{charge}.#{LANDLORD_VALUES[record.landlord]}.#{NEEDSTYPE_VALUES[record.needstype]}")
end end
end end

31
spec/models/validations/financial_validations_spec.rb

@ -244,7 +244,7 @@ RSpec.describe Validations::FinancialValidations do
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 81 }, charge: { field: "supcharg", value: 81 },
}].each do |test_case| }].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value] record.period = test_case[:period][:value]
record[test_case[:charge][:field]] = test_case[:charge][:value] record[test_case[:charge][:field]] = test_case[:charge][:value]
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
@ -341,7 +341,7 @@ RSpec.describe Validations::FinancialValidations do
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 990 }, charge: { field: "supcharg", value: 990 },
}].each do |test_case| }].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value] record.period = test_case[:period][:value]
record[test_case[:charge][:field]] = test_case[:charge][:value] record[test_case[:charge][:field]] = test_case[:charge][:value]
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
@ -440,7 +440,7 @@ RSpec.describe Validations::FinancialValidations do
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 122 }, charge: { field: "supcharg", value: 122 },
}].each do |test_case| }].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value] record.period = test_case[:period][:value]
record[test_case[:charge][:field]] = test_case[:charge][:value] record[test_case[:charge][:field]] = test_case[:charge][:value]
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
@ -537,7 +537,7 @@ RSpec.describe Validations::FinancialValidations do
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "supcharg", value: 241 }, charge: { field: "supcharg", value: 241 },
}].each do |test_case| }].each do |test_case|
it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do
record.period = test_case[:period][:value] record.period = test_case[:period][:value]
record[test_case[:charge][:field]] = test_case[:charge][:value] record[test_case[:charge][:field]] = test_case[:charge][:value]
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
@ -546,6 +546,29 @@ RSpec.describe Validations::FinancialValidations do
end end
end end
context "when charges are not given" do
[{
period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: nil },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "pscharge", value: nil },
},
{
period: { label: "weekly", value: 1 },
charge: { field: "supcharg", value: nil },
}].each do |test_case|
it "does not error" do
record.period = test_case[:period][:value]
record[test_case[:charge][:field]] = test_case[:charge][:value]
financial_validator.validate_rent_amount(record)
expect(record.errors[test_case[:charge][:field]])
.to be_empty
end
end
end
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 120.88 }, charge: { field: "scharge", value: 120.88 },

Loading…
Cancel
Save