Browse Source

feat: validate on model (except startdate)

pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
fc47499e7e
  1. 29
      app/controllers/locations_controller.rb
  2. 46
      app/models/location.rb
  3. 5
      config/locales/en.yml

29
app/controllers/locations_controller.rb

@ -25,8 +25,7 @@ class LocationsController < ApplicationController
@location.postcode = params[:location][:postcode]
@location.location_admin_district = nil
@location.location_code = nil
@location.validate_postcode
if @location.errors.blank?
if @location.valid?(:postcode)
@location.save!
if @location.location_code.blank? || @location.location_admin_district.blank?
redirect_to scheme_location_local_authority_path(@scheme, @location, referrer: params[:referrer])
@ -43,9 +42,9 @@ class LocationsController < ApplicationController
def local_authority
if params[:location].present?
if params[:location][:location_admin_district] != "Select an option"
@location.location_admin_district = params[:location][:location_admin_district]
@location.location_code = Location.local_authorities.key(params[:location][:location_admin_district])
if @location.valid?(:local_authority)
@location.save!
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location)
@ -53,8 +52,6 @@ class LocationsController < ApplicationController
redirect_to scheme_location_name_path(@scheme, @location)
end
else
error_message = I18n.t("validations.location_admin_district")
@location.errors.add :local_authority, error_message
render :local_authority, status: :unprocessable_entity
end
end
@ -62,8 +59,8 @@ class LocationsController < ApplicationController
def name
if params[:location].present?
if params[:location][:name].present?
@location.name = params[:location][:name]
if @location.valid?(:name)
@location.save!
case params[:referrer]
when "check_answers"
@ -74,8 +71,6 @@ class LocationsController < ApplicationController
redirect_to scheme_location_units_path(@scheme, @location)
end
else
error_message = I18n.t("validations.location.name")
@location.errors.add :name, error_message
render :name, status: :unprocessable_entity
end
end
@ -83,8 +78,8 @@ class LocationsController < ApplicationController
def units
if params[:location].present?
if params[:location][:units].present?
@location.units = params[:location][:units]
if @location.valid?(:units)
@location.save!
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location)
@ -92,8 +87,6 @@ class LocationsController < ApplicationController
redirect_to scheme_location_type_of_unit_path(@scheme, @location)
end
else
error_message = I18n.t("validations.location.units")
@location.errors.add :units, error_message
render :units, status: :unprocessable_entity
end
end
@ -101,8 +94,8 @@ class LocationsController < ApplicationController
def type_of_unit
if params[:location].present?
if params[:location][:type_of_unit].present?
@location.type_of_unit = params[:location][:type_of_unit]
if @location.valid?(:type_of_unit)
@location.save!
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location)
@ -110,8 +103,6 @@ class LocationsController < ApplicationController
redirect_to scheme_location_mobility_standards_path(@scheme, @location)
end
else
error_message = I18n.t("validations.location.type_of_unit")
@location.errors.add :type_of_unit, error_message
render :type_of_unit, status: :unprocessable_entity
end
end
@ -119,8 +110,8 @@ class LocationsController < ApplicationController
def mobility_standards
if params[:location].present?
if params[:location][:mobility_type].present?
@location.mobility_type = params[:location][:mobility_type]
if @location.valid?(:mobility_type)
@location.save!
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location)
@ -128,8 +119,6 @@ class LocationsController < ApplicationController
redirect_to scheme_location_availability_path(@scheme, @location)
end
else
error_message = I18n.t("validations.location.mobility_standards")
@location.errors.add :mobility_type, error_message
render :mobility_standards, status: :unprocessable_entity
end
end
@ -146,12 +135,12 @@ class LocationsController < ApplicationController
@location.save!
redirect_to scheme_location_check_answers_path(@scheme, @location)
else
error_message = I18n.t("validations.location.availability_invalid")
error_message = I18n.t("validations.location.startdate_invalid")
@location.errors.add :startdate, error_message
render :availability, status: :unprocessable_entity
end
else
error_message = I18n.t("validations.location.availability_blank")
error_message = I18n.t("validations.location.startdate_blank")
@location.errors.add :startdate, error_message
render :availability, status: :unprocessable_entity
end
@ -160,8 +149,6 @@ class LocationsController < ApplicationController
def check_answers
if params[:location].present?
@location.confirmed = true
@location.save!
flash[:notice] = "#{@location.postcode} #{@location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme"
redirect_to scheme_locations_path(@scheme, @location)
end

46
app/models/location.rb

@ -1,4 +1,10 @@
class Location < ApplicationRecord
validate :validate_postcode, on: :postcode
validate :validate_location_admin_district, on: :location_admin_district
validate :validate_name, on: :name
validate :validate_units, on: :units
validate :validate_type_of_unit, on: :type_of_unit
validate :validate_mobility_type, on: :mobility_type
belongs_to :scheme
has_many :lettings_logs, class_name: "LettingsLog"
has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod"
@ -392,12 +398,50 @@ class Location < ApplicationRecord
end
def validate_postcode
if postcode.nil? || !postcode&.match(POSTCODE_REGEXP)
if postcode.blank?
error_message = I18n.t("validations.location.postcode_blank")
errors.add :postcode, error_message
elsif !postcode&.match(POSTCODE_REGEXP)
error_message = I18n.t("validations.postcode")
errors.add :postcode, error_message
end
end
def validate_location_admin_district
if location_admin_district == "Select an option"
error_message = I18n.t("validations.location_admin_district")
errors.add :location_admin_district, error_message
end
end
def validate_name
if name.blank?
error_message = I18n.t("validations.location.name")
errors.add :name, error_message
end
end
def validate_units
if units.blank?
error_message = I18n.t("validations.location.units")
errors.add :units, error_message
end
end
def validate_type_of_unit
if type_of_unit.blank?
error_message = I18n.t("validations.location.type_of_unit")
errors.add :type_of_unit, error_message
end
end
def validate_mobility_type
if mobility_type.blank?
error_message = I18n.t("validations.location.mobility_standards")
errors.add :mobility_type, error_message
end
end
private
PIO = PostcodeService.new

5
config/locales/en.yml

@ -325,12 +325,13 @@ en:
location:
postcode_blank: "Enter a postcode"
name: "Enter the name of the location"
units: "The units at this location must be a number"
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"
availability_blank: "Enter the day, month and year when the first property became available at this location"
availability_invalid: "Enter a valid day, month and year when the first property became available at this location"
startdate_blank: "Enter the 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"
toggle_date:
not_selected: "Select one of the options"
invalid: "Enter a valid day, month and year"

Loading…
Cancel
Save