|
|
|
@ -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 |
|
|
|
|