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

32
app/models/scheme.rb

@ -18,10 +18,11 @@ class Scheme < ApplicationRecord
} }
validate :validate_confirmed validate :validate_confirmed
validate :deactivation_date_errors
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type attr_accessor :deactivation_date_type, :run_validations
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,
@ -198,7 +199,7 @@ class Scheme < ApplicationRecord
end end
def validate_confirmed 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 if confirmed == true
required_attributes.any? do |attribute| required_attributes.any? do |attribute|
@ -224,25 +225,16 @@ class Scheme < ApplicationRecord
status == :active status == :active
end end
def deactivation_date_errors(params) def deactivation_date_errors
if params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank? return unless :run_validations
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)"]
collection_start_date = FormHandler.instance.current_collection_start_date collection_start_date = FormHandler.instance.current_collection_start_date
if deactivation_date_type.blank?
if [day, month, year].any?(&:blank?) errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered")) elsif deactivation_date.blank?
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered"))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1))
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)))
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
end end
end end

6
config/locales/en.yml

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

Loading…
Cancel
Save