From df9dcb27c09a014ee692fdf2eda1864d01b57b5d Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 5 Dec 2022 10:56:51 +0000 Subject: [PATCH] refactor: simplify validation and location_edit_path method --- app/controllers/locations_controller.rb | 2 +- app/helpers/locations_helper.rb | 14 ++------- app/models/location.rb | 36 +++------------------- spec/requests/locations_controller_spec.rb | 4 +-- 4 files changed, 9 insertions(+), 47 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index b557bed8f..9a066ce81 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -5,7 +5,7 @@ class LocationsController < ApplicationController before_action :find_location, except: %i[create index] before_action :find_scheme before_action :authenticate_action! - before_action :scheme_and_location_present, only: %i[postcode local_authority name units type_of_unit mobility_standards availability check_answers] + before_action :scheme_and_location_present, only: %i[postcode update_postcode local_authority update_local_authority name update_name units update_units type_of_unit update_type_of_unit mobility_standards update_mobility_standards availability update_availability check_answers update_check_answers] include Modules::SearchFilter diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index b775e8ec0..7e9bf47cf 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -55,20 +55,10 @@ module LocationsHelper def location_edit_path(location, page) case page - when "postcode" - scheme_location_postcode_path(location.scheme, location, referrer: "check_answers", route: params[:route]) - when "name" - scheme_location_name_path(location.scheme, location, referrer: "check_answers", route: params[:route]) when "location_admin_district" scheme_location_local_authority_path(location.scheme, location, referrer: "check_local_authority", route: params[:route]) - when "units" - scheme_location_units_path(location.scheme, location, referrer: "check_answers", route: params[:route]) - when "type_of_unit" - scheme_location_type_of_unit_path(location.scheme, location, referrer: "check_answers", route: params[:route]) - when "mobility_standards" - scheme_location_mobility_standards_path(location.scheme, location, referrer: "check_answers", route: params[:route]) - when "availability" - scheme_location_availability_path(location.scheme, location, referrer: "check_answers", route: params[:route]) + else + send("scheme_location_#{page}_path", location.scheme, location, referrer: "check_answers", route: params[:route]) end end diff --git a/app/models/location.rb b/app/models/location.rb index 10c1e275f..8164617bb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,10 +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 + validates :name, on: :name, presence: { message: I18n.t("validations.location.name") } + validates :units, on: :units, presence: { message: I18n.t("validations.location.units") } + 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") } validate :validate_startdate, on: :startdate belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" @@ -424,34 +424,6 @@ class Location < ApplicationRecord 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 - def validate_startdate unless startdate.between?(Time.zone.local(1900, 1, 1), Time.zone.local(2200, 1, 1)) error_message = I18n.t("validations.location.startdate_out_of_range") diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 56b766b5a..fef738427 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -13,7 +13,7 @@ RSpec.describe LocationsController, type: :request do describe "#create" do context "when not signed in" do it "redirects to the sign in page" do - post "/schemes/1/locations/create" + get "/schemes/1/locations/create" expect(response).to redirect_to("/account/sign-in") end end @@ -23,7 +23,7 @@ RSpec.describe LocationsController, type: :request do before do sign_in user - post "/schemes/1/locations/create" + get "/schemes/1/locations/create" end it "returns 401 unauthorized" do