From c7b4e907be2498e199c7d0827bd151c7609b63c9 Mon Sep 17 00:00:00 2001 From: Mo Seedat Date: Tue, 11 Oct 2022 11:45:28 +0100 Subject: [PATCH] Initial experimental version --- app/models/lettings_log.rb | 1 + app/models/sales_log.rb | 40 ++++++++++++++++++- .../sales/property_information_validations.rb | 8 ++++ config/locales/en.yml | 10 +++++ db/schema.rb | 8 ++-- spec/factories/sales_log.rb | 7 ++++ spec/models/sales_log_spec.rb | 30 ++++++++++++++ .../property_information_validations_spec.rb | 13 ++++++ 8 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 app/models/validations/sales/property_information_validations.rb create mode 100644 spec/models/validations/sales/property_information_validations_spec.rb diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 9e38138eb..371191ee6 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -8,6 +8,7 @@ class LettingsLogValidator < ActiveModel::Validator include Validations::TenancyValidations include Validations::DateValidations include Validations::LocalAuthorityValidations + def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } validation_methods.each { |meth| public_send(meth, record) } diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index e1458caa9..c96589644 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,5 +1,37 @@ class SalesLogValidator < ActiveModel::Validator - def validate(record); end + + # included do + # validates :beds, numericality: { only_integer: true }, presence: true, comparison: { greater_than: 0, less_than: 10 } + # end + def self.included(klass) + #klass.extend(ClassMethods) + puts "INCLUDING VALIDATIONS" + validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 } + + + end + + SalesLogValidator.class_eval do + p "class_eval - self is: " + self.to_s + def frontend + p "inside a method self is: " + self.to_s + end + validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 } + + end + + + + # Validations methods need to be called 'validate_' to run on model save + # or form page submission + include Validations::Sales::PropertyInformationValidations + #extend ActiveSupport::Concern + + + def validate(record) + validation_methods = public_methods.select { |method| method.starts_with?("validate_") } + validation_methods.each { |meth| public_send(meth, record) } + end end class SalesLog < Log @@ -9,7 +41,9 @@ class SalesLog < Log has_paper_trail + #validates :beds, numericality: { only_integer: true }, presence: true, comparison: { greater_than: 0, less_than: 10 } validates_with SalesLogValidator + before_validation :set_derived_fields! before_validation :reset_invalidated_dependent_fields! @@ -47,4 +81,8 @@ class SalesLog < Log def completed? status == "completed" end + + def bedsit? + proptype == 2 + end end diff --git a/app/models/validations/sales/property_information_validations.rb b/app/models/validations/sales/property_information_validations.rb new file mode 100644 index 000000000..cfed71d22 --- /dev/null +++ b/app/models/validations/sales/property_information_validations.rb @@ -0,0 +1,8 @@ +module Validations::Sales::PropertyInformationValidations + # CLDC-858 + def validate_bedsit_has_one_room(record) + if record.bedsit? && record.beds > 1 + record.errors.add(:beds, :non_bedsit_max) + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index ef58f0cae..d9ded75d4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -89,6 +89,16 @@ en: role: invalid: "Role must be data accessor, data provider or data coordinator" blank: "Select role" + sales_log: + attributes: + beds: + blank: "Enter the number of bedrooms" + invalid: "Number of bedrooms must be between 1 and 9" + not_a_number: "Number of bedrooms must be between 1 and 9" + not_an_integer: "Number of bedrooms must be a whole number between 1 and 9" + greater_than: "Number of bedrooms must be between 1 and 9" + less_than: "Number of bedrooms must be between 1 and 9" + non_bedsit_max: "A bedsit can not have more than 1 bedroom" validations: organisation: diff --git a/db/schema.rb b/db/schema.rb index f12988ab8..dbe09f0fb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -337,12 +337,10 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_07_093606) do t.integer "age1" t.integer "age1_known" t.string "sex1" - t.integer "national" - t.string "othernational" - t.integer "ethnic" - t.integer "ethnic_group" t.integer "buy1livein" t.integer "buylivein" + t.integer "ethnic" + t.integer "ethnic_group" t.integer "builtype" t.integer "proptype" t.integer "age2" @@ -352,6 +350,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_07_093606) do t.integer "noint" t.integer "buy2livein" t.integer "ecstat2" + t.integer "national" + t.string "othernational" t.integer "privacynotice" t.integer "ecstat1" t.integer "wheel" diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 322b3d6fa..f26e9b679 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -5,12 +5,14 @@ FactoryBot.define do managing_organisation { created_by.organisation } created_at { Time.utc(2022, 2, 8, 16, 52, 15) } updated_at { Time.utc(2022, 2, 8, 16, 52, 15) } + trait :in_progress do purchid { "PC123" } ownershipsch { 2 } type { 8 } saledate { Time.utc(2022, 2, 2, 10, 36, 49) } end + trait :completed do purchid { "PC123" } ownershipsch { 2 } @@ -42,5 +44,10 @@ FactoryBot.define do age3_known { 0 } age3 { 40 } end + + trait :bedsit do + proptype { 2 } + beds { 1 } + end end end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 3ee033971..a3fae265a 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -90,4 +90,34 @@ RSpec.describe SalesLog, type: :model do expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0) end end + + # Rails helper validations only. Custom validations belong in their respective + # validations spec + describe "validations" do + describe "beds" do + context "when valid" do + it "is between 1 and 9" do + sales_log = FactoryBot.build(:sales_log, beds: 4) + + expect(sales_log).to be_valid + end + end + + context "when invalid" do + it "is not an integer" do + sales_log = FactoryBot.build(:sales_log, beds: "Four") + + expect(sales_log).to_not be_valid + expect(sales_log.errors.message[:beds]).to eq ["is invalid"] + end + + it "is a negative number" do + sales_log = FactoryBot.build(:sales_log, beds: -4) + + expect(sales_log).to_not be_valid + expect(sales_log.errors.message[:beds]).to eq ["is invalid"] + end + end + end + end end diff --git a/spec/models/validations/sales/property_information_validations_spec.rb b/spec/models/validations/sales/property_information_validations_spec.rb new file mode 100644 index 000000000..ce48977a3 --- /dev/null +++ b/spec/models/validations/sales/property_information_validations_spec.rb @@ -0,0 +1,13 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::PropertyInformationValidations do + subject(:date_validator) { validator_class.new } + + describe "#validate_bedsit_has_one_room" do + context "when property is bedsit" do + end + + context "when property is not a bedsit" do + end + end +end