Browse Source

feat: wip validation update

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
3ca673d668
  1. 18
      app/controllers/schemes_controller.rb
  2. 32
      app/models/scheme.rb
  3. 6
      config/locales/en.yml

18
app/controllers/schemes_controller.rb

@ -27,12 +27,13 @@ class SchemesController < ApplicationController
elsif params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present?
confirm_deactivation
else
@scheme.deactivation_date_errors(params)
if @scheme.errors.present?
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else
@scheme.run_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
if @scheme.valid?
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: }
else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
end
end
end
@ -282,7 +283,7 @@ private
end
def confirm_deactivation
if @scheme.update(deactivation_date: params[:scheme][:deactivation_date])
if @scheme.update!(deactivation_date: @scheme.deactivation_date)
flash[:notice] = "#{@scheme.service_name} has been deactivated"
end
redirect_to scheme_details_path(@scheme)
@ -293,12 +294,13 @@ private
collection_start_date = FormHandler.instance.current_collection_start_date
return collection_start_date if params[:scheme][:deactivation_date_type] == "default"
return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date_type].blank?
return nil if params[:scheme][:deactivation_date_type].blank?
day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"]
return nil if [day, month, year].any?(&:blank?)
Date.new(year.to_i, month.to_i, day.to_i)
Date.new(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end
end

32
app/models/scheme.rb

@ -18,10 +18,11 @@ class Scheme < ApplicationRecord
}
validate :validate_confirmed
validate :deactivation_date_errors
auto_strip_attributes :service_name
attr_accessor :deactivation_date_type
attr_accessor :deactivation_date_type, :run_validations
SENSITIVE = {
No: 0,
@ -198,7 +199,7 @@ class Scheme < ApplicationRecord
end
def validate_confirmed
required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date]
required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date deactivation_date_type]
if confirmed == true
required_attributes.any? do |attribute|
@ -224,25 +225,16 @@ class Scheme < ApplicationRecord
status == :active
end
def deactivation_date_errors(params)
if params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank?
errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
end
if params[:scheme][:deactivation_date_type] == "other"
day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"]
def deactivation_date_errors
return unless :run_validations
collection_start_date = FormHandler.instance.current_collection_start_date
if [day, month, year].any?(&:blank?)
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered"))
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i)
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid"))
elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
collection_start_date = FormHandler.instance.current_collection_start_date
if deactivation_date_type.blank?
errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
elsif deactivation_date.blank?
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered"))
elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
end
end

6
config/locales/en.yml

@ -315,15 +315,13 @@ en:
scheme:
deactivation_date:
not_selected: "Select one of the options"
not_entered: "Enter a day, month and year"
invalid: "Enter a valid date"
not_entered: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
location:
deactivation_date:
not_selected: "Select one of the options"
not_entered: "Enter a day, month and year"
invalid: "Enter a valid date"
not_entered: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
soft_validations:

Loading…
Cancel
Save