Browse Source

CLDC-1461 add validation ensure postcodes match if discounted ownership purchase (#1200)

* new property validation file for sales logs with postcode validation regarding discounted ownership, tests also

* linting problem
CLDC-869-add-validations-for-initial-equity-for-shared-ownership
Arthur Campbell 2 years ago committed by GitHub
parent
commit
d2afa46946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Gemfile.lock
  2. 6
      app/models/sales_log.rb
  3. 10
      app/models/validations/sales/property_validations.rb
  4. 2
      config/locales/en.yml
  5. 51
      spec/models/validations/sales/property_validations_spec.rb

1
Gemfile.lock

@ -426,6 +426,7 @@ GEM
PLATFORMS PLATFORMS
arm64-darwin-21 arm64-darwin-21
arm64-darwin-22
x86_64-darwin-19 x86_64-darwin-19
x86_64-darwin-20 x86_64-darwin-20
x86_64-darwin-21 x86_64-darwin-21

6
app/models/sales_log.rb

@ -1,5 +1,7 @@
class SalesLogValidator < ActiveModel::Validator class SalesLogValidator < ActiveModel::Validator
include Validations::Sales::HouseholdValidations include Validations::Sales::HouseholdValidations
include Validations::Sales::PropertyValidations
include Validations::SharedValidations
include Validations::Sales::FinancialValidations include Validations::Sales::FinancialValidations
include Validations::Sales::SaleInformationValidations include Validations::Sales::SaleInformationValidations
include Validations::SharedValidations include Validations::SharedValidations
@ -167,6 +169,10 @@ class SalesLog < Log
ownershipsch == 3 ownershipsch == 3
end end
def discounted_ownership_sale?
ownershipsch == 2
end
def mortgage_not_used? def mortgage_not_used?
mortgageused == 2 mortgageused == 2
end end

10
app/models/validations/sales/property_validations.rb

@ -0,0 +1,10 @@
module Validations::Sales::PropertyValidations
def validate_postcodes_match_if_discounted_ownership(record)
return unless record.ppostcode_full.present? && record.postcode_full.present?
if record.discounted_ownership_sale? && record.ppostcode_full != record.postcode_full
record.errors.add :postcode_full, I18n.t("validations.property.postcode.must_match_previous")
record.errors.add :ppostcode_full, I18n.t("validations.property.postcode.must_match_previous")
end
end
end

2
config/locales/en.yml

@ -195,6 +195,8 @@ en:
beds: beds:
non_positive: "Number of bedrooms has to be greater than 0" non_positive: "Number of bedrooms has to be greater than 0"
over_max: "Number of bedrooms cannot be more than 12" over_max: "Number of bedrooms cannot be more than 12"
postcode:
must_match_previous: "Buyer's last accommodation and discounted ownership postcodes must match"
financial: financial:
tshortfall: tshortfall:

51
spec/models/validations/sales/property_validations_spec.rb

@ -0,0 +1,51 @@
require "rails_helper"
RSpec.describe Validations::Sales::PropertyValidations do
subject(:property_validator) { property_validator_class.new }
let(:property_validator_class) { Class.new { include Validations::Sales::PropertyValidations } }
describe "#validate_postcodes_match_if_discounted_ownership" do
context "when ownership scheme is not discounted ownership" do
let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1) }
it "when postcodes match no error is added" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
end
end
context "when ownership scheme is discounted ownership" do
let(:record) { FactoryBot.build(:sales_log, ownershipsch: 2) }
it "when ppostcode_full is not present no error is added" do
record.postcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
end
it "when postcode_full is not present no error is added" do
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
end
it "when postcodes match no error is added" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
end
it "when postcodes do not match an error is added" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 0AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to include(match I18n.t("validations.property.postcode.must_match_previous"))
end
end
end
end
Loading…
Cancel
Save