Browse Source

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

pull/1200/head
Arthur Campbell 3 years ago
parent
commit
55b5f5ef2d
  1. 1
      Gemfile.lock
  2. 5
      app/models/sales_log.rb
  3. 10
      app/models/validations/sales/property_validations.rb
  4. 2
      config/locales/en.yml
  5. 50
      spec/models/validations/sales/property_validations_spec.rb

1
Gemfile.lock

@ -429,6 +429,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

5
app/models/sales_log.rb

@ -1,5 +1,6 @@
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::SharedValidations
include Validations::Sales::FinancialValidations include Validations::Sales::FinancialValidations
include Validations::LocalAuthorityValidations include Validations::LocalAuthorityValidations
@ -166,6 +167,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:

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

@ -0,0 +1,50 @@
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