Browse Source

feat: add validation to model layer

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
e3177c47ed
  1. 14
      app/controllers/schemes_controller.rb
  2. 4
      app/models/scheme.rb
  3. 1
      app/views/schemes/toggle_active_confirm.html.erb

14
app/controllers/schemes_controller.rb

@ -24,14 +24,14 @@ class SchemesController < ApplicationController
def deactivate
if params[:scheme].blank?
render "toggle_active", locals: { action: "deactivate" }
elsif params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present?
confirm_deactivation
else
@scheme.run_validations = true
@scheme.run_deactivation_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: }
if params[:scheme][:confirm].present?
confirm_deactivation
elsif @scheme.valid?
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type }
else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
end
@ -294,13 +294,13 @@ private
collection_start_date = FormHandler.instance.current_collection_start_date
return collection_start_date if params[:scheme][:deactivation_date_type] == "default"
return nil if params[:scheme][:deactivation_date_type].blank?
return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date].present?
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) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
Time.utc(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end
end

4
app/models/scheme.rb

@ -22,7 +22,7 @@ class Scheme < ApplicationRecord
auto_strip_attributes :service_name
attr_accessor :deactivation_date_type, :run_validations
attr_accessor :deactivation_date_type, :run_deactivation_validations
SENSITIVE = {
No: 0,
@ -226,7 +226,7 @@ class Scheme < ApplicationRecord
end
def deactivation_date_errors
return unless :run_validations
return unless run_deactivation_validations == true
collection_start_date = FormHandler.instance.current_collection_start_date
if deactivation_date_type.blank?

1
app/views/schemes/toggle_active_confirm.html.erb

@ -11,6 +11,7 @@
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %>
<%= f.hidden_field :confirm, value: true %>
<%= f.hidden_field :deactivation_date, value: deactivation_date %>
<%= f.hidden_field :deactivation_date_type, value: deactivation_date_type %>
<div class="govuk-button-group">
<%= f.govuk_submit "#{action.humanize} this scheme" %>
<%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>

Loading…
Cancel
Save