Browse Source

Display 2 errors

pull/981/head
Kat 4 years ago
parent
commit
68c49f8db5
  1. 38
      app/controllers/locations_controller.rb
  2. 5
      app/views/locations/toggle_active.html.erb
  3. 4
      app/views/locations/toggle_active_confirm.html.erb
  4. 5
      config/locales/en.yml
  5. 34
      spec/requests/locations_controller_spec.rb

38
app/controllers/locations_controller.rb

@ -21,14 +21,22 @@ class LocationsController < ApplicationController
def show; end
def deactivate
if params[:deactivation_date].blank?
render "toggle_active", locals: { action: "deactivate" }
elsif (params[:confirm])
# update the deactivation_date
# update the logs
# redirect to location page
deactivation_date_value = deactivation_date
if @location.errors.present?
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else
render "toggle_active_confirm", locals: {action: "deactivate", deactivation_date: params[:deactivation_date]}
if deactivation_date_value.blank?
render "toggle_active", locals: { action: "deactivate" }
elsif (params[:location][:confirm].present?)
if @location.update(deactivation_date: deactivation_date_value)
# update the logs
flash[:notice] = "#{@location.name} has been deactivated"
end
redirect_to scheme_locations_path(@scheme)
else
render "toggle_active_confirm", locals: {action: "deactivate", deactivation_date: deactivation_date_value}
end
end
end
@ -154,4 +162,20 @@ private
def valid_location_admin_district?(location_params)
location_params["location_admin_district"] != "Select an option"
end
def deactivation_date
return unless params[:location].present?
return @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_selected")) unless params[:location][:deactivation_date].present?
return params[:location][:deactivation_date] unless params[:location][:deactivation_date] == "other"
day = params[:location]["deactivation_date(3i)"]
month = params[:location]["deactivation_date(2i)"]
year = params[:location]["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)
else
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered"))
end
end
end

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

@ -4,14 +4,15 @@
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: "/schemes/#{@scheme.id}/locations/#{@location.id}",
href: "/schemes/#{@location.scheme.id}/locations/#{@location.id}",
) %>
<% end %>
<%= form_with url: location_deactivate_path(@location), method: "patch", local: true do |f| %>
<%= form_with model: @location, url: scheme_location_deactivate_path(scheme_id: @location.scheme.id, location_id: @location.id), method: "patch", local: true do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date,
legend: { text: "When should this change apply?"},
caption: { text: "Deactivate #{@location.postcode}"},

4
app/views/locations/toggle_active_confirm.html.erb

@ -1,4 +1,4 @@
<%= form_with url: location_deactivate_path(@location), method: "patch", local: true do |f| %>
<%= form_with model: @location, url: scheme_location_deactivate_path(@location), method: "patch", local: true do |f| %>
<% content_for :before_content do %>
<%= govuk_back_link(href: :back) %>
<% end %>
@ -11,7 +11,7 @@
<%= f.hidden_field :deactivation_date, :value => deactivation_date %>
<div class="govuk-button-group">
<%= f.govuk_submit "Deactivate this scheme" %>
<%= govuk_button_link_to "Cancel", location_path, html: { method: :get }, secondary: true %>
<%= govuk_button_link_to "Cancel", scheme_location_path(scheme_id: @scheme, id: @location.id), html: { method: :get }, secondary: true %>
</div>
<% end %>

5
config/locales/en.yml

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

34
spec/requests/locations_controller_spec.rb

@ -1249,7 +1249,7 @@ RSpec.describe LocationsController, type: :request do
end
context "default date" do
let(:params) { { deactivation_date: deactivation_date } }
let(:params) { {location:{ deactivation_date: deactivation_date } } }
it "renders the confirmation page" do
expect(response).to have_http_status(:ok)
@ -1258,13 +1258,43 @@ RSpec.describe LocationsController, type: :request do
end
context "other date" do
let(:params) { { deactivation_date: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022"} }
let(:params) { {location:{ deactivation_date: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022"} }}
it "renders the confirmation page" do
expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect SOME logs")
end
end
context "when confirming deactivation" do
let(:params) { {location:{ deactivation_date: Time.new(2022, 10, 10), confirm: true } } }
it "updates existing location with valid deactivation date and renders location page" do
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
location.reload
expect(location.deactivation_date).to eq(deactivation_date)
end
end
context "when the date is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "", "deactivation_date(1i)": ""} }}
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered"))
end
end
context "when the date is not selected" do
let(:params) { { location: { "deactivation_date": "" } }}
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_selected"))
end
end
end
end
end

Loading…
Cancel
Save