From 3ca673d66879674e62a8d029347117f91baf57e8 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 14 Nov 2022 17:45:52 +0000 Subject: [PATCH] feat: wip validation update --- app/controllers/schemes_controller.rb | 18 ++++++++------- app/models/scheme.rb | 32 ++++++++++----------------- config/locales/en.yml | 6 ++--- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index bbca0e631..48ac8e8a1 100644 --- a/app/controllers/schemes_controller.rb +++ b/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 diff --git a/app/models/scheme.rb b/app/models/scheme.rb index e78a89318..f5e7d43ba 100644 --- a/app/models/scheme.rb +++ b/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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 8a2c45651..9247ebc93 100644 --- a/config/locales/en.yml +++ b/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: