Browse Source

feat: add second error in correct position and wip date range error

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
af82ea4141
  1. 18
      app/controllers/schemes_controller.rb
  2. 4
      app/models/form_handler.rb
  3. 2
      app/models/scheme.rb
  4. 10
      app/views/schemes/toggle_active.html.erb
  5. 1
      app/views/schemes/toggle_active_confirm.html.erb
  6. 1
      config/locales/en.yml
  7. 7
      db/migrate/20221110111447_add_second_deactivation_field_to_schemes.rb
  8. 3
      db/schema.rb

18
app/controllers/schemes_controller.rb

@ -25,17 +25,18 @@ class SchemesController < ApplicationController
deactivation_date_value = deactivation_date
if @scheme.errors.present?
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type].to_i
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)
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)
else
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value }
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value, deactivation_date_type: params[:scheme][:deactivation_date_type].to_i }
end
end
@ -147,15 +148,22 @@ class SchemesController < ApplicationController
def deactivation_date
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"
return @scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) if params[:scheme][:deactivation_date_type].blank?
return Time.utc(2022, 4, 1) if params[:scheme][:deactivation_date_type].to_i == 1
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)"]
if [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200)
Date.new(year.to_i, month.to_i, day.to_i)
current_collection_start_date = FormHandler.instance.current_collection_start_date
specified_date = Date.new(year.to_i, month.to_i, day.to_i)
if specified_date.before?(current_collection_start_date)
@scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.before_current_collection_start", date: current_collection_start_date))
else
specified_date
end
else
@scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered"))
end

4
app/models/form_handler.rb

@ -49,6 +49,10 @@ class FormHandler
today < window_end_date ? today.year - 1 : today.year
end
def current_collection_start_date
Time.utc(current_collection_start_year, 4, 1)
end
def form_name_from_start_year(year, type)
form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" }
form_mappings[current_collection_start_year - year]

2
app/models/scheme.rb

@ -196,7 +196,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]
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_type]
if confirmed == true
required_attributes.any? do |attribute|

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

@ -9,17 +9,17 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date,
<%= f.govuk_radio_buttons_fieldset :deactivation_date_type,
legend: { text: "When should this change apply?" },
caption: { text: @scheme.service_name },
hint: { text: "If the date is before 1 April 2022, select ‘From the start of the current collection period’ because the previous period has now closed."} do %>
<%= govuk_warning_text text: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected." %>
<%= f.govuk_radio_button :deactivation_date,
Time.utc(2022, 4, 1),
<%= f.govuk_radio_button :deactivation_date_type,
1,
label: { text: "From the start of the current collection period (1 April 2022)" } %>
<%= f.govuk_radio_button :deactivation_date,
"other",
<%= f.govuk_radio_button :deactivation_date_type,
2,
label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({"deactivation_date" => ["other"]}, "scheme") do %>
<%= f.govuk_date_field :deactivation_date,

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

@ -9,6 +9,7 @@
<%= 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, :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 "Deactivate this scheme" %>
<%= govuk_button_link_to "Cancel", scheme_details_path, html: { method: :get }, secondary: true %>

1
config/locales/en.yml

@ -315,6 +315,7 @@ en:
deactivation_date:
not_selected: "Select one of the options"
not_entered: "Enter a date"
before_current_collection_start: "The date must be on or after %{date}"
soft_validations:
net_income:

7
db/migrate/20221110111447_add_second_deactivation_field_to_schemes.rb

@ -0,0 +1,7 @@
class AddSecondDeactivationFieldToSchemes < ActiveRecord::Migration[7.0]
def change
change_table :schemes, bulk: true do |t|
t.column :deactivation_date_type, :integer
end
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_11_09_095650) do
ActiveRecord::Schema[7.0].define(version: 2022_11_10_111447) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -398,6 +398,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_09_095650) do
t.integer "total_units"
t.boolean "confirmed"
t.datetime "deactivation_date"
t.integer "deactivation_date_type"
t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end

Loading…
Cancel
Save