Browse Source

refactor: tidy up controller logic

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
d7a44b3ae5
  1. 93
      app/controllers/schemes_controller.rb
  2. 1
      app/models/lettings_log.rb
  3. 1
      app/models/scheme.rb
  4. 2
      app/views/schemes/toggle_active.html.erb
  5. 1
      app/views/schemes/toggle_active_confirm.html.erb
  6. 14
      config/locales/en.yml

93
app/controllers/schemes_controller.rb

@ -22,22 +22,22 @@ class SchemesController < ApplicationController
end end
def deactivate def deactivate
deactivation_date_value = deactivation_date if params[:scheme].present? && params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present?
confirm_deactivation
else
deactivation_date_errors
if @scheme.errors.present? if @scheme.errors.present?
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
@scheme.deactivation_date = nil # @scheme.deactivation_date = deactivation_date_errors
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
elsif deactivation_date_value.blank? else
deactivation_date_value = deactivation_date
if deactivation_date_value.blank?
render "toggle_active", locals: { action: "deactivate" } 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)
else else
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value, deactivation_date_type: params[:scheme][:deactivation_date_type] } render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value }
end
end
end end
end end
@ -147,29 +147,6 @@ class SchemesController < ApplicationController
render "schemes/support_services_provider" render "schemes/support_services_provider"
end end
def deactivation_date
return if params[:scheme].blank?
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] == "default"
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)
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
end
private private
def validation_errors(scheme_params) def validation_errors(scheme_params)
@ -303,4 +280,52 @@ private
def redirect_if_scheme_confirmed def redirect_if_scheme_confirmed
redirect_to @scheme if @scheme.confirmed? redirect_to @scheme if @scheme.confirmed?
end end
def confirm_deactivation
if @scheme.update(deactivation_date: params[:scheme][:deactivation_date])
@scheme.lettings_logs.filter_by_before_startdate(params[:scheme][:deactivation_date]).update!(scheme: nil)
flash[:notice] = "#{@scheme.service_name} has been deactivated"
end
redirect_to scheme_details_path(@scheme)
nil
end
def deactivation_date_errors
if params[:scheme].present? && params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank?
@scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
end
if params[:scheme].present? && params[:scheme][:deactivation_date_type] == "other"
day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"]
collection_start_date = FormHandler.instance.current_collection_start_date
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
# { 1 => year.to_i, 2 => month.to_i, 3 => day.to_i }
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 !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1))
@scheme.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
def deactivation_date
return if params[:scheme].blank?
collection_start_date = FormHandler.instance.current_collection_start_date
return collection_start_date if params[:scheme][:deactivation_date_type] == "default"
return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date_type].blank?
day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"]
Date.new(year.to_i, month.to_i, day.to_i)
end
end end

1
app/models/lettings_log.rb

@ -38,6 +38,7 @@ class LettingsLog < Log
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) }
scope :search_by, lambda { |param| scope :search_by, lambda { |param|
filter_by_location_postcode(param) filter_by_location_postcode(param)
.or(filter_by_tenant_code(param)) .or(filter_by_tenant_code(param))

1
app/models/scheme.rb

@ -22,6 +22,7 @@ class Scheme < ApplicationRecord
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type attr_accessor :deactivation_date_type
attr_accessor :deactivation_date
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,

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

@ -11,7 +11,7 @@
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date_type, <%= f.govuk_radio_buttons_fieldset :deactivation_date_type,
legend: { text: "When should this change apply?" }, legend: { text: "When should this change apply?" },
caption: { text: @scheme.service_name }, caption: { text: "Deactivate #{@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 %> 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." %> <%= 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_type, <%= f.govuk_radio_button :deactivation_date_type,

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

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

14
config/locales/en.yml

@ -314,8 +314,9 @@ en:
scheme: scheme:
deactivation_date: deactivation_date:
not_selected: "Select one of the options" not_selected: "Select one of the options"
not_entered: "Enter a date" not_entered: "Enter a %{period}"
before_current_collection_start: "The date must be on or after %{date}" invalid: "Enter a valid date"
out_of_range: "The date must be on or after the %{date}"
soft_validations: soft_validations:
net_income: net_income:
@ -367,6 +368,8 @@ en:
startdate: "When did the first property in this location become available under this scheme? (optional)" startdate: "When did the first property in this location become available under this scheme? (optional)"
add_another_location: "Do you want to add another location?" add_another_location: "Do you want to add another location?"
mobility_type: "What are the mobility standards for the majority of units in this location?" mobility_type: "What are the mobility standards for the majority of units in this location?"
deactivation:
apply_from: "When should this change apply?"
descriptions: descriptions:
location: location:
mobility_type: mobility_type:
@ -379,6 +382,13 @@ en:
postcode: "For example, SW1P 4DF." postcode: "For example, SW1P 4DF."
name: "This is how you refer to this location within your organisation" name: "This is how you refer to this location within your organisation"
units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff." units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff."
deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
warnings:
location:
deactivation:
existing_logs: "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."
review_logs: "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."
test: test:
one_argument: "This is based on the tenant’s work situation: %{ecstat1}" one_argument: "This is based on the tenant’s work situation: %{ecstat1}"

Loading…
Cancel
Save