Browse Source

refactor: simplify validation and location_edit_path method

pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
df9dcb27c0
  1. 2
      app/controllers/locations_controller.rb
  2. 14
      app/helpers/locations_helper.rb
  3. 36
      app/models/location.rb
  4. 4
      spec/requests/locations_controller_spec.rb

2
app/controllers/locations_controller.rb

@ -5,7 +5,7 @@ class LocationsController < ApplicationController
before_action :find_location, except: %i[create index] before_action :find_location, except: %i[create index]
before_action :find_scheme before_action :find_scheme
before_action :authenticate_action! 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 include Modules::SearchFilter

14
app/helpers/locations_helper.rb

@ -55,20 +55,10 @@ module LocationsHelper
def location_edit_path(location, page) def location_edit_path(location, page)
case 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" when "location_admin_district"
scheme_location_local_authority_path(location.scheme, location, referrer: "check_local_authority", route: params[:route]) scheme_location_local_authority_path(location.scheme, location, referrer: "check_local_authority", route: params[:route])
when "units" else
scheme_location_units_path(location.scheme, location, referrer: "check_answers", route: params[:route]) send("scheme_location_#{page}_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])
end end
end end

36
app/models/location.rb

@ -1,10 +1,10 @@
class Location < ApplicationRecord class Location < ApplicationRecord
validate :validate_postcode, on: :postcode validate :validate_postcode, on: :postcode
validate :validate_location_admin_district, on: :location_admin_district validate :validate_location_admin_district, on: :location_admin_district
validate :validate_name, on: :name validates :name, on: :name, presence: { message: I18n.t("validations.location.name") }
validate :validate_units, on: :units validates :units, on: :units, presence: { message: I18n.t("validations.location.units") }
validate :validate_type_of_unit, on: :type_of_unit validates :type_of_unit, on: :type_of_unit, presence: { message: I18n.t("validations.location.type_of_unit") }
validate :validate_mobility_type, on: :mobility_type validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") }
validate :validate_startdate, on: :startdate validate :validate_startdate, on: :startdate
belongs_to :scheme belongs_to :scheme
has_many :lettings_logs, class_name: "LettingsLog" has_many :lettings_logs, class_name: "LettingsLog"
@ -424,34 +424,6 @@ class Location < ApplicationRecord
end end
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 def validate_startdate
unless startdate.between?(Time.zone.local(1900, 1, 1), Time.zone.local(2200, 1, 1)) 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") error_message = I18n.t("validations.location.startdate_out_of_range")

4
spec/requests/locations_controller_spec.rb

@ -13,7 +13,7 @@ RSpec.describe LocationsController, type: :request do
describe "#create" do describe "#create" do
context "when not signed in" do context "when not signed in" do
it "redirects to the sign in page" 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") expect(response).to redirect_to("/account/sign-in")
end end
end end
@ -23,7 +23,7 @@ RSpec.describe LocationsController, type: :request do
before do before do
sign_in user sign_in user
post "/schemes/1/locations/create" get "/schemes/1/locations/create"
end end
it "returns 401 unauthorized" do it "returns 401 unauthorized" do

Loading…
Cancel
Save