Browse Source

feat: wip errors refactoring

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
62d511bc75
  1. 52
      app/controllers/schemes_controller.rb
  2. 2
      app/views/schemes/toggle_active.html.erb
  3. 2
      app/views/schemes/toggle_active_confirm.html.erb
  4. 6
      config/locales/en.yml

52
app/controllers/schemes_controller.rb

@ -22,32 +22,20 @@ class SchemesController < ApplicationController
end
def deactivate
if params[:scheme] && params[:scheme][:confirm] && deactivation_date.present?
if @scheme.update!(deactivation_date:)
deactivation_date_value = deactivation_date
if @scheme.errors.present?
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
elsif deactivation_date_value.blank?
render "toggle_active", locals: { action: "deactivate" }
elsif params[:scheme][:confirm].present?
if @scheme.update(deactivation_date: deactivation_date_value)
# update the logs
flash[:notice] = "#{@scheme.service_name} has been deactivated"
end
redirect_to scheme_details_path(@scheme)
elsif deactivation_date.present?
if deactivation_date == "other"
if params[:scheme]["deactivation_date(1i)"].blank? && params[:scheme]["deactivation_date(2i)"].blank? && params[:scheme]["deactivation_date(3i)"].blank?
@scheme.errors.add(:deactivation_date, message: "Enter a date")
elsif params[:scheme]["deactivation_date(3i)"].blank?
@scheme.errors.add(:deactivation_date, message: "Enter the day")
elsif params[:scheme]["deactivation_date(2i)"].blank?
@scheme.errors.add(:deactivation_date, message: "Enter the month")
elsif params[:scheme]["deactivation_date(1i)"].blank?
@scheme.errors.add(:deactivation_date, message: "Enter the year")
end
render "toggle_active", locals: { action: "deactivate", deactivation_date: }, status: :unprocessable_entity
else
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: }
end
elsif params[:scheme]
@scheme.errors.add(:deactivation_date, message: "Select one of the options")
render "toggle_active", locals: { action: "deactivate", deactivation_date: }, status: :unprocessable_entity
else
render "toggle_active", locals: { action: "deactivate" }
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value }
end
end
@ -158,12 +146,24 @@ class SchemesController < ApplicationController
end
def deactivation_date
if params[:scheme].blank?
nil
elsif params[:scheme][:deactivation_date] == "other" && params[:scheme]["deactivation_date(1i)"].present? && params[:scheme]["deactivation_date(2i)"].present? && params[:scheme]["deactivation_date(3i)"].present?
Time.utc(params[:scheme]["deactivation_date(1i)"].to_i, params[:scheme]["deactivation_date(2i)"].to_i, params[:scheme]["deactivation_date(3i)"].to_i)
return if params[:scheme].blank?
return @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_selected")) if params[:scheme][:deactivation_date].blank?
return params[:scheme][:deactivation_date] unless params[:scheme][:deactivation_date] == "other"
day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"]
if [day, month, year].any?(&:blank?)
{ day:, month:, year: }.each do |period, value|
@scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered", period: period.to_s)) if value.blank?
end
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i)
@scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid"))
elsif !year.to_i.between?(2000, 2200)
@scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid"))
else
params[:scheme][:deactivation_date]
Date.new(year.to_i, month.to_i, day.to_i)
end
end

2
app/views/schemes/toggle_active.html.erb

@ -22,7 +22,7 @@
"other",
label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({"deactivation_date" => ["other"]}, "scheme") do %>
<%= f.govuk_date_field :deactivation_date_field,
<%= f.govuk_date_field :deactivation_date,
legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2008" },
width: 20 %>

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

@ -7,7 +7,7 @@
<%= "This change will affect SOME logs" %>
</h1>
<%= govuk_warning_text text: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." %>
<%= f.hidden_field :confirm %>
<%= f.hidden_field :confirm, :value => true %>
<%= f.hidden_field :deactivation_date, :value => deactivation_date %>
<div class="govuk-button-group">
<%= f.govuk_submit "Deactivate this scheme" %>

6
config/locales/en.yml

@ -311,6 +311,12 @@ en:
declaration:
missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log."
scheme:
deactivation_date:
not_selected: "Select one of the options"
not_entered: "Enter a %{period}"
invalid: "Enter a valid date"
soft_validations:
net_income:
title_text: "Net income is outside the expected range based on the lead tenant’s working situation"

Loading…
Cancel
Save