Browse Source

lint

pull/981/head
Kat 4 years ago
parent
commit
352a8f1959
  1. 20
      app/controllers/locations_controller.rb
  2. 10
      app/views/locations/toggle_active.html.erb
  3. 5
      app/views/locations/toggle_active_confirm.html.erb
  4. 24
      spec/requests/locations_controller_spec.rb

20
app/controllers/locations_controller.rb

@ -25,18 +25,16 @@ class LocationsController < ApplicationController
if @location.errors.present? if @location.errors.present?
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else elsif deactivation_date_value.blank?
if deactivation_date_value.blank?
render "toggle_active", locals: { action: "deactivate" } render "toggle_active", locals: { action: "deactivate" }
elsif (params[:location][:confirm].present?) elsif params[:location][:confirm].present?
if @location.update(deactivation_date: deactivation_date_value) if @location.update(deactivation_date: deactivation_date_value)
# update the logs # update the logs
flash[:notice] = "#{@location.name} has been deactivated" flash[:notice] = "#{@location.name} has been deactivated"
end end
redirect_to scheme_locations_path(@scheme) redirect_to scheme_locations_path(@scheme)
else else
render "toggle_active_confirm", locals: {action: "deactivate", deactivation_date: deactivation_date_value} render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value }
end
end end
end end
@ -164,8 +162,8 @@ private
end end
def deactivation_date def deactivation_date
return unless params[:location].present? return if params[:location].blank?
return @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_selected")) unless params[:location][:deactivation_date].present? 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 params[:location][:deactivation_date] unless params[:location][:deactivation_date] == "other"
day = params[:location]["deactivation_date(3i)"] day = params[:location]["deactivation_date(3i)"]
@ -182,17 +180,17 @@ private
end end
def deactivation_date_valid?(day, month, year, collection_start_date) 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 end
def set_deactivation_date_errors(day, month, year, collection_start_date) def set_deactivation_date_errors(day, month, year, collection_start_date)
if [day, month, year].any?(&:blank?) if [day, month, year].any?(&:blank?)
{day:, month:, year:}.each do |period, value| { 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? @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s)) if value.blank?
end end
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) 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")) @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))) @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
end end

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

@ -8,16 +8,15 @@
) %> ) %>
<% end %> <% 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| %> <%= 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-row">
<div class="govuk-grid-column-two-thirds"> <div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %> <% collection_start_date = FormHandler.instance.current_collection_start_date %>
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date, <%= f.govuk_radio_buttons_fieldset :deactivation_date,
legend: { text: I18n.t("questions.location.deactivation.apply_from")}, legend: { text: I18n.t("questions.location.deactivation.apply_from") },
caption: { text: "Deactivate #{@location.postcode}"}, caption: { text: "Deactivate #{@location.postcode}" },
hint: { text: I18n.t("hints.location.deactivation", date: collection_start_date.to_formatted_s(:govuk_date))} do %> 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") %> <%= govuk_warning_text text: I18n.t("warnings.location.deactivation.existing_logs") %>
<%= f.govuk_radio_button :deactivation_date, <%= f.govuk_radio_button :deactivation_date,
collection_start_date, collection_start_date,
@ -26,8 +25,7 @@
<%= f.govuk_radio_button :deactivation_date, <%= f.govuk_radio_button :deactivation_date,
"other", "other",
label: { text: "For tenancies starting after a certain date" }, label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({ **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "location") do %>
"deactivation_date" => ["other"]}, "location") do %>
<%= f.govuk_date_field :deactivation_date, <%= f.govuk_date_field :deactivation_date,
legend: { text: "Date", size: "m" }, legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2008" }, hint: { text: "For example, 27 3 2008" },

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

@ -7,11 +7,10 @@
<%= "This change will affect #{@location.lettings_logs.count} logs" %> <%= "This change will affect #{@location.lettings_logs.count} logs" %>
</h1> </h1>
<%= govuk_warning_text text: I18n.t("warnings.location.deactivation.review_logs") %> <%= govuk_warning_text text: I18n.t("warnings.location.deactivation.review_logs") %>
<%= 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 %>
<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_location_path(scheme_id: @scheme, id: @location.id), 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> </div>
<% end %> <% end %>

24
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!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) } let!(:location) { FactoryBot.create(:location, scheme:) }
let(:startdate) { Time.utc(2021, 1, 2) } 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 before do
Timecop.freeze(Time.utc(2022, 10, 10)) 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: patch "/schemes/#{scheme.id}/locations/#{location.id}/deactivate", params:
end end
context "default date" do context "with default date" do
let(:params) { {location:{ deactivation_date: deactivation_date } } } let(:params) { { location: { deactivation_date: } } }
it "renders the confirmation page" do it "renders the confirmation page" do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
@ -1257,8 +1257,8 @@ RSpec.describe LocationsController, type: :request do
end end
end end
context "other date" do context "with other date" do
let(:params) { {location:{ 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 it "renders the confirmation page" do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
@ -1267,7 +1267,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when confirming deactivation" do 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 it "updates existing location with valid deactivation date and renders location page" do
follow_redirect! follow_redirect!
@ -1279,7 +1279,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the date is not selected" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1288,7 +1288,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when invalid date is entered" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1297,7 +1297,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the date is entered is before the beginning of current collection window" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1306,7 +1306,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the day is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1315,7 +1315,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the month is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1324,7 +1324,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the year is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)

Loading…
Cancel
Save