Browse Source

refactor: respond to PR comments 3 (locations side)

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
04f11ea532
  1. 24
      app/controllers/locations_controller.rb
  2. 22
      app/models/location.rb

24
app/controllers/locations_controller.rb

@ -26,7 +26,7 @@ class LocationsController < ApplicationController
elsif params[:location][:confirm].present? && params[:location][:deactivation_date].present? elsif params[:location][:confirm].present? && params[:location][:deactivation_date].present?
confirm_deactivation confirm_deactivation
else else
deactivation_date_errors @location.deactivation_date_errors(params)
if @location.errors.present? if @location.errors.present?
@location.deactivation_date_type = params[:location][:deactivation_date_type] @location.deactivation_date_type = params[:location][:deactivation_date_type]
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
@ -170,28 +170,6 @@ private
redirect_to scheme_location_path(@scheme, @location) redirect_to scheme_location_path(@scheme, @location)
end end
def deactivation_date_errors
if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank?
@location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected"))
end
if params[:location][:deactivation_date_type] == "other"
day = params[:location]["deactivation_date(3i)"]
month = params[:location]["deactivation_date(2i)"]
year = params[:location]["deactivation_date(1i)"]
collection_start_date = FormHandler.instance.current_collection_start_date
if [day, month, year].any?(&:blank?)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered"))
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1))
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
end
end
def deactivation_date def deactivation_date
return if params[:location].blank? return if params[:location].blank?

22
app/models/location.rb

@ -382,6 +382,28 @@ class Location < ApplicationRecord
status == :active status == :active
end end
def deactivation_date_errors(params)
if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank?
@location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected"))
end
if params[:location][:deactivation_date_type] == "other"
day = params[:location]["deactivation_date(3i)"]
month = params[:location]["deactivation_date(2i)"]
year = params[:location]["deactivation_date(1i)"]
collection_start_date = FormHandler.instance.current_collection_start_date
if [day, month, year].any?(&:blank?)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered"))
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1))
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
end
end
private private
PIO = PostcodeService.new PIO = PostcodeService.new

Loading…
Cancel
Save