Browse Source

feat: fix tests, add status tag behaviour and remove unnecessary nils

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
967df3deca
  1. 3
      app/controllers/locations_controller.rb
  2. 1
      app/controllers/schemes_controller.rb
  3. 2
      app/helpers/locations_helper.rb
  4. 13
      app/helpers/schemes_helper.rb
  5. 2
      app/models/location.rb
  6. 2
      app/models/organisation.rb
  7. 2
      app/models/scheme.rb
  8. 2
      app/views/locations/show.html.erb
  9. 2
      app/views/organisations/show.html.erb
  10. 3
      app/views/schemes/show.html.erb
  11. 6
      spec/helpers/locations_helper_spec.rb
  12. 4
      spec/helpers/schemes_helper_spec.rb
  13. 4
      spec/models/location_spec.rb
  14. 2
      spec/models/scheme_spec.rb
  15. 6
      spec/requests/locations_controller_spec.rb
  16. 6
      spec/requests/schemes_controller_spec.rb

3
app/controllers/locations_controller.rb

@ -29,7 +29,6 @@ class LocationsController < ApplicationController
deactivation_date_errors deactivation_date_errors
if @location.errors.present? if @location.errors.present?
@location.deactivation_date_type = params[:location][:deactivation_date_type] @location.deactivation_date_type = params[:location][:deactivation_date_type]
@location.deactivation_date = nil
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else else
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: }
@ -184,7 +183,7 @@ private
collection_start_date = FormHandler.instance.current_collection_start_date collection_start_date = FormHandler.instance.current_collection_start_date
if [day, month, year].any?(&:blank?) if [day, month, year].any?(&:blank?)
@location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s)) @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered"))
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, Date.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))

1
app/controllers/schemes_controller.rb

@ -30,7 +30,6 @@ class SchemesController < ApplicationController
deactivation_date_errors deactivation_date_errors
if @scheme.errors.present? if @scheme.errors.present?
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
@scheme.deactivation_date = nil
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else else
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: }

2
app/helpers/locations_helper.rb

@ -23,7 +23,7 @@ module LocationsHelper
resource.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } resource.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) }
end end
def display_attributes(location) def display_location_attributes(location)
[ [
{ name: "Postcode", value: location.postcode }, { name: "Postcode", value: location.postcode },
{ name: "Local authority", value: location.location_admin_district }, { name: "Local authority", value: location.location_admin_district },

13
app/helpers/schemes_helper.rb

@ -1,5 +1,5 @@
module SchemesHelper module SchemesHelper
def display_attributes(scheme) def display_scheme_attributes(scheme)
base_attributes = [ base_attributes = [
{ name: "Scheme code", value: scheme.id_to_display }, { name: "Scheme code", value: scheme.id_to_display },
{ name: "Name", value: scheme.service_name, edit: true }, { name: "Name", value: scheme.service_name, edit: true },
@ -31,15 +31,4 @@ module SchemesHelper
end end
base_text base_text
end end
def scheme_status(scheme)
now = Time.zone.now
if scheme.deactivation_date.nil?
"active"
elsif scheme.deactivation_date < now
"deactivated"
elsif now < scheme.deactivation_date
"deactivates_soon"
end
end
end end

2
app/models/location.rb

@ -374,7 +374,7 @@ class Location < ApplicationRecord
def status def status
return :active if deactivation_date.blank? return :active if deactivation_date.blank?
return :deactivating_soon if Time.zone.now < deactivation_date return :deactivates_soon if Time.zone.now < deactivation_date
return :deactivated if Time.zone.now >= deactivation_date return :deactivated if Time.zone.now >= deactivation_date
end end

2
app/models/organisation.rb

@ -77,7 +77,7 @@ class Organisation < ApplicationRecord
DISPLAY_PROVIDER_TYPE[provider_type.to_sym] DISPLAY_PROVIDER_TYPE[provider_type.to_sym]
end end
def display_attributes def display_organisation_attributes
[ [
{ name: "Name", value: name, editable: true }, { name: "Name", value: name, editable: true },
{ name: "Address", value: address_string, editable: true }, { name: "Address", value: address_string, editable: true },

2
app/models/scheme.rb

@ -216,7 +216,7 @@ class Scheme < ApplicationRecord
def status def status
return :active if deactivation_date.blank? return :active if deactivation_date.blank?
return :deactivating_soon if Time.zone.now < deactivation_date return :deactivates_soon if Time.zone.now < deactivation_date
return :deactivated if Time.zone.now >= deactivation_date return :deactivated if Time.zone.now >= deactivation_date
end end
end end

2
app/views/locations/show.html.erb

@ -13,7 +13,7 @@
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop"> <div class="govuk-grid-column-two-thirds-from-desktop">
<%= govuk_summary_list do |summary_list| %> <%= govuk_summary_list do |summary_list| %>
<% display_attributes(@location).each do |attr| %> <% display_location_attributes(@location).each do |attr| %>
<%= summary_list.row do |row| %> <%= summary_list.row do |row| %>
<% row.key { attr[:name] } %> <% row.key { attr[:name] } %>
<% row.value { attr[:name].eql?("Status") ? status_tag(attr[:value]) : details_html(attr) } %> <% row.value { attr[:name].eql?("Status") ? status_tag(attr[:value]) : details_html(attr) } %>

2
app/views/organisations/show.html.erb

@ -14,7 +14,7 @@
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop"> <div class="govuk-grid-column-two-thirds-from-desktop">
<%= govuk_summary_list do |summary_list| %> <%= govuk_summary_list do |summary_list| %>
<% @organisation.display_attributes.each do |attr| %> <% @organisation.display_organisation_attributes.each do |attr| %>
<% if can_edit_org?(current_user) && attr[:editable] %> <% if can_edit_org?(current_user) && attr[:editable] %>
<%= summary_list.row do |row| %> <%= summary_list.row do |row| %>
<% row.key { attr[:name].to_s.humanize } %> <% row.key { attr[:name].to_s.humanize } %>

3
app/views/schemes/show.html.erb

@ -15,11 +15,12 @@
<h2 class="govuk-visually-hidden">Scheme</h2> <h2 class="govuk-visually-hidden">Scheme</h2>
<%= govuk_summary_list do |summary_list| %> <%= govuk_summary_list do |summary_list| %>
<% display_attributes(@scheme).each do |attr| %> <% display_scheme_attributes(@scheme).each do |attr| %>
<% next if current_user.data_coordinator? && attr[:name] == ("Housing stock owned by") %> <% next if current_user.data_coordinator? && attr[:name] == ("Housing stock owned by") %>
<%= summary_list.row do |row| %> <%= summary_list.row do |row| %>
<% row.key { attr[:name].eql?("Registered under Care Standards Act 2000") ? "Registered under Care Standards Act 2000" : attr[:name].to_s.humanize } %> <% row.key { attr[:name].eql?("Registered under Care Standards Act 2000") ? "Registered under Care Standards Act 2000" : attr[:name].to_s.humanize } %>
<% row.value { details_html(attr) } %> <% row.value { details_html(attr) } %>
<% row.value { attr[:name].eql?("Status") ? status_tag(attr[:value]) : details_html(attr) } %>
<% row.action(text: "Change", href: scheme_edit_name_path(scheme_id: @scheme.id)) if attr[:edit] %> <% row.action(text: "Change", href: scheme_edit_name_path(scheme_id: @scheme.id)) if attr[:edit] %>
<% end %> <% end %>
<% end %> <% end %>

6
spec/helpers/locations_helper_spec.rb

@ -47,7 +47,7 @@ RSpec.describe LocationsHelper do
end end
end end
describe "display_attributes" do describe "display_location_attributes" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 8, 8)) } let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 8, 8)) }
it "returns correct display attributes" do it "returns correct display attributes" do
@ -63,12 +63,12 @@ RSpec.describe LocationsHelper do
{ name: "Status", value: :active }, { name: "Status", value: :active },
] ]
expect(display_attributes(location)).to eq(attributes) expect(display_location_attributes(location)).to eq(attributes)
end end
it "displays created_at as availability date if startdate is not present" do it "displays created_at as availability date if startdate is not present" do
location.update!(startdate: nil) location.update!(startdate: nil)
availability_attribute = display_attributes(location).find { |x| x[:name] == "Availability" }[:value] availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Available from #{location.created_at.to_formatted_s(:govuk_date)}") expect(availability_attribute).to eq("Available from #{location.created_at.to_formatted_s(:govuk_date)}")
end end

4
spec/helpers/schemes_helper_spec.rb

@ -1,7 +1,7 @@
require "rails_helper" require "rails_helper"
RSpec.describe SchemesHelper do RSpec.describe SchemesHelper do
describe "display_attributes" do describe "display_scheme_attributes" do
let!(:scheme) { FactoryBot.create(:scheme, created_at: Time.zone.local(2022, 8, 8)) } let!(:scheme) { FactoryBot.create(:scheme, created_at: Time.zone.local(2022, 8, 8)) }
it "returns correct display attributes" do it "returns correct display attributes" do
@ -21,7 +21,7 @@ RSpec.describe SchemesHelper do
{ name: "Availability", value: "Available from 8 August 2022" }, { name: "Availability", value: "Available from 8 August 2022" },
{ name: "Status", value: :active }, { name: "Status", value: :active },
] ]
expect(display_attributes(scheme)).to eq(attributes) expect(display_scheme_attributes(scheme)).to eq(attributes)
end end
end end
end end

4
spec/models/location_spec.rb

@ -125,10 +125,10 @@ RSpec.describe Location, type: :model do
expect(location.status).to eq(:active) expect(location.status).to eq(:active)
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivates soon if deactivation_date is in the future" do
location.deactivation_date = Time.zone.local(2022, 8, 8) location.deactivation_date = Time.zone.local(2022, 8, 8)
location.save! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivates_soon)
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do

2
spec/models/scheme_spec.rb

@ -108,7 +108,7 @@ RSpec.describe Scheme, type: :model do
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
scheme.deactivation_date = Time.zone.local(2022, 8, 8) scheme.deactivation_date = Time.zone.local(2022, 8, 8)
scheme.save! scheme.save!
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivates_soon)
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do

6
spec/requests/locations_controller_spec.rb

@ -1310,7 +1310,7 @@ RSpec.describe LocationsController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "day")) expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered"))
end end
end end
@ -1319,7 +1319,7 @@ RSpec.describe LocationsController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "month")) expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered"))
end end
end end
@ -1328,7 +1328,7 @@ RSpec.describe LocationsController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered", period: "year")) expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_entered"))
end end
end end
end end

6
spec/requests/schemes_controller_spec.rb

@ -1797,7 +1797,7 @@ RSpec.describe SchemesController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered", period: "day")) expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered"))
end end
end end
@ -1806,7 +1806,7 @@ RSpec.describe SchemesController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered", period: "month")) expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered"))
end end
end end
@ -1815,7 +1815,7 @@ RSpec.describe SchemesController, type: :request do
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)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered", period: "year")) expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.not_entered"))
end end
end end
end end

Loading…
Cancel
Save