diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index c11236026..e424604ee 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -25,18 +25,16 @@ class LocationsController < ApplicationController if @location.errors.present? render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity - else - 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} + elsif 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 @@ -164,8 +162,8 @@ private 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 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" day = params[:location]["deactivation_date(3i)"] @@ -182,17 +180,17 @@ private end def deactivation_date_valid?(day, month, year, collection_start_date) - [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Time.new(2200,1,1)) + [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) end def set_deactivation_date_errors(day, month, year, collection_start_date) if [day, month, year].any?(&:blank?) - {day:, month:, year:}.each do |period, value| - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s )) if value.blank? + { day:, month:, year: }.each do |period, value| + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s)) if value.blank? end elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Time.new(2200,1,1)) + elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) end end diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 2757c5804..c52049707 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -4,20 +4,19 @@ <% content_for :before_content do %> <%= govuk_back_link( text: "Back", - href: "/schemes/#{@location.scheme.id}/locations/#{@location.id}", + href: "/schemes/#{@location.scheme.id}/locations/#{@location.id}", ) %> <% end %> - <%= form_with model: @location, url: scheme_location_deactivate_path(scheme_id: @location.scheme.id, location_id: @location.id), method: "patch", local: true do |f| %>
<% collection_start_date = FormHandler.instance.current_collection_start_date %> <%= f.govuk_error_summary %> <%= f.govuk_radio_buttons_fieldset :deactivation_date, - legend: { text: I18n.t("questions.location.deactivation.apply_from")}, - caption: { text: "Deactivate #{@location.postcode}"}, - hint: { text: I18n.t("hints.location.deactivation", date: collection_start_date.to_formatted_s(:govuk_date))} do %> + legend: { text: I18n.t("questions.location.deactivation.apply_from") }, + 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, @@ -26,9 +25,8 @@ <%= f.govuk_radio_button :deactivation_date, "other", label: { text: "For tenancies starting after a certain date" }, - **basic_conditional_html_attributes({ - "deactivation_date" => ["other"]}, "location") do %> - <%= f.govuk_date_field :deactivation_date, + **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "location") do %> + <%= f.govuk_date_field :deactivation_date, legend: { text: "Date", size: "m" }, hint: { text: "For example, 27 3 2008" }, width: 20 %> diff --git a/app/views/locations/toggle_active_confirm.html.erb b/app/views/locations/toggle_active_confirm.html.erb index 474e36454..bd8f328a9 100644 --- a/app/views/locations/toggle_active_confirm.html.erb +++ b/app/views/locations/toggle_active_confirm.html.erb @@ -7,11 +7,10 @@ <%= "This change will affect #{@location.lettings_logs.count} logs" %> <%= 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 :confirm, value: true %> + <%= f.hidden_field :deactivation_date, value: deactivation_date %>
<%= f.govuk_submit "Deactivate this scheme" %> <%= govuk_button_link_to "Cancel", scheme_location_path(scheme_id: @scheme, id: @location.id), html: { method: :get }, secondary: true %>
<% end %> - \ No newline at end of file diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 80213a73f..a55084e39 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1240,7 +1240,7 @@ RSpec.describe LocationsController, type: :request do let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } let(:startdate) { Time.utc(2021, 1, 2) } - let(:deactivation_date) { Time.new(2022, 10, 10) } + let(:deactivation_date) { Time.utc(2022, 10, 10) } before do Timecop.freeze(Time.utc(2022, 10, 10)) @@ -1248,8 +1248,8 @@ RSpec.describe LocationsController, type: :request do patch "/schemes/#{scheme.id}/locations/#{location.id}/deactivate", params: end - context "default date" do - let(:params) { {location:{ deactivation_date: deactivation_date } } } + context "with default date" do + let(:params) { { location: { deactivation_date: } } } it "renders the confirmation page" do expect(response).to have_http_status(:ok) @@ -1257,8 +1257,8 @@ RSpec.describe LocationsController, type: :request do end end - context "other date" do - let(:params) { {location:{ deactivation_date: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022"} }} + context "with other date" do + 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) @@ -1267,7 +1267,7 @@ RSpec.describe LocationsController, type: :request do end context "when confirming deactivation" do - let(:params) { {location:{ deactivation_date: Time.new(2022, 10, 10), confirm: true } } } + let(:params) { { location: { deactivation_date: Time.utc(2022, 10, 10), confirm: true } } } it "updates existing location with valid deactivation date and renders location page" do follow_redirect! @@ -1279,7 +1279,7 @@ RSpec.describe LocationsController, type: :request do end context "when the date is not selected" do - let(:params) { { location: { "deactivation_date": "" } }} + let(:params) { { location: { "deactivation_date": "" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -1288,7 +1288,7 @@ 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": "other", "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) @@ -1297,7 +1297,7 @@ 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": "other", "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) @@ -1306,7 +1306,7 @@ 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": "other", "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) @@ -1315,7 +1315,7 @@ 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": "other", "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) @@ -1324,7 +1324,7 @@ 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": "other", "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)