Browse Source

CLDC-3723: Add validation on location units question (#2798)

* Update location units question validation

* Update value

* Change nil value in incomplete locations to another attribute

* Update validation

* Update test

* Update test with new additional error message

* Updates

* Update test
pull/2843/head
Manny Dinssa 3 weeks ago committed by GitHub
parent
commit
ad4a7dcfc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      app/models/location.rb
  2. 4
      config/locales/en.yml
  3. 30
      spec/models/location_spec.rb
  4. 2
      spec/models/validations/setup_validations_spec.rb

3
app/models/location.rb

@ -2,7 +2,8 @@ class Location < ApplicationRecord
validates :postcode, on: :postcode, presence: { message: I18n.t("validations.location.postcode_blank") } validates :postcode, on: :postcode, presence: { message: I18n.t("validations.location.postcode_blank") }
validate :validate_postcode, on: :postcode, if: proc { |model| model.postcode.presence } validate :validate_postcode, on: :postcode, if: proc { |model| model.postcode.presence }
validates :location_admin_district, on: :location_admin_district, presence: { message: I18n.t("validations.location_admin_district") } validates :location_admin_district, on: :location_admin_district, presence: { message: I18n.t("validations.location_admin_district") }
validates :units, on: :units, presence: { message: I18n.t("validations.location.units") } validates :units, on: :units, presence: { message: I18n.t("validations.location.units.must_be_number") }
validates :units, on: :units, numericality: { greater_than_or_equal_to: 1, message: I18n.t("validations.location.units.must_be_one_or_more") }
validates :type_of_unit, on: :type_of_unit, presence: { message: I18n.t("validations.location.type_of_unit") } validates :type_of_unit, on: :type_of_unit, presence: { message: I18n.t("validations.location.type_of_unit") }
validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") } validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") }
validates :startdate, on: :startdate, presence: { message: I18n.t("validations.location.startdate_invalid") } validates :startdate, on: :startdate, presence: { message: I18n.t("validations.location.startdate_invalid") }

4
config/locales/en.yml

@ -361,7 +361,9 @@ en:
location: location:
postcode_blank: "Enter a postcode." postcode_blank: "Enter a postcode."
units: "The units at this location must be a number." units:
must_be_number: "The units at this location must be a number."
must_be_one_or_more: "Number of units must be at least 1."
type_of_unit: "Select the most common type of unit at this location." type_of_unit: "Select the most common type of unit at this location."
mobility_standards: "Select the mobility standard for the majority of the units at this location." mobility_standards: "Select the mobility standard for the majority of the units at this location."
startdate_invalid: "Enter a valid day, month and year when the first property became available at this location." startdate_invalid: "Enter a valid day, month and year when the first property became available at this location."

30
spec/models/location_spec.rb

@ -740,13 +740,41 @@ RSpec.describe Location, type: :model do
describe "#units" do describe "#units" do
let(:location) { FactoryBot.build(:location) } let(:location) { FactoryBot.build(:location) }
it "does add an error when the number of units is invalid" do context "when the number of units is invalid" do
it "adds an error when units is nil" do
location.units = nil location.units = nil
location.valid?(:units) location.valid?(:units)
expect(location.errors.count).to eq(2)
end
it "adds an error when units is 0" do
location.units = 0
location.valid?(:units)
expect(location.errors.count).to eq(1) expect(location.errors.count).to eq(1)
end end
end end
context "when the number of units is valid" do
it "does not add an error when units is 1" do
location.units = 1
location.valid?(:units)
expect(location.errors.count).to eq(0)
end
it "does not add an error when units is 10" do
location.units = 10
location.valid?(:units)
expect(location.errors.count).to eq(0)
end
it "does not add an error when units is 200" do
location.units = 200
location.valid?(:units)
expect(location.errors.count).to eq(0)
end
end
end
describe "#type_of_unit" do describe "#type_of_unit" do
let(:location) { FactoryBot.build(:location) } let(:location) { FactoryBot.build(:location) }

2
spec/models/validations/setup_validations_spec.rb

@ -703,7 +703,7 @@ RSpec.describe Validations::SetupValidations do
.to include("This location is incomplete. Select another location or update this one.") .to include("This location is incomplete. Select another location or update this one.")
end end
it "produces no error when location is completes" do it "produces no error when location is complete" do
location.update!(units: 1) location.update!(units: 1)
location.reload location.reload
record.location = location record.location = location

Loading…
Cancel
Save