Browse Source

Initial experimental version

CLDC-858-update-sales-property-information
Mo Seedat 2 years ago
parent
commit
c7b4e907be
  1. 1
      app/models/lettings_log.rb
  2. 40
      app/models/sales_log.rb
  3. 8
      app/models/validations/sales/property_information_validations.rb
  4. 10
      config/locales/en.yml
  5. 8
      db/schema.rb
  6. 7
      spec/factories/sales_log.rb
  7. 30
      spec/models/sales_log_spec.rb
  8. 13
      spec/models/validations/sales/property_information_validations_spec.rb

1
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) }

40
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

8
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

10
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:

8
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"

7
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

30
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

13
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
Loading…
Cancel
Save