Browse Source

CLDC-2016 mortgage validation (#1445)

* add method to sales log and use in depends on to increase readability

* increase minimum allowed value for mortgage

* write tests and add validation error to config for new validation

* write validation

* adjust validation so it doesn't trigger when mortgage value is derived as 0

* correct linting problem and update tests to account for change in validation
pull/1459/head
Arthur Campbell 2 years ago committed by GitHub
parent
commit
97bbf1924d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/form/sales/pages/mortgage_amount.rb
  2. 2
      app/models/form/sales/questions/mortgage_amount.rb
  3. 4
      app/models/sales_log.rb
  4. 4
      app/models/validations/sales/financial_validations.rb
  5. 1
      config/locales/en.yml
  6. 4
      spec/models/form/sales/pages/mortgage_amount_spec.rb
  7. 6
      spec/models/form/sales/questions/mortgage_amount_spec.rb
  8. 18
      spec/models/validations/sales/financial_validations_spec.rb

4
app/models/form/sales/pages/mortgage_amount.rb

@ -3,9 +3,7 @@ class Form::Sales::Pages::MortgageAmount < ::Form::Page
super(id, hsh, subsection) super(id, hsh, subsection)
@ownershipsch = ownershipsch @ownershipsch = ownershipsch
@header = "Mortgage Amount" @header = "Mortgage Amount"
@depends_on = [{ @depends_on = [{ "mortgage_used?" => true }]
"mortgageused" => 1,
}]
end end
def questions def questions

2
app/models/form/sales/questions/mortgage_amount.rb

@ -5,7 +5,7 @@ class Form::Sales::Questions::MortgageAmount < ::Form::Question
@check_answer_label = "Mortgage amount" @check_answer_label = "Mortgage amount"
@header = "What is the mortgage amount?" @header = "What is the mortgage amount?"
@type = "numeric" @type = "numeric"
@min = 0 @min = 1
@width = 5 @width = 5
@prefix = "£" @prefix = "£"
@hint_text = "Enter the amount of mortgage agreed with the mortgage lender. Exclude any deposits or cash payments. Numeric in pounds. Rounded to the nearest pound." @hint_text = "Enter the amount of mortgage agreed with the mortgage lender. Exclude any deposits or cash payments. Numeric in pounds. Rounded to the nearest pound."

4
app/models/sales_log.rb

@ -243,6 +243,10 @@ class SalesLog < Log
ownershipsch == 2 ownershipsch == 2
end end
def mortgage_used?
mortgageused == 1
end
def mortgage_not_used? def mortgage_not_used?
mortgageused == 2 mortgageused == 2
end end

4
app/models/validations/sales/financial_validations.rb

@ -36,6 +36,10 @@ module Validations::Sales::FinancialValidations
end end
end end
def validate_mortgage(record)
record.errors.add :mortgage, I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero?
end
def validate_cash_discount(record) def validate_cash_discount(record)
return unless record.cashdis return unless record.cashdis

1
config/locales/en.yml

@ -316,6 +316,7 @@ en:
equity: equity:
under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%" under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%"
over_max: "The maximum initial equity stake is %{max_equity}%" over_max: "The maximum initial equity stake is %{max_equity}%"
mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property"
household: household:
reasonpref: reasonpref:

4
spec/models/form/sales/pages/mortgage_amount_spec.rb

@ -28,8 +28,6 @@ RSpec.describe Form::Sales::Pages::MortgageAmount, type: :model do
end end
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to eq([{ expect(page.depends_on).to eq([{ "mortgage_used?" => true }])
"mortgageused" => 1,
}])
end end
end end

6
spec/models/form/sales/questions/mortgage_amount_spec.rb

@ -8,7 +8,7 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do
let(:page) { instance_double(Form::Page) } let(:page) { instance_double(Form::Page) }
it "has correct page" do it "has correct page" do
expect(question.page).to eq(page) expect(question.page).to be(page)
end end
it "has the correct id" do it "has the correct id" do
@ -36,7 +36,7 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do
end end
it "has correct width" do it "has correct width" do
expect(question.width).to eq(5) expect(question.width).to be(5)
end end
it "has correct prefix" do it "has correct prefix" do
@ -44,6 +44,6 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do
end end
it "has correct min" do it "has correct min" do
expect(question.min).to eq(0) expect(question.min).to be(1)
end end
end end

18
spec/models/validations/sales/financial_validations_spec.rb

@ -113,6 +113,24 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
describe "#validate_mortgage" do
let(:record) { FactoryBot.create(:sales_log) }
it "adds an error is the mortgage is zero" do
record.mortgageused = 1
record.mortgage = 0
financial_validator.validate_mortgage(record)
expect(record.errors[:mortgage]).to include I18n.t("validations.financial.mortgage")
end
it "does not add an error is the mortgage is positive" do
record.mortgageused = 1
record.mortgage = 234
financial_validator.validate_mortgage(record)
expect(record.errors).to be_empty
end
end
describe "#validate_cash_discount" do describe "#validate_cash_discount" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.create(:sales_log) }

Loading…
Cancel
Save