Browse Source

refactor: cleanup of redundant code

pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
cb653bbc05
  1. 97
      app/controllers/locations_controller.rb

97
app/controllers/locations_controller.rb

@ -22,7 +22,7 @@ class LocationsController < ApplicationController
def postcode def postcode
if params[:location].present? if params[:location].present?
@location.postcode = params[:location][:postcode] @location.postcode = PostcodeService.clean(params[:location][:postcode])
@location.location_admin_district = nil @location.location_admin_district = nil
@location.location_code = nil @location.location_code = nil
if @location.valid?(:postcode) if @location.valid?(:postcode)
@ -213,93 +213,8 @@ class LocationsController < ApplicationController
end end
end end
def create
if date_params_missing?(location_params) || valid_date_params?(location_params)
@location = Location.new(location_params)
if @location.save
if @location.location_admin_district.nil?
redirect_to(scheme_location_local_authority_path(scheme_id: @scheme.id, location_id: @location.id, add_another_location: location_params[:add_another_location]))
elsif location_params[:add_another_location] == "Yes"
redirect_to new_scheme_location_path(@scheme)
else
check_answers_path = @scheme.confirmed? ? scheme_check_answers_path(@scheme, anchor: "locations") : scheme_check_answers_path(@scheme)
redirect_to check_answers_path
end
else
render :new, status: :unprocessable_entity
end
else
@location = Location.new(location_params.except("startdate(3i)", "startdate(2i)", "startdate(1i)"))
@location.valid?
@location.errors.add(:startdate)
render :new, status: :unprocessable_entity
end
end
def edit
render_not_found and return unless @location && @scheme
end
def edit_name
render_not_found and return unless @location && @scheme
end
def update
render_not_found and return unless @location && @scheme
page = params[:location][:page]
if page == "edit-local-authority" && !valid_location_admin_district?(location_params)
error_message = I18n.t("validations.location_admin_district")
@location.errors.add :location_admin_district, error_message
render :edit_local_authority, status: :unprocessable_entity
else
if page == "edit-local-authority"
params[:location][:location_code] = Location.local_authorities.key(params[:location][:location_admin_district])
end
if @location.update(location_params)
case page
when "edit"
if @location.location_admin_district.nil?
redirect_to(scheme_location_local_authority_path(scheme_id: @scheme.id, location_id: @location.id, add_another_location: location_params[:add_another_location]))
elsif location_params[:add_another_location] == "Yes"
redirect_to(new_scheme_location_path(@location.scheme))
else
redirect_to(scheme_check_answers_path(@scheme, anchor: "locations"))
end
when "edit-name"
if @scheme.locations.count == @scheme.locations.active.count
redirect_to(scheme_location_path(@scheme, @location))
else
redirect_to(scheme_check_answers_path(@scheme, anchor: "locations"))
end
when "edit-local-authority"
if params[:add_another_location] == "Yes"
redirect_to(new_scheme_location_path(@location.scheme))
else
redirect_to(scheme_check_answers_path(@scheme, anchor: "locations"))
end
end
else
render :edit, status: :unprocessable_entity
end
end
end
private private
def valid_date_params?(location_params)
is_integer?(location_params["startdate(1i)"]) && is_integer?(location_params["startdate(2i)"]) && is_integer?(location_params["startdate(3i)"]) &&
Date.valid_date?(location_params["startdate(1i)"].to_i, location_params["startdate(2i)"].to_i, location_params["startdate(3i)"].to_i)
end
def date_params_missing?(location_params)
location_params["startdate(1i)"].blank? || location_params["startdate(2i)"].blank? || location_params["startdate(3i)"].blank?
end
def is_integer?(string)
string.sub(/^0+/, "").to_i.to_s == string.sub(/^0+/, "")
end
def find_scheme def find_scheme
@scheme = if %w[new create index edit_name].include?(action_name) @scheme = if %w[new create index edit_name].include?(action_name)
Scheme.find(params[:scheme_id]) Scheme.find(params[:scheme_id])
@ -322,20 +237,10 @@ private
end end
end end
def location_params
required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :add_another_location, :startdate, :mobility_type, :location_admin_district, :location_code).merge(scheme_id: @scheme.id)
required_params[:postcode] = PostcodeService.clean(required_params[:postcode]) if required_params[:postcode]
required_params
end
def search_term def search_term
params["search"] params["search"]
end end
def valid_location_admin_district?(location_params)
location_params["location_admin_district"] != "Select an option"
end
def deactivate_success_notice def deactivate_success_notice
case @location.status case @location.status
when :deactivated when :deactivated

Loading…
Cancel
Save