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)
@ownershipsch = ownershipsch
@header = "Mortgage Amount"
@depends_on = [{
"mortgageused" => 1,
}]
@depends_on = [{ "mortgage_used?" => true }]
end
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"
@header = "What is the mortgage amount?"
@type = "numeric"
@min = 0
@min = 1
@width = 5
@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."

4
app/models/sales_log.rb

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

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

@ -36,6 +36,10 @@ module Validations::Sales::FinancialValidations
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)
return unless record.cashdis

1
config/locales/en.yml

@ -316,6 +316,7 @@ en:
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}%"
mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property"
household:
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
it "has correct depends_on" do
expect(page.depends_on).to eq([{
"mortgageused" => 1,
}])
expect(page.depends_on).to eq([{ "mortgage_used?" => true }])
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) }
it "has correct page" do
expect(question.page).to eq(page)
expect(question.page).to be(page)
end
it "has the correct id" do
@ -36,7 +36,7 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do
end
it "has correct width" do
expect(question.width).to eq(5)
expect(question.width).to be(5)
end
it "has correct prefix" do
@ -44,6 +44,6 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do
end
it "has correct min" do
expect(question.min).to eq(0)
expect(question.min).to be(1)
end
end

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

@ -113,6 +113,24 @@ RSpec.describe Validations::Sales::FinancialValidations do
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
let(:record) { FactoryBot.create(:sales_log) }

Loading…
Cancel
Save