From d7ceec030c9c581875a906d23dbe53bef1509414 Mon Sep 17 00:00:00 2001 From: JG Date: Fri, 15 Jul 2022 15:33:31 +0100 Subject: [PATCH] wip --- app/controllers/schemes_controller.rb | 27 ++++++++++------- app/views/schemes/details.html.erb | 37 ++++++++++++++---------- app/views/schemes/new.html.erb | 32 ++++++++++++-------- spec/requests/schemes_controller_spec.rb | 14 ++++----- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 443a28ecd..040ed68db 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -25,12 +25,12 @@ class SchemesController < ApplicationController end def create - @scheme = Scheme.new(scheme_params) + @scheme = Scheme.new(scheme_params.except(:support_services_provider_before_type_cast)) - validation_errors scheme_params + validation_errors scheme_params.except(:support_services_provider_before_type_cast) if @scheme.errors.empty? && @scheme.save - if scheme_params[:support_services_provider] == "The same organisation that owns the housing stock" + if scheme_params[:support_services_provider].zero? redirect_to scheme_primary_client_group_path(@scheme) else redirect_to scheme_support_services_provider_path(@scheme) @@ -46,9 +46,9 @@ class SchemesController < ApplicationController check_answers = params[:scheme][:check_answers] page = params[:scheme][:page] - validation_errors scheme_params + validation_errors scheme_params.except(:support_services_provider_before_type_cast) - if @scheme.errors.empty? && @scheme.update(scheme_params) + if @scheme.errors.empty? && @scheme.update(scheme_params.except(:support_services_provider_before_type_cast)) if check_answers if confirm_secondary_page? page redirect_to scheme_secondary_client_group_path(@scheme, check_answers: "true") @@ -96,7 +96,7 @@ class SchemesController < ApplicationController render "schemes/support_services_provider" end -private + private def validation_errors(scheme_params) scheme_params.each_key do |key| @@ -139,7 +139,7 @@ private when "support" new_location_path when "details" - if @scheme.support_services_provider.eql? "The same organisation that owns the housing stock" + if @scheme.support_services_provider.eql? "0" scheme_primary_client_group_path(@scheme) else scheme_support_services_provider_path(@scheme) @@ -162,17 +162,24 @@ private :secondary_client_group, :support_type, :support_services_provider, + :support_services_provider_before_type_cast, :intended_stay) - same_org_providing_support = required_params[:support_services_provider] == "The same organisation that owns the housing stock" + same_org_providing_support = required_params[:support_services_provider_before_type_cast] == "0" full_params = same_org_providing_support && required_params[:owning_organisation_id].present? ? required_params.merge(managing_organisation_id: required_params[:owning_organisation_id]) : required_params full_params[:sensitive] = full_params[:sensitive].to_i if full_params[:sensitive] + + if full_params[:support_services_provider_before_type_cast] + translated_params = full_params.merge(support_services_provider: full_params[:support_services_provider_before_type_cast].to_i) + else + translated_params = full_params + end if current_user.data_coordinator? - full_params[:owning_organisation_id] = current_user.organisation_id + translated_params[:owning_organisation_id] = current_user.organisation_id end - full_params + translated_params end def search_term diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 788c36ce6..52d8a255f 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -2,9 +2,9 @@ <% content_for :before_content do %> <%= govuk_back_link( - text: "Back", - href: :back, - ) %> + text: "Back", + href: :back, + ) %> <% end %> <%= render partial: "organisations/headings", locals: { main: "Create a new supported housing scheme", sub: nil } %> @@ -15,11 +15,11 @@ <%= f.govuk_error_summary %> <%= f.govuk_text_field :service_name, - label: { text: "Scheme name", size: "m" }, - hint: { text: "This is how you refer to this supported housing scheme within your organisation. For example, the name could relate to the address or location. You’ll be able to see the client group when selecting it." } %> + label: { text: "Scheme name", size: "m" }, + hint: { text: "This is how you refer to this supported housing scheme within your organisation. For example, the name could relate to the address or location. You’ll be able to see the client group when selecting it." } %> <%= f.govuk_check_boxes_fieldset :sensitive, - legend: nil do %> + legend: nil do %> <%= f.govuk_check_box :sensitive, 1, 0, @@ -35,10 +35,10 @@ <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, - scheme_types_selection, - :id, - :name, - legend: { text: "What is this type of scheme?", size: "m" } %> + scheme_types_selection, + :id, + :name, + legend: { text: "What is this type of scheme?", size: "m" } %> <% care_acts_options_hints = { "Yes – part registered as a care home": "A proportion of units are registered as being a care home." } %> @@ -63,17 +63,24 @@ "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> - <% support_services_provider_selection = Scheme.support_services_providers.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> - - <%= f.govuk_collection_radio_buttons :support_services_provider, + <% support_services_provider_selection = Scheme.support_services_providers.map do |key, value| %> + <% OpenStruct.new(id: value, name: key.to_s.humanize) %> + <% end %> + <%= f.govuk_collection_radio_buttons :support_services_provider_before_type_cast, support_services_provider_selection, :id, - :name, + ->(option) do + if option.id.zero? && !current_user.support? + "Your organisation" + else + option.name + end + end, legend: { text: "Who provides the support services used by this scheme?", size: "m" } %> <%= f.hidden_field :page, value: "details" %> <% if request.query_parameters["check_answers"] %> - <%= f.hidden_field :check_answers, value: "true" %> + <%= f.hidden_field :check_answers, value: "true" %> <% end %> <%= f.govuk_submit "Save and continue" %> diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index eca8de2a3..10bee999a 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -2,9 +2,9 @@ <% content_for :before_content do %> <%= govuk_back_link( - text: "Back", - href: "javascript:history.go(-1);", - ) %> + text: "Back", + href: "javascript:history.go(-1);", + ) %> <% end %> <%= form_for(@scheme, as: :scheme, method: :post) do |f| %> @@ -17,8 +17,8 @@ <%= f.govuk_text_field :service_name, - label: { text: "Scheme name", size: "m" }, - hint: { text: "This is how you refer to this supported housing scheme within your organisation. For example, the name could relate to the address or location. You’ll be able to see the client group when selecting it." } %> + label: { text: "Scheme name", size: "m" }, + hint: { text: "This is how you refer to this supported housing scheme within your organisation. For example, the name could relate to the address or location. You’ll be able to see the client group when selecting it." } %> <%= f.govuk_check_boxes_fieldset :sensitive, legend: nil do %> @@ -39,10 +39,10 @@ <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, - scheme_types_selection, - :id, - :name, - legend: { text: "What is this type of scheme?", size: "m" } %> + scheme_types_selection, + :id, + :name, + legend: { text: "What is this type of scheme?", size: "m" } %> <% care_acts_options_hints = { "Yes – part registered as a care home": "A proportion of units are registered as being a care home." } %> @@ -65,12 +65,20 @@ "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> - <% support_services_provider_selection = Scheme.support_services_providers.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + <% support_services_provider_selection = Scheme.support_services_providers.map do |key, value| %> + <% OpenStruct.new(id: value, name: key.to_s.humanize) %> + <% end %> - <%= f.govuk_collection_radio_buttons :support_services_provider, + <%= f.govuk_collection_radio_buttons :support_services_provider_before_type_cast, support_services_provider_selection, :id, - :name, + ->(option) do + if option.id.zero? && !current_user.support? + "Your organisation" + else + option.name + end + end, legend: { text: "Who provides the support services used by this scheme?", size: "m" } %> <%= f.govuk_submit "Save and continue" %> diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index b970fa57d..a2ec127e9 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -341,7 +341,7 @@ RSpec.describe SchemesController, type: :request do sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", - support_services_provider: "The same organisation that owns the housing stock" } } + support_services_provider_before_type_cast: "0" } } end before do @@ -378,7 +378,7 @@ RSpec.describe SchemesController, type: :request do sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", - support_services_provider: "Another registered housing provider" } } + support_services_provider_before_type_cast: "1" } } end it "creates a new scheme for user organisation with valid params and renders correct page" do @@ -411,7 +411,7 @@ RSpec.describe SchemesController, type: :request do { scheme: { service_name: "", scheme_type: "", registered_under_care_act: "", - support_services_provider: "" } } + support_services_provider_before_type_cast: "" } } end it "renders the same page with error message" do @@ -435,7 +435,7 @@ RSpec.describe SchemesController, type: :request do scheme_type: "Foyer", registered_under_care_act: "No", owning_organisation_id: organisation.id, - support_services_provider: "The same organisation that owns the housing stock" } } + support_services_provider_before_type_cast: "0" } } end before do @@ -474,7 +474,7 @@ RSpec.describe SchemesController, type: :request do scheme_type: "Foyer", registered_under_care_act: "No", owning_organisation_id: organisation.id, - support_services_provider: "Another registered housing provider" } } + support_services_provider_before_type_cast: "1" } } end it "creates a new scheme for user organisation with valid params and renders correct page" do @@ -506,7 +506,7 @@ RSpec.describe SchemesController, type: :request do { scheme: { service_name: "", scheme_type: "", registered_under_care_act: "", - support_services_provider: "" } } + support_services_provider_before_type_cast: "" } } end it "renders the same page with error message" do @@ -787,7 +787,7 @@ RSpec.describe SchemesController, type: :request do registered_under_care_act: "No", page: "details", owning_organisation_id: organisation.id, - support_services_provider: "The same organisation that owns the housing stock" } } + support_services_provider_before_type_cast: "0" } } end it "renders confirm secondary group after successful update" do