Browse Source

lint

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

14
app/controllers/locations_controller.rb

@ -25,10 +25,9 @@ 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"
@ -38,7 +37,6 @@ class LocationsController < ApplicationController
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
def create def create
if date_params_missing?(location_params) || valid_date_params?(location_params) if date_params_missing?(location_params) || valid_date_params?(location_params)
@ -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,7 +180,7 @@ 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)
@ -192,7 +190,7 @@ private
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

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

@ -8,7 +8,6 @@
) %> ) %>
<% 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">
@ -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 %>

10
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,7 +1257,7 @@ 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
@ -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!

Loading…
Cancel
Save