Browse Source

Refactor locations and schemes controller actions

- Rename confirmation partials to `deactivate_confirm.html.erb` so that they match the actions in which they belong to
- Make all deactivation date comparision UTC time
pull/980/head
James Rose 2 years ago
parent
commit
adbdc9be7e
  1. 26
      app/controllers/locations_controller.rb
  2. 24
      app/controllers/schemes_controller.rb
  3. 2
      app/models/form_handler.rb
  4. 4
      app/models/location.rb
  5. 4
      app/models/scheme.rb
  6. 6
      app/views/locations/deactivate_confirm.html.erb
  7. 2
      app/views/locations/toggle_active.html.erb
  8. 6
      app/views/schemes/deactivate_confirm.html.erb
  9. 12
      db/schema.rb
  10. 2
      spec/models/form_handler_spec.rb

26
app/controllers/locations_controller.rb

@ -24,7 +24,7 @@ class LocationsController < ApplicationController
if params[:location].blank?
render "toggle_active", locals: { action: "deactivate" }
else
@location.run_deactivation_validations = true
@location.run_deactivation_validations!
@location.deactivation_date = deactivation_date
@location.deactivation_date_type = params[:location][:deactivation_date_type]
if @location.valid?
@ -36,14 +36,17 @@ class LocationsController < ApplicationController
end
def deactivate_confirm
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] }
@deactivation_date = params[:deactivation_date]
@deactivation_date_type = params[:deactivation_date_type]
end
def deactivate
@location.run_deactivation_validations = true
@location.deactivation_date = deactivation_date
@location.deactivation_date_type = params[:location][:deactivation_date_type]
confirm_deactivation
@location.run_deactivation_validations!
if @location.update!(deactivation_date:)
flash[:notice] = deactivate_success_notice
end
redirect_to scheme_location_path(@scheme, @location)
end
def reactivate
@ -173,14 +176,7 @@ private
location_params["location_admin_district"] != "Select an option"
end
def confirm_deactivation
if @location.update!(deactivation_date: @location.deactivation_date)
flash[:notice] = success_text
end
redirect_to scheme_location_path(@scheme, @location)
end
def success_text
def deactivate_success_notice
case @location.status
when :deactivated
"#{@location.name} has been deactivated"
@ -203,6 +199,6 @@ private
year = params[:location]["deactivation_date(1i)"]
return nil if [day, month, year].any?(&:blank?)
Time.utc(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end
end

24
app/controllers/schemes_controller.rb

@ -37,14 +37,17 @@ class SchemesController < ApplicationController
end
def deactivate_confirm
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] }
@deactivation_date = params[:deactivation_date]
@deactivation_date_type = params[:deactivation_date_type]
end
def deactivate
@scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
confirm_deactivation
@scheme.run_deactivation_validations!
if @scheme.update!(deactivation_date:)
flash[:notice] = deactivate_success_notice
end
redirect_to scheme_details_path(@scheme)
end
def reactivate
@ -291,14 +294,7 @@ private
redirect_to @scheme if @scheme.confirmed?
end
def confirm_deactivation
if @scheme.update!(deactivation_date: @scheme.deactivation_date)
flash[:notice] = success_text
end
redirect_to scheme_details_path(@scheme)
end
def success_text
def deactivate_success_notice
case @scheme.status
when :deactivated
"#{@scheme.service_name} has been deactivated"
@ -321,6 +317,6 @@ private
year = params[:scheme]["deactivation_date(1i)"]
return nil if [day, month, year].any?(&:blank?)
Time.utc(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end
end

2
app/models/form_handler.rb

@ -50,7 +50,7 @@ class FormHandler
end
def current_collection_start_date
Time.utc(current_collection_start_year, 4, 1)
Time.zone.local(current_collection_start_year, 4, 1)
end
def form_name_from_start_year(year, type)

4
app/models/location.rb

@ -384,6 +384,10 @@ class Location < ApplicationRecord
status == :active
end
def run_deactivation_validations!
@run_deactivation_validations = true
end
def implicit_run_deactivation_validations
deactivation_date.present? || @run_deactivation_validations
end

4
app/models/scheme.rb

@ -226,6 +226,10 @@ class Scheme < ApplicationRecord
status == :active
end
def run_deactivation_validations!
@run_deactivation_validations = true
end
def implicit_run_deactivation_validations
deactivation_date.present? || @run_deactivation_validations
end

6
app/views/locations/toggle_active_confirm.html.erb → app/views/locations/deactivate_confirm.html.erb

@ -8,10 +8,10 @@
</h1>
<%= govuk_warning_text text: I18n.t("warnings.location.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 %>
<%= 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 location" %>
<%= govuk_button_link_to "Cancel", scheme_location_path(scheme_id: @scheme, id: @location.id), html: { method: :get }, secondary: true %>
<%= govuk_button_link_to "Cancel", scheme_location_path(@scheme, @location), html: { method: :get }, secondary: true %>
</div>
<% end %>

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

@ -4,7 +4,7 @@
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: scheme_location_path(scheme_id: @location.scheme.id, id: @location.id),
href: scheme_location_path(@location.scheme, @location),
) %>
<% end %>

6
app/views/schemes/toggle_active_confirm.html.erb → app/views/schemes/deactivate_confirm.html.erb

@ -1,4 +1,4 @@
<% title = "#{action.humanize} #{@scheme.service_name}" %>
<% title = "Deactivate #{@scheme.service_name}" %>
<% content_for :title, title %>
<%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
<% content_for :before_content do %>
@ -10,8 +10,8 @@
</h1>
<%= 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 %>
<%= 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 %>

12
db/schema.rb

@ -360,6 +360,12 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_11_102656) do
t.integer "hholdcount"
t.integer "age3"
t.integer "age3_known"
t.integer "age4"
t.integer "age4_known"
t.integer "age5"
t.integer "age5_known"
t.integer "age6"
t.integer "age6_known"
t.string "la"
t.integer "la_known"
t.integer "income1"
@ -367,12 +373,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_11_102656) do
t.integer "details_known_2"
t.integer "details_known_3"
t.integer "details_known_4"
t.integer "age4"
t.integer "age4_known"
t.integer "age5"
t.integer "age5_known"
t.integer "age6"
t.integer "age6_known"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"

2
spec/models/form_handler_spec.rb

@ -120,7 +120,7 @@ RSpec.describe FormHandler do
end
it "returns the correct current start date" do
expect(form_handler.current_collection_start_date).to eq(Time.utc(2022, 4, 1))
expect(form_handler.current_collection_start_date).to eq(Time.zone.local(2022, 4, 1))
end
end

Loading…
Cancel
Save