From 5964d2f4bcbc37beecd71caf5b83924c63a59e3d Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 24 Jun 2022 14:20:41 +0100 Subject: [PATCH] Add has other client group field to schemes. Display it in the check answers. Fix tests and routing --- Gemfile.lock | 1 - app/controllers/schemes_controller.rb | 51 +++++++++++-------- app/models/scheme.rb | 20 ++++++-- app/views/schemes/check_answers.html.erb | 10 ++++ app/views/schemes/confirm_secondary.html.erb | 7 ++- app/views/schemes/index.html.erb | 2 +- config/routes.rb | 4 +- ...132228_add_has_other_client_group_field.rb | 7 +++ db/schema.rb | 6 ++- spec/features/schemes_spec.rb | 17 ++++--- .../requests/organisations_controller_spec.rb | 8 +-- spec/requests/schemes_controller_spec.rb | 4 +- 12 files changed, 92 insertions(+), 45 deletions(-) create mode 100644 db/migrate/20220623132228_add_has_other_client_group_field.rb diff --git a/Gemfile.lock b/Gemfile.lock index b71a87ded..d7648b37c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -469,7 +469,6 @@ DEPENDENCIES rubocop-govuk (= 4.3.0) rubocop-performance rubocop-rails - securerandom selenium-webdriver sentry-rails sentry-ruby diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 1d556a4e0..38d83b427 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -32,7 +32,7 @@ class SchemesController < ApplicationController def create @scheme = Scheme.new(clean_params) - @scheme.save + @scheme.save! @path = scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id) render "schemes/primary_client_group" @@ -41,26 +41,27 @@ class SchemesController < ApplicationController def primary_client_group @scheme = Scheme.find_by(id: params[:scheme_id]) - if params[:check_answers] - @path = scheme_check_your_answers_path(scheme_id: @scheme.id) - else - @path = scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id) - end + @path = if params[:check_answers] + scheme_check_your_answers_path(scheme_id: @scheme.id) + else + scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id) + end if params[:scheme] required_params = params.require(:scheme).permit(:intended_stay, :support_type, :service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed, :secondary_client_group, :primary_client_group) required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - @scheme.update(required_params) + @scheme.update!(required_params) end render "schemes/primary_client_group" end - def confirm_secondary_group + def confirm_secondary_client_group @scheme = Scheme.find_by(id: params[:scheme_id]) + @path = params[:check_answers] ? scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true) : scheme_secondary_client_group_path(scheme_id: @scheme.id) if params[:scheme] required_params = params.require(:scheme).permit(:primary_client_group) if params - @scheme.update(required_params) if required_params + @scheme.update!(required_params) if required_params end render "schemes/confirm_secondary" @@ -69,8 +70,16 @@ class SchemesController < ApplicationController def secondary_client_group @scheme = Scheme.find_by(id: params[:scheme_id]) @path = params[:check_answers] ? scheme_check_your_answers_path(scheme_id: @scheme.id) : scheme_support_path(scheme_id: @scheme.id) - if params[:confirmed] - params[:confirmed][:selection] == "Yes" ? render("schemes/secondary_client_group") : render("schemes/support") + if params[:scheme] + required_params = params.require(:scheme).permit(:has_other_client_group) if params + @scheme.update!(required_params) if required_params + if @scheme.has_other_client_group == "Yes" + render("schemes/secondary_client_group") + elsif params[:check_answers] + redirect_to(scheme_check_your_answers_path(sheme_id: @scheme.id)) + else + redirect_to(scheme_support_path(scheme_id: @scheme.id)) + end else render "schemes/secondary_client_group" end @@ -80,7 +89,7 @@ class SchemesController < ApplicationController @scheme = Scheme.find_by(id: params[:scheme_id]) if params[:scheme] required_params = params.require(:scheme).permit(:secondary_client_group) - @scheme.update(required_params) if required_params + @scheme.update!(required_params) if required_params end render "schemes/support" @@ -88,11 +97,11 @@ class SchemesController < ApplicationController def details @scheme = Scheme.find_by(id: params[:scheme_id]) - if params[:check_answers] - @path = scheme_check_your_answers_path(scheme_id: @scheme.id) - else - @path = scheme_primary_client_group_path(scheme_id: @scheme.id) - end + @path = if params[:check_answers] + scheme_check_your_answers_path(scheme_id: @scheme.id) + else + scheme_primary_client_group_path(scheme_id: @scheme.id) + end render "schemes/details" end @@ -102,7 +111,7 @@ class SchemesController < ApplicationController if params[:scheme] required_params = params.require(:scheme).permit(:intended_stay, :support_type, :service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed, :secondary_client_group, :primary_client_group) required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - @scheme.update(required_params) + @scheme.update!(required_params) end render "schemes/check_answers" @@ -110,12 +119,14 @@ class SchemesController < ApplicationController def update @scheme = Scheme.find_by(id: params[:scheme_id]) - flash[:notice] = ("#{@scheme.service_name} has been created.") + flash[:notice] = "#{@scheme.service_name} has been created." redirect_to schemes_path end - private + def edit; end + +private def clean_params required_params = params.require(:scheme).permit(:service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 8da1f3ca4..e2500bbe9 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -10,7 +10,7 @@ class Scheme < ApplicationRecord scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by, ->(param) { search_by_postcode(param).or(search_by_service_name(param)).or(search_by_code(param)).distinct } - SENSITIVE= { + SENSITIVE = { No: 0, Yes: 1, }.freeze @@ -74,12 +74,18 @@ class Scheme < ApplicationRecord INTENDED_STAY = { "Medium stay": "M", "Permanent": "P", - "Short Stay": "S", + "Short stay": "S", "Very short stay": "V", "Missing": "X", }.freeze + HAS_OTHER_CLIENT_GROUP = { + "Yes": "yes", + "No": "no", + }.freeze + enum intended_stay: INTENDED_STAY, _suffix: true + enum has_other_client_group: HAS_OTHER_CLIENT_GROUP, _suffix: true def check_details_attributes [ @@ -99,6 +105,12 @@ class Scheme < ApplicationRecord ] end + def check_secondary_client_confirmation_attributes + [ + { name: "Scheme provides for another client group", value: has_other_client_group }, + ] + end + def check_secondary_client_attributes [ { name: "Secondary client group", value: secondary_client_group }, @@ -128,9 +140,9 @@ class Scheme < ApplicationRecord ] end - private +private def create_code - self.code = Scheme.last.nil? ? "S1" : "S#{Scheme.last.code[1..-1].to_i + 1}" + self.code = Scheme.last.nil? ? "S1" : "S#{Scheme.last.code[1..].to_i + 1}" end end diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 998b59819..20f29d0b9 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -33,6 +33,16 @@ ) %> <% end %> <% end %> + <% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> + <% end %> <% @scheme.check_secondary_client_attributes.each do |attr| %> <%= summary_list.row do |row| %> <% row.key { attr[:name].to_s } %> diff --git a/app/views/schemes/confirm_secondary.html.erb b/app/views/schemes/confirm_secondary.html.erb index 7f7da7d35..38bdc0eb8 100644 --- a/app/views/schemes/confirm_secondary.html.erb +++ b/app/views/schemes/confirm_secondary.html.erb @@ -9,17 +9,16 @@ <%= render partial: "organisations/headings", locals: { main: "Does this scheme provide for another client group?", sub: @scheme.service_name } %> -<%= form_for(:confirmed, method: :patch, url: scheme_secondary_client_group_path(scheme_id: @scheme.id)) do |f| %> +<%= form_for(@scheme, method: :patch, url: @path) do |f| %>
- <% selection = ["Yes", "No"].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + <% selection = [OpenStruct.new(id: "Yes", name: "Yes"), OpenStruct.new(id: "No", name: "No")] %> - <%= f.govuk_collection_radio_buttons :selection, + <%= f.govuk_collection_radio_buttons :has_other_client_group, selection, :id, :name, - { checked: "Yes" }, legend: nil %> diff --git a/app/views/schemes/index.html.erb b/app/views/schemes/index.html.erb index 9acbda5ab..ea318add5 100644 --- a/app/views/schemes/index.html.erb +++ b/app/views/schemes/index.html.erb @@ -9,7 +9,7 @@ <%= govuk_button_link_to "Create a new supported housing scheme", new_scheme_path, html: { method: :post } %> -<%= render SearchComponent.new(current_user:, search_label: "Search by service name or code", value: @searched) %> +<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name or code", value: @searched) %>
diff --git a/config/routes.rb b/config/routes.rb index 2f39699de..a0efdee0e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,8 +38,8 @@ Rails.application.routes.draw do resources :schemes do patch "primary-client-group", to: "schemes#primary_client_group" get "primary-client-group", to: "schemes#primary_client_group" - patch "confirm-secondary-client-group", to: "schemes#confirm_secondary_group" - get "confirm-secondary-client-group", to: "schemes#confirm_secondary_group" + patch "confirm-secondary-client-group", to: "schemes#confirm_secondary_client_group" + get "confirm-secondary-client-group", to: "schemes#confirm_secondary_client_group" patch "secondary-client-group", to: "schemes#secondary_client_group" get "secondary-client-group", to: "schemes#secondary_client_group" patch "support", to: "schemes#support" diff --git a/db/migrate/20220623132228_add_has_other_client_group_field.rb b/db/migrate/20220623132228_add_has_other_client_group_field.rb new file mode 100644 index 000000000..138cd26f1 --- /dev/null +++ b/db/migrate/20220623132228_add_has_other_client_group_field.rb @@ -0,0 +1,7 @@ +class AddHasOtherClientGroupField < ActiveRecord::Migration[7.0] + def change + change_table :schemes, bulk: true do |t| + t.column :has_other_client_group, :string + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 56f4c9b08..ad2897c11 100644 --- a/db/schema.rb +++ b/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_06_17_102313) do +ActiveRecord::Schema[7.0].define(version: 2022_06_23_132228) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -310,7 +310,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do t.integer "registered_under_care_act" t.integer "support_type" t.string "intended_stay" +<<<<<<< HEAD t.datetime "end_date" +======= + t.string "has_other_client_group" +>>>>>>> f6b27a1a (Add has other client group field to schemes. Display it in the check answers. Fix tests and routing) t.index ["organisation_id"], name: "index_schemes_on_organisation_id" end diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index c3f399ead..2319bc380 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -224,6 +224,7 @@ RSpec.describe "Schemes scheme Features" do end it "displays the link to create a new scheme" do + expect(page).to have_current_path("/schemes") expect(page).to have_link("Create a new supported housing scheme") end @@ -235,6 +236,7 @@ RSpec.describe "Schemes scheme Features" do end it "lets me fill in the scheme details" do + expect(page).to have_current_path("/schemes/new") expect(page).to have_content "Scheme name" expect(page).to have_content "This scheme contains confidential information" expect(page).to have_content "Which organisation manages this scheme" @@ -245,8 +247,8 @@ RSpec.describe "Schemes scheme Features" do context "when I fill in scheme details and I press save I see primary client group section" do before do - fill_in 'Scheme name', with: 'FooBar' - check 'This scheme contains confidential information' + fill_in "Scheme name", with: "FooBar" + check "This scheme contains confidential information" choose "Direct access hostel" choose "Yes – registered care home providing nursing care" fill_in "Total number of units", with: 1 @@ -261,6 +263,7 @@ RSpec.describe "Schemes scheme Features" do context "when I select primary client group details" do before do choose "Homeless families with support needs" + click_button "Save and continue" end it "lets me confirm if I want to select secondary group details" do @@ -270,19 +273,21 @@ RSpec.describe "Schemes scheme Features" do context "when I confirm the secondary group" do before do choose "Yes" + click_button "Save and continue" end - it "lets me select secondary client group detail" do - expect(page).to have_content "Does this scheme provide for another client group?" + it "lets me select secondary client group" do + expect(page).to have_content "What is the other client group?" end context "when I select the secondary group" do before do choose "Homeless families with support needs" + click_button "Save and continue" end - it "lets me select secondary client group detail" do - expect(page).to have_content "Does this scheme provide for another client group?" + it "lets me select level of support" do + expect(page).to have_content "What support does this scheme provide?" end end end diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index a929f9304..01a45f2b1 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -72,8 +72,8 @@ RSpec.describe OrganisationsController, type: :request do end context "when searching" do - let!(:searched_scheme) { FactoryBot.create(:scheme, code: "CODE321", organisation: user.organisation) } - let(:search_param) { "CODE321" } + let!(:searched_scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let(:search_param) { searched_scheme.code } before do FactoryBot.create(:location, scheme: searched_scheme) @@ -141,8 +141,8 @@ RSpec.describe OrganisationsController, type: :request do end context "when searching" do - let!(:searched_scheme) { FactoryBot.create(:scheme, code: "CODE321", organisation: user.organisation) } - let(:search_param) { "CODE321" } + let!(:searched_scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let(:search_param) { searched_scheme.code } before do FactoryBot.create(:location, scheme: searched_scheme) diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 0a8070442..eda2849e3 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -136,8 +136,8 @@ RSpec.describe SchemesController, type: :request do end context "when searching" do - let!(:searched_scheme) { FactoryBot.create(:scheme, code: "CODE321") } - let(:search_param) { "CODE321" } + let!(:searched_scheme) { FactoryBot.create(:scheme) } + let(:search_param) { searched_scheme.code } before do FactoryBot.create(:location, scheme: searched_scheme)