Browse Source

Keep deactivation_date_type selected if an error occurs

pull/981/head
Kat 4 years ago
parent
commit
68630cd292
  1. 9
      app/controllers/locations_controller.rb
  2. 2
      app/models/location.rb
  3. 8
      app/views/locations/toggle_active.html.erb
  4. 22
      spec/requests/locations_controller_spec.rb

9
app/controllers/locations_controller.rb

@ -24,6 +24,7 @@ class LocationsController < ApplicationController
deactivation_date_value = deactivation_date
if @location.errors.present?
@location.deactivation_date_type = params[:location][: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" }
@ -163,8 +164,12 @@ private
def deactivation_date
return if params[:location].blank?
return @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_selected")) if params[:location][:deactivation_date].blank?
return params[:location][:deactivation_date] unless params[:location][:deactivation_date] == "other"
return @location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank?
collection_start_date = FormHandler.instance.current_collection_start_date
return collection_start_date if params[:location][:deactivation_date_type].to_i == 1
return params[:location][:deactivation_date] if params[:location][:deactivation_date_type].blank?
day = params[:location]["deactivation_date(3i)"]
month = params[:location]["deactivation_date(2i)"]

2
app/models/location.rb

@ -18,6 +18,8 @@ class Location < ApplicationRecord
scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) }
scope :active, -> { where(confirmed: true).and(started) }
attribute :deactivation_date_type
LOCAL_AUTHORITIES = {
"E07000223": "Adur",
"E07000026": "Allerdale",

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

@ -18,12 +18,12 @@
caption: { text: "Deactivate #{@location.postcode}" },
hint: { text: I18n.t("hints.location.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } do %>
<%= govuk_warning_text text: I18n.t("warnings.location.deactivation.existing_logs") %>
<%= f.govuk_radio_button :deactivation_date,
collection_start_date,
<%= f.govuk_radio_button :deactivation_date_type,
1,
label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %>
<%= 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" => %w[other] }, "location") do %>
<%= f.govuk_date_field :deactivation_date,

22
spec/requests/locations_controller_spec.rb

@ -1249,7 +1249,7 @@ RSpec.describe LocationsController, type: :request do
end
context "with default date" do
let(:params) { { location: { deactivation_date: } } }
let(:params) { { location: { deactivation_date_type: 1 } } }
it "renders the confirmation page" do
expect(response).to have_http_status(:ok)
@ -1288,8 +1288,8 @@ RSpec.describe LocationsController, type: :request do
end
context "when invalid date is entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "44", "deactivation_date(1i)": "2022" } } }
let(:params) { { location: { deactivation_date_type: 2, "deactivation_date(3i)": "10", "deactivation_date(2i)": "44", "deactivation_date(1i)": "2022" } } }
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.invalid"))
@ -1297,8 +1297,8 @@ RSpec.describe LocationsController, type: :request do
end
context "when the date is entered is before the beginning of current collection window" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "4", "deactivation_date(1i)": "2020" } } }
let(:params) { { location: { deactivation_date_type: 2, "deactivation_date(3i)": "10", "deactivation_date(2i)": "4", "deactivation_date(1i)": "2020" } } }
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.out_of_range", date: "5 April 2022"))
@ -1306,8 +1306,8 @@ RSpec.describe LocationsController, type: :request do
end
context "when the day is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022" } } }
let(:params) { { location: { deactivation_date_type: 2, "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022" } } }
it "displays 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", period: "day"))
@ -1315,8 +1315,8 @@ RSpec.describe LocationsController, type: :request do
end
context "when the month is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "", "deactivation_date(1i)": "2022" } } }
let(:params) { { location: { deactivation_date_type: 2, "deactivation_date(3i)": "2", "deactivation_date(2i)": "", "deactivation_date(1i)": "2022" } } }
it "displays 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", period: "month"))
@ -1324,8 +1324,8 @@ RSpec.describe LocationsController, type: :request do
end
context "when the year is not entered" do
let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "2", "deactivation_date(1i)": "" } } }
let(:params) { { location: { deactivation_date_type: 2, "deactivation_date(3i)": "2", "deactivation_date(2i)": "2", "deactivation_date(1i)": "" } } }
it "displays 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", period: "year"))

Loading…
Cancel
Save