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 def deactivate
if params[:scheme].blank? if params[:scheme].blank?
render "toggle_active", locals: { action: "deactivate" } render "toggle_active", locals: { action: "deactivate" }
elsif params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present?
confirm_deactivation
else else
@scheme.run_validations = true @scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date @scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
if @scheme.valid? if params[:scheme][:confirm].present?
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } 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 else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
end end
@ -294,13 +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 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)"] 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?) 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
end end

4
app/models/scheme.rb

@ -22,7 +22,7 @@ class Scheme < ApplicationRecord
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type, :run_validations attr_accessor :deactivation_date_type, :run_deactivation_validations
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,
@ -226,7 +226,7 @@ class Scheme < ApplicationRecord
end end
def deactivation_date_errors def deactivation_date_errors
return unless :run_validations return unless run_deactivation_validations == true
collection_start_date = FormHandler.instance.current_collection_start_date collection_start_date = FormHandler.instance.current_collection_start_date
if deactivation_date_type.blank? 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") %> <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %>
<%= f.hidden_field :confirm, value: true %> <%= f.hidden_field :confirm, value: true %>
<%= f.hidden_field :deactivation_date, value: deactivation_date %> <%= f.hidden_field :deactivation_date, value: deactivation_date %>
<%= f.hidden_field :deactivation_date_type, value: deactivation_date_type %>
<div class="govuk-button-group"> <div class="govuk-button-group">
<%= f.govuk_submit "#{action.humanize} this scheme" %> <%= f.govuk_submit "#{action.humanize} this scheme" %>
<%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %> <%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>

Loading…
Cancel
Save