Browse Source

Add appropriate specs

CLDC-858-update-sales-property-information
Mo Seedat 2 years ago
parent
commit
8f51dd5524
  1. 32
      app/models/sales_log.rb
  2. 7
      app/models/validations/sales/property_information_validations.rb
  3. 4
      config/locales/en.yml
  4. 54
      spec/models/sales_log_spec.rb

32
app/models/sales_log.rb

@ -1,32 +1,5 @@
class SalesLogValidator < ActiveModel::Validator
# 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_") }
@ -41,7 +14,10 @@ class SalesLog < Log
has_paper_trail
#validates :beds, numericality: { only_integer: true }, presence: true, comparison: { greater_than: 0, less_than: 10 }
## Regular validations
validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 }, if: -> { beds.present? }
## Custom validations
validates_with SalesLogValidator
before_validation :set_derived_fields!

7
app/models/validations/sales/property_information_validations.rb

@ -1,8 +1,11 @@
module Validations::Sales::PropertyInformationValidations
# CLDC-858
# Error must be defined for both :proptype and :beds
# to ensure the appropriate error is shown when selecting
# property type and number of rooms
def validate_bedsit_has_one_room(record)
if record.bedsit? && record.beds > 1
record.errors.add(:beds, :non_bedsit_max)
record.errors.add(:proptype, :bedsit_max)
record.errors.add(:beds, :bedsit_max)
end
end
end

4
config/locales/en.yml

@ -98,7 +98,9 @@ en:
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"
bedsit_max: "A bedsit can not have more than 1 bedroom"
proptype:
bedsit_max: "A bedsit can not have more than 1 bedroom"
validations:
organisation:

54
spec/models/sales_log_spec.rb

@ -94,28 +94,54 @@ RSpec.describe SalesLog, type: :model do
# 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)
describe "#beds" do
before do
expect(sales_log).to be_valid
end
context "when property is a bedsit" do
let(:sales_log) {FactoryBot.build(:sales_log, :completed, :bedsit) }
# Set error for :beds and :proptype to ensure message is shown in
# both bedroom number selection and property type selection
it "must only have 1 bedroom" do
sales_log.beds = 3
expect(sales_log).to be_valid
expect(sales_log).to_not be_valid
expect(sales_log.errors[:beds]).to eq ["A bedsit can not have more than 1 bedroom"]
expect(sales_log.errors[:proptype]).to eq ["A bedsit can not have more than 1 bedroom"]
end
end
context "when invalid" do
it "is not an integer" do
sales_log = FactoryBot.build(:sales_log, beds: "Four")
context "when property is not a bedsit" do
let(:sales_log) { FactoryBot.build(:sales_log, :completed, beds: 4, proptype: 3) }
expect(sales_log).to_not be_valid
expect(sales_log.errors.message[:beds]).to eq ["is invalid"]
it "must have 1 to 9 bedrooms", aggregate_failures: true do
[0, 10].each do |num_beds|
sales_log.beds = num_beds
expect(sales_log).to_not be_valid
expect(sales_log.errors[:beds]).to eq ["Number of bedrooms must be between 1 and 9"]
end
end
end
it "is a negative number" do
sales_log = FactoryBot.build(:sales_log, beds: -4)
context "when given invalid data" do
let(:sales_log) { FactoryBot.build(:sales_log, :completed) }
expect(sales_log).to_not be_valid
expect(sales_log.errors.message[:beds]).to eq ["is invalid"]
it "fails with appropriate error message", aggregate_failures: true do
invalid_beds_values = {
"Four" => ["Number of bedrooms must be between 1 and 9", "Number of bedrooms must be between 1 and 9"],
-2 => ["Number of bedrooms must be between 1 and 9"],
2.5 => ["Number of bedrooms must be a whole number between 1 and 9"]
}
invalid_beds_values.each do |beds_value, expected_error|
sales_log.beds = beds_value
expect(sales_log).to_not be_valid
expect(sales_log.errors[:beds]).to eq expected_error
end
end
end
end

Loading…
Cancel
Save