Browse Source

Merge branch 'main' into CLDC-1669-add-location-redesign

# Conflicts:
#	app/helpers/locations_helper.rb
#	config/locales/en.yml
pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
0434653620
  1. 6
      app/controllers/cookies_controller.rb
  2. 4
      app/controllers/locations_controller.rb
  3. 16
      app/controllers/organisation_relationships_controller.rb
  4. 47
      app/controllers/schemes_controller.rb
  5. 1
      app/controllers/users_controller.rb
  6. 7
      app/frontend/application.js
  7. 57
      app/frontend/cookie-banner.js
  8. 28
      app/helpers/locations_helper.rb
  9. 42
      app/helpers/schemes_helper.rb
  10. 2
      app/helpers/tag_helper.rb
  11. 17
      app/helpers/toggle_active_scheme_helper.rb
  12. 1
      app/models/location.rb
  13. 5
      app/models/organisation.rb
  14. 12
      app/models/organisation_relationship.rb
  15. 19
      app/models/scheme.rb
  16. 35
      app/models/scheme_deactivation_period.rb
  17. 48
      app/views/cookies/_banner.html.erb
  18. 24
      app/views/layouts/application.html.erb
  19. 22
      app/views/schemes/_scheme_list.html.erb
  20. 4
      app/views/schemes/deactivate_confirm.html.erb
  21. 4
      app/views/schemes/show.html.erb
  22. 18
      app/views/schemes/toggle_active.html.erb
  23. 18
      config/locales/en.yml
  24. 2
      config/routes.rb
  25. 5
      db/migrate/20221122122311_delete_relationship_type.rb
  26. 10
      db/migrate/20221122130928_add_org_relation_indexes.rb
  27. 14
      db/schema.rb
  28. 12
      db/seeds.rb
  29. 8
      spec/factories/organisation_relationship.rb
  30. 1
      spec/factories/scheme_deactivation_period.rb
  31. 48
      spec/helpers/locations_helper_spec.rb
  32. 182
      spec/helpers/schemes_helper_spec.rb
  33. 7
      spec/models/form/lettings/pages/housing_provider_spec.rb
  34. 10
      spec/models/form/lettings/pages/managing_organisation_spec.rb
  35. 8
      spec/models/form/lettings/questions/housing_provider_spec.rb
  36. 16
      spec/models/form/lettings/questions/managing_organisation_spec.rb
  37. 14
      spec/models/location_spec.rb
  38. 25
      spec/models/organisation_relationship_spec.rb
  39. 20
      spec/models/organisation_spec.rb
  40. 20
      spec/models/scheme_spec.rb
  41. 38
      spec/requests/organisation_relationships_controller_spec.rb
  42. 17
      spec/requests/schemes_controller_spec.rb
  43. 57
      spec/views/layouts/application_layout_spec.rb

6
app/controllers/cookies_controller.rb

@ -18,6 +18,12 @@ class CookiesController < ApplicationController
redirect_to cookies_path
end
format.json do
render json: {
status: "ok",
message: %(You’ve #{analytics_consent == 'on' ? 'accepted' : 'rejected'} analytics cookies.),
}
end
end
end

4
app/controllers/locations_controller.rb

@ -78,12 +78,12 @@ class LocationsController < ApplicationController
end
def new_reactivation
@location_deactivation_period = LocationDeactivationPeriod.deactivations_without_reactivation.first
@location_deactivation_period = @location.location_deactivation_periods.deactivations_without_reactivation.first
render "toggle_active", locals: { action: "reactivate" }
end
def reactivate
@location_deactivation_period = LocationDeactivationPeriod.deactivations_without_reactivation.first
@location_deactivation_period = @location.location_deactivation_periods.deactivations_without_reactivation.first
@location_deactivation_period.reactivation_date = toggle_date("reactivation_date")
@location_deactivation_period.reactivation_date_type = params[:location_deactivation_period][:reactivation_date_type]

16
app/controllers/organisation_relationships_controller.rb

@ -39,7 +39,6 @@ class OrganisationRelationshipsController < ApplicationController
def create_housing_provider
child_organisation = @organisation
relationship_type = OrganisationRelationship::OWNING
if params[:organisation][:related_organisation_id].empty?
@organisation.errors.add :related_organisation_id, "You must choose a housing provider"
@organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name)
@ -47,21 +46,20 @@ class OrganisationRelationshipsController < ApplicationController
return
else
parent_organisation = related_organisation
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:, relationship_type:)
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:)
@organisation.errors.add :related_organisation_id, "You have already added this housing provider"
@organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name)
render "organisation_relationships/add_housing_provider"
return
end
end
create!(child_organisation:, parent_organisation:, relationship_type:)
create!(child_organisation:, parent_organisation:)
flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers"
redirect_to housing_providers_organisation_path
end
def create_managing_agent
parent_organisation = @organisation
relationship_type = OrganisationRelationship::MANAGING
if params[:organisation][:related_organisation_id].empty?
@organisation.errors.add :related_organisation_id, "You must choose a managing agent"
@organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name)
@ -69,14 +67,14 @@ class OrganisationRelationshipsController < ApplicationController
return
else
child_organisation = related_organisation
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:, relationship_type:)
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:)
@organisation.errors.add :related_organisation_id, "You have already added this managing agent"
@organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name)
render "organisation_relationships/add_managing_agent"
return
end
end
create!(child_organisation:, parent_organisation:, relationship_type:)
create!(child_organisation:, parent_organisation:)
flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents"
redirect_to managing_agents_organisation_path
end
@ -89,7 +87,6 @@ class OrganisationRelationshipsController < ApplicationController
relationship = OrganisationRelationship.find_by!(
child_organisation: @organisation,
parent_organisation: target_organisation,
relationship_type: OrganisationRelationship::OWNING,
)
relationship.destroy!
flash[:notice] = "#{target_organisation.name} is no longer one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers"
@ -104,7 +101,6 @@ class OrganisationRelationshipsController < ApplicationController
relationship = OrganisationRelationship.find_by!(
parent_organisation: @organisation,
child_organisation: target_organisation,
relationship_type: OrganisationRelationship::MANAGING,
)
relationship.destroy!
flash[:notice] = "#{target_organisation.name} is no longer one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents"
@ -113,8 +109,8 @@ class OrganisationRelationshipsController < ApplicationController
private
def create!(child_organisation:, parent_organisation:, relationship_type:)
@resource = OrganisationRelationship.new(child_organisation:, parent_organisation:, relationship_type:)
def create!(child_organisation:, parent_organisation:)
@resource = OrganisationRelationship.new(child_organisation:, parent_organisation:)
@resource.save!
end

47
app/controllers/schemes_controller.rb

@ -27,10 +27,10 @@ class SchemesController < ApplicationController
if params[:scheme_deactivation_period].blank?
render "toggle_active", locals: { action: "deactivate" }
else
@scheme_deactivation_period.deactivation_date = deactivation_date
@scheme_deactivation_period.deactivation_date = toggle_date("deactivation_date")
@scheme_deactivation_period.deactivation_date_type = params[:scheme_deactivation_period][:deactivation_date_type]
@scheme_deactivation_period.scheme = @scheme
if @scheme_deactivation_period.validate
if @scheme_deactivation_period.valid?
redirect_to scheme_deactivate_confirm_path(@scheme, deactivation_date: @scheme_deactivation_period.deactivation_date, deactivation_date_type: @scheme_deactivation_period.deactivation_date_type)
else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
@ -50,10 +50,25 @@ class SchemesController < ApplicationController
redirect_to scheme_details_path(@scheme)
end
def reactivate
def new_reactivation
@scheme_deactivation_period = @scheme.scheme_deactivation_periods.deactivations_without_reactivation.first
render "toggle_active", locals: { action: "reactivate" }
end
def reactivate
@scheme_deactivation_period = @scheme.scheme_deactivation_periods.deactivations_without_reactivation.first
@scheme_deactivation_period.reactivation_date = toggle_date("reactivation_date")
@scheme_deactivation_period.reactivation_date_type = params[:scheme_deactivation_period][:reactivation_date_type]
if @scheme_deactivation_period.update(reactivation_date: toggle_date("reactivation_date"))
flash[:notice] = reactivate_success_notice
redirect_to scheme_details_path(@scheme)
else
render "toggle_active", locals: { action: "reactivate" }, status: :unprocessable_entity
end
end
def new
@scheme = Scheme.new
end
@ -239,8 +254,7 @@ private
:support_type,
:arrangement_type,
:intended_stay,
:confirmed,
:deactivation_date)
:confirmed)
if arrangement_type_changed_to_different_org?(required_params)
required_params[:managing_organisation_id] = nil
@ -303,18 +317,27 @@ private
end
end
def deactivation_date
def reactivate_success_notice
case @scheme.status
when :active
"#{@scheme.service_name} has been reactivated"
when :reactivating_soon
"#{@scheme.service_name} will reactivate on #{toggle_date('reactivation_date').to_time.to_formatted_s(:govuk_date)}"
end
end
def toggle_date(key)
if params[:scheme_deactivation_period].blank?
return
elsif params[:scheme_deactivation_period][:deactivation_date_type] == "default"
elsif params[:scheme_deactivation_period]["#{key}_type".to_sym] == "default"
return FormHandler.instance.current_collection_start_date
elsif params[:scheme_deactivation_period][:deactivation_date].present?
return params[:scheme_deactivation_period][:deactivation_date]
elsif params[:scheme_deactivation_period][key.to_sym].present?
return params[:scheme_deactivation_period][key.to_sym]
end
day = params[:scheme_deactivation_period]["deactivation_date(3i)"]
month = params[:scheme_deactivation_period]["deactivation_date(2i)"]
year = params[:scheme_deactivation_period]["deactivation_date(1i)"]
day = params[:scheme_deactivation_period]["#{key}(3i)"]
month = params[:scheme_deactivation_period]["#{key}(2i)"]
year = params[:scheme_deactivation_period]["#{key}(1i)"]
return nil if [day, month, year].any?(&:blank?)
Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)

1
app/controllers/users_controller.rb

@ -3,6 +3,7 @@ class UsersController < ApplicationController
include Devise::Controllers::SignInOut
include Helpers::Email
include Modules::SearchFilter
before_action :authenticate_user!
before_action :find_resource, except: %i[new create]
before_action :authenticate_scope!, except: %i[new]

7
app/frontend/application.js

@ -4,18 +4,19 @@
// files to reference that code so it'll be compiled.
// Polyfills for IE
import '@stimulus/polyfills'
import '@webcomponents/webcomponentsjs'
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import '@stimulus/polyfills'
import 'custom-event-polyfill'
import 'intersection-observer'
import 'regenerator-runtime/runtime'
//
import { initAll as GOVUKFrontend } from 'govuk-frontend'
import { initAll as GOVUKPrototypeComponents } from 'govuk-prototype-components'
import './styles/application.scss'
import './controllers'
import './cookie-banner'
import './styles/application.scss'
require.context('govuk-frontend/govuk/assets')

57
app/frontend/cookie-banner.js

@ -0,0 +1,57 @@
const cookieBannerEl = document.querySelector('.js-cookie-banner')
if (cookieBannerEl) {
const cookieFormEl = document.querySelector('.js-cookie-form')
cookieFormEl.addEventListener('click', (e) => {
if (e.target.tagName !== 'BUTTON') {
return
}
const body = new window.FormData(cookieFormEl)
body.append('cookies_form[accept_analytics_cookies]', e.target.value)
fetch(cookieFormEl.action, {
method: 'PUT',
headers: {
Accept: 'application/json'
},
body
})
.then((res) => {
if (res.status >= 200 && res.status < 300) {
return res
}
throw new Error(res)
})
.then((res) => res.json())
.then(({ message }) => {
const messageEl = cookieBannerEl.querySelector('.js-cookie-message')
messageEl.textContent = message
cookieBannerEl
.querySelector('.js-cookie-banner__form')
.setAttribute('hidden', '')
cookieBannerEl
.querySelector('.js-cookie-banner__success')
.removeAttribute('hidden')
})
const gaSrc = window.analyticsScript
if (e.target.value === 'on' && gaSrc) {
const scriptEl = document.createElement('script')
scriptEl.src = gaSrc
document.body.appendChild(scriptEl)
}
e.preventDefault()
})
const hideBannerEl = document.querySelector('.js-hide-cookie-banner')
hideBannerEl.addEventListener('click', (e) => {
e.preventDefault()
cookieBannerEl.setAttribute('hidden', '')
})
}

28
app/helpers/locations_helper.rb

@ -42,22 +42,9 @@ module LocationsHelper
base_attributes
end
ActivePeriod = Struct.new(:from, :to)
def active_periods(location)
periods = [ActivePeriod.new(location.available_from, nil)]
sorted_deactivation_periods = remove_nested_periods(location.location_deactivation_periods.sort_by(&:deactivation_date))
sorted_deactivation_periods.each do |deactivation|
periods.last.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end
remove_overlapping_and_empty_periods(periods)
end
def location_availability(location)
availability = ""
active_periods(location).each do |period|
location_active_periods(location).each do |period|
if period.from.present?
availability << "\nActive from #{period.from.to_formatted_s(:govuk_date)}"
availability << " to #{(period.to - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{period.to.to_formatted_s(:govuk_date)}" if period.to.present?
@ -87,6 +74,19 @@ module LocationsHelper
private
ActivePeriod = Struct.new(:from, :to)
def location_active_periods(location)
periods = [ActivePeriod.new(location.available_from, nil)]
sorted_deactivation_periods = remove_nested_periods(location.location_deactivation_periods.sort_by(&:deactivation_date))
sorted_deactivation_periods.each do |deactivation|
periods.last.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end
remove_overlapping_and_empty_periods(periods)
end
def remove_overlapping_and_empty_periods(periods)
periods.select { |period| period.from.present? && (period.to.nil? || period.from < period.to) }
end

42
app/helpers/schemes_helper.rb

@ -28,11 +28,43 @@ module SchemesHelper
end
def scheme_availability(scheme)
availability = "Active from #{scheme.available_from.to_formatted_s(:govuk_date)}"
scheme.scheme_deactivation_periods.each do |deactivation|
availability << " to #{(deactivation.deactivation_date - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{deactivation.deactivation_date.to_formatted_s(:govuk_date)}"
availability << "\nActive from #{deactivation.reactivation_date.to_formatted_s(:govuk_date)}" if deactivation.reactivation_date.present?
availability = ""
scheme_active_periods(scheme).each do |period|
if period.from.present?
availability << "\nActive from #{period.from.to_formatted_s(:govuk_date)}"
availability << " to #{(period.to - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{period.to.to_formatted_s(:govuk_date)}" if period.to.present?
end
end
availability
availability.strip
end
private
ActivePeriod = Struct.new(:from, :to)
def scheme_active_periods(scheme)
periods = [ActivePeriod.new(scheme.available_from, nil)]
sorted_deactivation_periods = remove_nested_periods(scheme.scheme_deactivation_periods.sort_by(&:deactivation_date))
sorted_deactivation_periods.each do |deactivation|
periods.last.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end
remove_overlapping_and_empty_periods(periods)
end
def remove_overlapping_and_empty_periods(periods)
periods.select { |period| period.from.present? && (period.to.nil? || period.from < period.to) }
end
def remove_nested_periods(periods)
periods.select { |inner_period| periods.none? { |outer_period| is_nested?(inner_period, outer_period) } }
end
def is_nested?(inner, outer)
return false if inner == outer
return false if [inner.deactivation_date, inner.reactivation_date, outer.deactivation_date, outer.reactivation_date].any?(&:blank?)
[inner.deactivation_date, inner.reactivation_date].all? { |date| date.between?(outer.deactivation_date, outer.reactivation_date) }
end
end

2
app/helpers/tag_helper.rb

@ -9,6 +9,7 @@ module TagHelper
active: "Active",
incomplete: "Incomplete",
deactivating_soon: "Deactivating soon",
activating_soon: "Activating soon",
reactivating_soon: "Reactivating soon",
deactivated: "Deactivated",
}.freeze
@ -21,6 +22,7 @@ module TagHelper
active: "green",
incomplete: "red",
deactivating_soon: "yellow",
activating_soon: "blue",
reactivating_soon: "blue",
deactivated: "grey",
}.freeze

17
app/helpers/toggle_active_scheme_helper.rb

@ -0,0 +1,17 @@
module ToggleActiveSchemeHelper
def toggle_scheme_form_path(action, scheme)
if action == "deactivate"
scheme_new_deactivation_path(scheme)
else
scheme_reactivate_path(scheme)
end
end
def date_type_question(action)
action == "deactivate" ? :deactivation_date_type : :reactivation_date_type
end
def date_question(action)
action == "deactivate" ? :deactivation_date : :reactivation_date
end
end

1
app/models/location.rb

@ -380,6 +380,7 @@ class Location < ApplicationRecord
return :deactivated if open_deactivation&.deactivation_date.present? && Time.zone.now >= open_deactivation.deactivation_date
return :deactivating_soon if open_deactivation&.deactivation_date.present? && Time.zone.now < open_deactivation.deactivation_date
return :reactivating_soon if recent_deactivation&.reactivation_date.present? && Time.zone.now < recent_deactivation.reactivation_date
return :activating_soon if startdate.present? && Time.zone.now < startdate
:active
end

5
app/models/organisation.rb

@ -13,9 +13,10 @@ class Organisation < ApplicationRecord
has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :child_organisations, through: :child_organisation_relationships
has_many :housing_provider_relationships, -> { where(relationship_type: OrganisationRelationship::OWNING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :housing_provider_relationships, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :housing_providers, through: :housing_provider_relationships, source: :parent_organisation
has_many :managing_agent_relationships, -> { where(relationship_type: OrganisationRelationship::MANAGING) }, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agent_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agents, through: :managing_agent_relationships, source: :child_organisation
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }

12
app/models/organisation_relationship.rb

@ -1,16 +1,4 @@
class OrganisationRelationship < ApplicationRecord
belongs_to :child_organisation, class_name: "Organisation"
belongs_to :parent_organisation, class_name: "Organisation"
scope :owning, -> { where(relationship_type: OWNING) }
scope :managing, -> { where(relationship_type: MANAGING) }
OWNING = "owning".freeze
MANAGING = "managing".freeze
RELATIONSHIP_TYPE = {
OWNING => 0,
MANAGING => 1,
}.freeze
enum relationship_type: RELATIONSHIP_TYPE
end

19
app/models/scheme.rb

@ -210,18 +210,27 @@ class Scheme < ApplicationRecord
end
def available_from
created_at
[created_at, FormHandler.instance.current_collection_start_date].min
end
def status
recent_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first
return :active if recent_deactivation.blank?
return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date
return :incomplete unless confirmed
:deactivated
open_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first
recent_deactivation = scheme_deactivation_periods.order("created_at").last
return :deactivated if open_deactivation&.deactivation_date.present? && Time.zone.now >= open_deactivation.deactivation_date
return :deactivating_soon if open_deactivation&.deactivation_date.present? && Time.zone.now < open_deactivation.deactivation_date
return :reactivating_soon if recent_deactivation&.reactivation_date.present? && Time.zone.now < recent_deactivation.reactivation_date
:active
end
def active?
status == :active
end
def reactivating_soon?
status == :reactivating_soon
end
end

35
app/models/scheme_deactivation_period.rb

@ -1,15 +1,40 @@
class SchemeDeactivationPeriodValidator < ActiveModel::Validator
def validate(record)
scheme = record.scheme
recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first
if recent_deactivation.present?
validate_reactivation(record, recent_deactivation, scheme)
else
validate_deactivation(record, scheme)
end
end
def validate_reactivation(record, recent_deactivation, scheme)
if record.reactivation_date.blank?
if record.reactivation_date_type.blank?
record.errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected"))
elsif record.reactivation_date_type == "other"
record.errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid"))
end
elsif !record.reactivation_date.between?(scheme.available_from, Time.zone.local(2200, 1, 1))
record.errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: scheme.available_from.to_formatted_s(:govuk_date)))
elsif record.reactivation_date < recent_deactivation.deactivation_date
record.errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation.before_deactivation", date: recent_deactivation.deactivation_date.to_formatted_s(:govuk_date)))
end
end
def validate_deactivation(record, scheme)
if record.deactivation_date.blank?
if record.deactivation_date_type.blank?
record.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
record.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected"))
elsif record.deactivation_date_type == "other"
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid"))
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid"))
end
elsif scheme.scheme_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) }
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation.during_deactivated_period"))
else
collection_start_date = FormHandler.instance.current_collection_start_date
unless record.deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
unless record.deactivation_date.between?(scheme.available_from, Time.zone.local(2200, 1, 1))
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: scheme.available_from.to_formatted_s(:govuk_date)))
end
end
end

48
app/views/cookies/_banner.html.erb

@ -0,0 +1,48 @@
<div class="govuk-cookie-banner js-cookie-banner" role="region" aria-label="Cookies on CORE">
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__form">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h2 class="govuk-cookie-banner__heading govuk-heading-m">Cookies on CORE</h2>
<div class="govuk-cookie-banner__content">
<p class="govuk-body">We use some essential cookies to make this service work.</p>
<p class="govuk-body">We’d like to use analytics cookies so we can understand how you use the service and make improvements.</p>
</div>
</div>
</div>
<%= form_with url: cookies_path, class: "js-cookie-form", method: :put do |f| %>
<div class="govuk-button-group">
<button type="submit" class="govuk-button" name="cookies_form[analytics_consent]" value="on">
Accept analytics cookies
</button>
<button type="submit" class="govuk-button" name="cookies_form[analytics_consent]" value="off">
Reject analytics cookies
</button>
<%= govuk_link_to "View cookies", cookies_path %>
</div>
<% end %>
</div>
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__success" role="alert" hidden>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="govuk-cookie-banner__content">
<p class="govuk-body">
<span class="js-cookie-message">
You’ve set your cookie preferences.
</span>
You can <%= govuk_link_to "change your cookie settings", cookies_path %> at any time.
</p>
</div>
</div>
</div>
<div class="govuk-button-group">
<button class="govuk-button js-hide-cookie-banner">
Hide this message
</button>
</div>
</div>
</div>

24
app/views/layouts/application.html.erb

@ -25,15 +25,15 @@
<% gtm_container = get_gtm_container %>
<% gtm_id = get_gtm_id %>
<!-- Google Tag Manager (doesn't store personal info until permission given) -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<%= gtm_container %>');</script>
<!-- End Google Tag Manager -->
<% if cookies[:accept_analytics_cookies] == "on" %>
<!-- Google Tag Manager (doesn't store personal info until permission given) -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<%= gtm_container %>');</script>
<!-- End Google Tag Manager -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= gtm_id %>"></script>
<% else %>
<script>
@ -72,13 +72,17 @@
document.body.className = ((document.body.className) ? document.body.className + " js-enabled" : "js-enabled");
</script>
<!-- Google Tag Manager (noscript) -->
<% if cookies[:accept_analytics_cookies] %>
<% if cookies[:accept_analytics_cookies] == "on" %>
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=<% gtm_container %>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<% end %>
<% unless cookies[:accept_analytics_cookies] || current_page?(cookies_path) %>
<%= render "cookies/banner" %>
<% end %>
<%= govuk_skip_link %>
<%= govuk_header(

22
app/views/schemes/_scheme_list.html.erb

@ -5,31 +5,31 @@
<% end %>
<%= table.head do |head| %>
<%= head.row do |row| %>
<% row.cell(header: true, text: "Code", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Scheme", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Locations", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Support provided by", html_attributes: {
<% row.cell(header: true, text: "Code", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Created", html_attributes: {
<% row.cell(header: true, text: "Locations", html_attributes: {
scope: "col",
}) %>
<% if FeatureToggle.scheme_toggle_enabled? %>
<% row.cell(header: true, text: "Status", html_attributes: {
scope: "col",
}) %>
<% end %>
<% end %>
<% end %>
<% @schemes.each do |scheme| %>
<%= table.body do |body| %>
<%= body.row do |row| %>
<% row.cell(text: scheme.id_to_display) %>
<% row.cell(text: simple_format(scheme_cell(scheme), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
<% row.cell(text: scheme.id_to_display) %>
<% row.cell(text: scheme.locations&.count) %>
<% row.cell(text: scheme.managing_organisation&.name) %>
<% row.cell(text: scheme.confirmed? ? scheme.created_at.to_formatted_s(:govuk_date) : govuk_tag(colour: "grey", text: "Incomplete")) %>
<% if FeatureToggle.scheme_toggle_enabled? %>
<% row.cell(text: status_tag(scheme.status)) %>
<% end %>
<% end %>
<% end %>
<% end %>

4
app/views/schemes/deactivate_confirm.html.erb

@ -1,5 +1,3 @@
<% title = "Deactivate #{@scheme.service_name}" %>
<% content_for :title, title %>
<%= form_with model: @scheme_deactivation_period, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
<% content_for :before_content do %>
<%= govuk_back_link(href: :back) %>
@ -8,7 +6,7 @@
<span class="govuk-caption-l"><%= @scheme.service_name %></span>
This change will affect <%= @scheme.lettings_logs.count %> logs
</h1>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivate.review_logs") %>
<%= f.hidden_field :confirm, value: true %>
<%= f.hidden_field :deactivation_date, value: @deactivation_date %>
<%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>

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

@ -26,9 +26,9 @@
<% end %>
<% if FeatureToggle.scheme_toggle_enabled? %>
<% if @scheme.active? %>
<% if @scheme.active? || @scheme.reactivating_soon? %>
<%= govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(@scheme), warning: true %>
<% else %>
<%= govuk_button_link_to "Reactivate this scheme", scheme_reactivate_path(@scheme) %>
<%= govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(@scheme) %>
<% end %>
<% end %>

18
app/views/schemes/toggle_active.html.erb

@ -1,29 +1,31 @@
<% title = "#{action.humanize} #{@scheme.service_name}" %>
<% content_for :title, title %>
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: scheme_details_path(@scheme),
) %>
<% end %>
<%= form_with model: @scheme_deactivation_period, url: scheme_new_deactivation_path(@scheme), method: "patch", local: true do |f| %>
<%= form_with model: @scheme_deactivation_period, url: toggle_scheme_form_path(action, @scheme), method: "patch", local: true do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %>
<%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date_type,
legend: { text: I18n.t("questions.scheme.deactivation.apply_from") },
<%= f.govuk_radio_buttons_fieldset date_type_question(action),
legend: { text: I18n.t("questions.scheme.toggle_active.apply_from") },
caption: { text: title },
hint: { text: I18n.t("hints.scheme.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } do %>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.existing_logs") %>
<%= f.govuk_radio_button :deactivation_date_type,
hint: { text: I18n.t("hints.scheme.toggle_active", date: collection_start_date.to_formatted_s(:govuk_date)) } do %>
<%= govuk_warning_text text: I18n.t("warnings.scheme.#{action}.existing_logs") %>
<%= f.govuk_radio_button date_type_question(action),
"default",
label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %>
<%= f.govuk_radio_button :deactivation_date_type,
<%= f.govuk_radio_button date_type_question(action),
"other",
label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "scheme") do %>
<%= f.govuk_date_field :deactivation_date,
<%= f.govuk_date_field date_question(action),
legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2022" },
width: 20 %>

18
config/locales/en.yml

@ -313,10 +313,16 @@ en:
missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log."
scheme:
deactivation_date:
toggle_date:
not_selected: "Select one of the options"
invalid: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
reactivation:
before_deactivation: "This scheme was deactivated on %{date}. The reactivation date must be on or after deactivation date"
deactivation:
during_deactivated_period: "The scheme is already deactivated during this date, please enter a different date"
location:
toggle_date:
@ -324,7 +330,7 @@ en:
invalid: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
reactivation:
before_deactivation: "This location was deactivated on %{date}\nThe reactivation date must be on or after deactivation date"
before_deactivation: "This location was deactivated on %{date}. The reactivation date must be on or after deactivation date"
deactivation:
during_deactivated_period: "The location is already deactivated during this date, please enter a different date"
@ -382,7 +388,7 @@ en:
toggle_active:
apply_from: "When should this change apply?"
scheme:
deactivation:
toggle_active:
apply_from: "When should this change apply?"
descriptions:
location:
@ -399,7 +405,7 @@ en:
toggle_active: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
startdate: "For example, 27 3 2021"
scheme:
deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
toggle_active: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
warnings:
location:
@ -409,9 +415,11 @@ en:
reactivate:
existing_logs: "You’ll be able to add logs with this location if their tenancy start date is on or after the date you enter."
scheme:
deactivation:
deactivate:
existing_logs: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected."
review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating."
reactivate:
existing_logs: "You’ll be able to add logs with this scheme if their tenancy start date is on or after the date you enter."
test:
one_argument: "This is based on the tenant’s work situation: %{ecstat1}"

2
config/routes.rb

@ -52,8 +52,10 @@ Rails.application.routes.draw do
get "new-deactivation", to: "schemes#new_deactivation"
get "deactivate-confirm", to: "schemes#deactivate_confirm"
get "reactivate", to: "schemes#reactivate"
get "new-reactivation", to: "schemes#new_reactivation"
patch "new-deactivation", to: "schemes#new_deactivation"
patch "deactivate", to: "schemes#deactivate"
patch "reactivate", to: "schemes#reactivate"
resources :locations do
get "edit-name", to: "locations#edit_name"

5
db/migrate/20221122122311_delete_relationship_type.rb

@ -0,0 +1,5 @@
class DeleteRelationshipType < ActiveRecord::Migration[7.0]
def change
remove_column :organisation_relationships, :relationship_type, :integer, null: false
end
end

10
db/migrate/20221122130928_add_org_relation_indexes.rb

@ -0,0 +1,10 @@
class AddOrgRelationIndexes < ActiveRecord::Migration[7.0]
def change
add_index :organisation_relationships, :child_organisation_id
add_index :organisation_relationships, :parent_organisation_id
add_index :organisation_relationships, %i[parent_organisation_id child_organisation_id], unique: true, name: "index_org_rel_parent_child_uniq"
add_foreign_key :organisation_relationships, :organisations, column: :parent_organisation_id
add_foreign_key :organisation_relationships, :organisations, column: :child_organisation_id
end
end

14
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
ActiveRecord::Schema[7.0].define(version: 2022_11_22_130928) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -286,7 +286,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
t.integer "parent_organisation_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "relationship_type", null: false
t.index ["child_organisation_id"], name: "index_organisation_relationships_on_child_organisation_id"
t.index ["parent_organisation_id", "child_organisation_id"], name: "index_org_rel_parent_child_uniq", unique: true
t.index ["parent_organisation_id"], name: "index_organisation_relationships_on_parent_organisation_id"
end
create_table "organisation_rent_periods", force: :cascade do |t|
@ -372,15 +374,15 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
t.integer "la_known"
t.integer "income1"
t.integer "income1nk"
t.integer "details_known_2"
t.integer "details_known_3"
t.integer "details_known_4"
t.integer "age4"
t.integer "age4_known"
t.integer "age5"
t.integer "age5_known"
t.integer "age6"
t.integer "age6_known"
t.integer "details_known_2"
t.integer "details_known_3"
t.integer "details_known_4"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"
@ -477,6 +479,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "lettings_logs", "schemes"
add_foreign_key "locations", "schemes"
add_foreign_key "organisation_relationships", "organisations", column: "child_organisation_id"
add_foreign_key "organisation_relationships", "organisations", column: "parent_organisation_id"
add_foreign_key "sales_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "schemes", "organisations", column: "managing_organisation_id"
add_foreign_key "schemes", "organisations", column: "owning_organisation_id", on_delete: :cascade

12
db/seeds.rb

@ -67,25 +67,21 @@ unless Rails.env.test?
end
end
OrganisationRelationship.create!(
OrganisationRelationship.find_or_create_by!(
child_organisation: org,
parent_organisation: housing_provider1,
relationship_type: OrganisationRelationship::OWNING,
)
OrganisationRelationship.create!(
OrganisationRelationship.find_or_create_by!(
child_organisation: org,
parent_organisation: housing_provider2,
relationship_type: OrganisationRelationship::OWNING,
)
OrganisationRelationship.create!(
OrganisationRelationship.find_or_create_by!(
child_organisation: managing_agent1,
parent_organisation: org,
relationship_type: OrganisationRelationship::MANAGING,
)
OrganisationRelationship.create!(
OrganisationRelationship.find_or_create_by!(
child_organisation: managing_agent2,
parent_organisation: org,
relationship_type: OrganisationRelationship::MANAGING,
)
if (Rails.env.development? || Rails.env.review?) && User.count.zero?

8
spec/factories/organisation_relationship.rb

@ -2,13 +2,5 @@ FactoryBot.define do
factory :organisation_relationship do
child_organisation { FactoryBot.create(:organisation) }
parent_organisation { FactoryBot.create(:organisation) }
trait :owning do
relationship_type { OrganisationRelationship::OWNING }
end
trait :managing do
relationship_type { OrganisationRelationship::MANAGING }
end
end
end

1
spec/factories/scheme_deactivation_period.rb

@ -1,5 +1,6 @@
FactoryBot.define do
factory :scheme_deactivation_period do
deactivation_date { Time.zone.local(2022, 4, 1) }
reactivation_date { nil }
end
end

48
spec/helpers/locations_helper_spec.rb

@ -59,8 +59,8 @@ RSpec.describe LocationsHelper do
end
it "returns one active period without to date" do
expect(active_periods(location).count).to eq(1)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil)
expect(location_active_periods(location).count).to eq(1)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil)
end
it "ignores reactivations that were deactivated on the same day" do
@ -68,8 +68,8 @@ RSpec.describe LocationsHelper do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:)
location.reload
expect(active_periods(location).count).to eq(1)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(location_active_periods(location).count).to eq(1)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
end
it "returns sequential non reactivated active periods" do
@ -77,19 +77,19 @@ RSpec.describe LocationsHelper do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), location:)
location.reload
expect(active_periods(location).count).to eq(2)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(location_active_periods(location).count).to eq(2)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
end
it "returns sequential reactivated active periods" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), location:)
location.reload
expect(active_periods(location).count).to eq(3)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
expect(location_active_periods(location).count).to eq(3)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(location_active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns non sequential non reactivated active periods" do
@ -97,19 +97,19 @@ RSpec.describe LocationsHelper do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: nil, location:)
location.reload
expect(active_periods(location).count).to eq(2)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
expect(location_active_periods(location).count).to eq(2)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns non sequential reactivated active periods" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:)
location.reload
expect(active_periods(location).count).to eq(3)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
expect(location_active_periods(location).count).to eq(3)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(location_active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns correct active periods when reactivation happends during a deactivated period" do
@ -117,9 +117,9 @@ RSpec.describe LocationsHelper do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), location:)
location.reload
expect(active_periods(location).count).to eq(2)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil)
expect(location_active_periods(location).count).to eq(2)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil)
end
it "returns correct active periods when a full deactivation period happens during another deactivation period" do
@ -127,9 +127,9 @@ RSpec.describe LocationsHelper do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), location:)
location.reload
expect(active_periods(location).count).to eq(2)
expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 7, 7), to: nil)
expect(location_active_periods(location).count).to eq(2)
expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 7, 7), to: nil)
end
end

182
spec/helpers/schemes_helper_spec.rb

@ -1,8 +1,94 @@
require "rails_helper"
RSpec.describe SchemesHelper do
describe "Active periods" do
let(:scheme) { FactoryBot.create(:scheme) }
before do
Timecop.freeze(2022, 10, 10)
end
after do
Timecop.unfreeze
end
it "returns one active period without to date" do
expect(scheme_active_periods(scheme).count).to eq(1)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil)
end
it "ignores reactivations that were deactivated on the same day" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(1)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
end
it "returns sequential non reactivated active periods" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(2)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
end
it "returns sequential reactivated active periods" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(3)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(scheme_active_periods(scheme).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns non sequential non reactivated active periods" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: nil, scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(2)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns non sequential reactivated active periods" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(3)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6))
expect(scheme_active_periods(scheme).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil)
end
it "returns correct active periods when reactivation happends during a deactivated period" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 11, 11), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(2)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil)
end
it "returns correct active periods when a full deactivation period happens during another deactivation period" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 11), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), scheme:)
scheme.reload
expect(scheme_active_periods(scheme).count).to eq(2)
expect(scheme_active_periods(scheme).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6))
expect(scheme_active_periods(scheme).second).to have_attributes(from: Time.zone.local(2022, 7, 7), to: nil)
end
end
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, 4, 1)) }
it "returns correct display attributes" do
attributes = [
@ -18,32 +104,116 @@ RSpec.describe SchemesHelper do
{ name: "Secondary client group", value: scheme.secondary_client_group },
{ name: "Level of support given", value: scheme.support_type },
{ name: "Intended length of stay", value: scheme.intended_stay },
{ name: "Availability", value: "Active from 8 August 2022" },
{ name: "Availability", value: "Active from 1 April 2022" },
{ name: "Status", value: :active },
]
expect(display_scheme_attributes(scheme)).to eq(attributes)
end
context "when viewing availability" do
context "with are no deactivations" do
context "with no deactivations" do
it "displays created_at as availability date" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from #{scheme.created_at.to_formatted_s(:govuk_date)}")
end
it "displays current collection start date as availability date if created_at is later than collection start date" do
scheme.update!(created_at: Time.zone.local(2022, 4, 16))
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022")
end
end
context "with previous deactivations" do
context "and all reactivated deactivations" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: Time.zone.local(2022, 9, 28), scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022\nActive from 28 September 2022")
end
end
context "and non reactivated deactivation" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil, scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022")
end
end
end
context "with out of order deactivations" do
context "and all reactivated deactivations" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: Time.zone.local(2022, 6, 18), scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 18 June 2022 to 23 September 2022\nDeactivated on 24 September 2022\nActive from 28 September 2022")
end
end
context "and one non reactivated deactivation" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022")
end
end
end
context "with multiple out of order deactivations" do
context "and one non reactivated deactivation" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 24), reactivation_date: Time.zone.local(2022, 10, 28), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2022 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022 to 23 October 2022\nDeactivated on 24 October 2022\nActive from 28 October 2022")
end
end
end
context "with intersecting deactivations" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil, scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 10), reactivation_date: Time.zone.local(2022, 12, 1), scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 11, 11), reactivation_date: Time.zone.local(2022, 12, 11), scheme:)
scheme.reload
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 8 August 2022 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022")
expect(availability_attribute).to eq("Active from 1 April 2022 to 9 October 2022\nDeactivated on 10 October 2022\nActive from 11 December 2022")
end
end
end

7
spec/models/form/lettings/pages/housing_provider_spec.rb

@ -79,7 +79,6 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
before do
create(
:organisation_relationship,
:owning,
child_organisation: user.organisation,
parent_organisation: housing_provider,
)
@ -101,13 +100,11 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
before do
create(
:organisation_relationship,
:owning,
child_organisation: user.organisation,
parent_organisation: housing_provider1,
)
create(
:organisation_relationship,
:owning,
child_organisation: user.organisation,
parent_organisation: housing_provider2,
)
@ -140,8 +137,8 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
context "with >0 housing_providers" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
end
it "is shown" do

10
spec/models/form/lettings/pages/managing_organisation_spec.rb

@ -76,8 +76,8 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
context "with >1 managing_agents" do
before do
create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation)
create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation)
create(:organisation_relationship, parent_organisation: log.owning_organisation)
create(:organisation_relationship, parent_organisation: log.owning_organisation)
end
it "is shown" do
@ -91,7 +91,6 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
before do
create(
:organisation_relationship,
:managing,
child_organisation: managing_agent,
parent_organisation: log.owning_organisation,
)
@ -128,8 +127,8 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
context "with >1 managing_agents" do
before do
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
create(:organisation_relationship, parent_organisation: user.organisation)
create(:organisation_relationship, parent_organisation: user.organisation)
end
it "is shown" do
@ -143,7 +142,6 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
before do
create(
:organisation_relationship,
:managing,
child_organisation: managing_agent,
parent_organisation: user.organisation,
)

8
spec/models/form/lettings/questions/housing_provider_spec.rb

@ -107,7 +107,7 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers != 0" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
end
it "is visible in check answers" do
@ -122,7 +122,7 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers <= 1" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
end
it "is hidden in check answers" do
@ -133,8 +133,8 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers >= 2" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
create(:organisation_relationship, child_organisation: user.organisation)
end
it "is visible in check answers" do

16
spec/models/form/lettings/questions/managing_organisation_spec.rb

@ -56,8 +56,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
let(:log) { create(:lettings_log) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let!(:org_rel1) { create(:organisation_relationship, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, parent_organisation: user.organisation) }
let(:options) do
{
@ -77,8 +77,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) }
let(:log) { create(:lettings_log) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let!(:org_rel1) { create(:organisation_relationship, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, parent_organisation: user.organisation) }
let(:options) do
{
@ -98,8 +98,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: false) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let(:options) do
{
@ -118,8 +118,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: true) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let(:options) do
{

14
spec/models/location_spec.rb

@ -151,6 +151,12 @@ RSpec.describe Location, type: :model do
location.save!
expect(location.status).to eq(:reactivating_soon)
end
it "returns activating soon if the location has a future startdate" do
location.startdate = Time.zone.local(2022, 7, 7)
location.save!
expect(location.status).to eq(:activating_soon)
end
end
context "when there have been previous deactivations" do
@ -188,12 +194,18 @@ RSpec.describe Location, type: :model do
expect(location.status).to eq(:reactivating_soon)
end
it "returns if the location had a deactivation during another deactivation" do
it "returns reactivating soon if the location had a deactivation during another deactivation" do
Timecop.freeze(2022, 6, 4)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), location:)
location.save!
expect(location.status).to eq(:reactivating_soon)
end
it "returns activating soon if the location has a future startdate" do
location.startdate = Time.zone.local(2022, 7, 7)
location.save!
expect(location.status).to eq(:activating_soon)
end
end
end
end

25
spec/models/organisation_relationship_spec.rb

@ -0,0 +1,25 @@
require "rails_helper"
RSpec.describe OrganisationRelationship do
let(:parent_organisation) { create(:organisation) }
let(:child_organisation) { create(:organisation) }
context "when a relationship exists" do
subject!(:relationship) do
described_class.create!(parent_organisation:,
child_organisation:)
end
describe "parent#managing_agents" do
it "includes child" do
expect(parent_organisation.managing_agents).to include(child_organisation)
end
end
describe "child#housing_providers" do
it "includes parent" do
expect(child_organisation.housing_providers).to include(parent_organisation)
end
end
end
end

20
spec/models/organisation_spec.rb

@ -34,14 +34,12 @@ RSpec.describe Organisation, type: :model do
before do
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
@ -65,21 +63,12 @@ RSpec.describe Organisation, type: :model do
before do
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
@ -98,21 +87,12 @@ RSpec.describe Organisation, type: :model do
before do
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)

20
spec/models/scheme_spec.rb

@ -125,6 +125,12 @@ RSpec.describe Scheme, type: :model do
scheme.reload
expect(scheme.status).to eq(:deactivated)
end
it "returns reactivating soon if the location has a future reactivation date" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), scheme:)
scheme.save!
expect(scheme.status).to eq(:reactivating_soon)
end
end
context "when there have been previous deactivations" do
@ -153,6 +159,20 @@ RSpec.describe Scheme, type: :model do
scheme.reload
expect(scheme.status).to eq(:deactivated)
end
it "returns reactivating soon if the scheme has a future reactivation date" do
Timecop.freeze(2022, 6, 8)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), scheme:)
scheme.save!
expect(scheme.status).to eq(:reactivating_soon)
end
it "returns if the scheme had a deactivation during another deactivation" do
Timecop.freeze(2022, 6, 4)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:)
scheme.save!
expect(scheme.status).to eq(:reactivating_soon)
end
end
end
end

38
spec/requests/organisation_relationships_controller_spec.rb

@ -21,8 +21,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
@ -83,8 +83,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
@ -153,7 +153,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id)
end
it "redirects to the organisation list" do
@ -181,7 +181,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING)
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id)
end
it "redirects to the organisation list" do
@ -200,7 +200,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: }
before do
FactoryBot.create(:organisation_relationship, :owning, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
end
it "deletes the new organisation relationship" do
@ -225,7 +225,6 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
before do
FactoryBot.create(
:organisation_relationship,
:managing,
parent_organisation: organisation,
child_organisation: managing_agent,
)
@ -256,8 +255,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
@ -304,8 +303,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
@ -383,7 +382,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id)
end
it "redirects to the organisation list" do
@ -411,7 +410,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING)
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id)
end
it "redirects to the organisation list" do
@ -430,7 +429,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: }
before do
FactoryBot.create(:organisation_relationship, :owning, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
end
it "deletes the new organisation relationship" do
@ -455,7 +454,6 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
before do
FactoryBot.create(
:organisation_relationship,
:managing,
parent_organisation: organisation,
child_organisation: managing_agent,
)
@ -477,8 +475,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
@ -531,8 +529,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end

17
spec/requests/schemes_controller_spec.rb

@ -274,7 +274,7 @@ RSpec.describe SchemesController, type: :request do
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation")
end
end
@ -283,7 +283,7 @@ RSpec.describe SchemesController, type: :request do
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation")
end
end
end
@ -915,7 +915,6 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a support" do
let(:user) { FactoryBot.create(:user, :support) }
let(:scheme_to_update) { FactoryBot.create(:scheme, owning_organisation: user.organisation, confirmed: nil) }
# let!(:location) { FactoryBot.create(:location, scheme: scheme_to_update) }
before do
FactoryBot.create(:location, scheme: scheme_to_update)
@ -1855,7 +1854,7 @@ RSpec.describe SchemesController, type: :request do
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.scheme.deactivation_date.not_selected"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.not_selected"))
end
end
@ -1864,7 +1863,7 @@ RSpec.describe SchemesController, type: :request do
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.scheme.deactivation_date.invalid"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
@ -1873,7 +1872,7 @@ RSpec.describe SchemesController, type: :request do
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.scheme.deactivation_date.out_of_range", date: "1 April 2022"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.out_of_range", date: "1 April 2022"))
end
end
@ -1882,7 +1881,7 @@ RSpec.describe SchemesController, type: :request do
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
@ -1891,7 +1890,7 @@ RSpec.describe SchemesController, type: :request do
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
@ -1900,7 +1899,7 @@ RSpec.describe SchemesController, type: :request do
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid"))
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
end

57
spec/views/layouts/application_layout_spec.rb

@ -0,0 +1,57 @@
require "rails_helper"
RSpec.describe "layouts/application" do
shared_examples "analytics cookie elements" do |banner:, scripts:|
define_negated_matcher :not_match, :match
it "#{banner ? 'includes' : 'omits'} the cookie banner" do
banner_text = "We’d like to use analytics cookies so we can understand how you use the service and make improvements."
if banner
expect(rendered).to match(banner_text)
else
expect(rendered).not_to match(banner_text)
end
end
it "#{scripts ? 'includes' : 'omits'} the analytics scripts" do
gtm_script_tag = /<script.*googletagmanager/
gtm_iframe_tag = /<iframe.*googletagmanager/
if scripts
expect(rendered).to match(gtm_script_tag).and match(gtm_iframe_tag)
else
expect(rendered).to not_match(gtm_script_tag).and not_match(gtm_iframe_tag)
end
end
end
context "with no cookie set" do
before do
cookies[:accept_analytics_cookies] = nil
render
end
include_examples "analytics cookie elements", banner: true, scripts: false
it "sets window.analyticsScript for the JS to refer to if the user accepts" do
expect(rendered).to match(/window\.analyticsScript = "https:\/\/www\.googletagmanager\.com\/gtag\/js\?id=G-[\w\d]+"/)
end
end
context "with analytics accepted" do
before do
cookies[:accept_analytics_cookies] = "on"
render
end
include_examples "analytics cookie elements", banner: false, scripts: true
end
context "with analytics rejected" do
before do
cookies[:accept_analytics_cookies] = "off"
render
end
include_examples "analytics cookie elements", banner: false, scripts: false
end
end
Loading…
Cancel
Save