From 2f59e0e4e8c72a87db71573f8377dc3a5c4764b8 Mon Sep 17 00:00:00 2001 From: JG Date: Wed, 22 Jun 2022 11:26:50 +0100 Subject: [PATCH] spike finished --- app/controllers/schemes_controller.rb | 15 +++++++++++++-- app/models/scheme.rb | 10 +++++++++- app/views/organisations/schemes.html.erb | 2 ++ app/views/schemes/check_answers.html.erb | 1 + app/views/schemes/details.html.erb | 18 ++++++++---------- app/views/schemes/new.html.erb | 9 ++++----- .../schemes/primary_client_group.html.erb | 2 +- db/seeds.rb | 3 --- 8 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 9600e6634..1d556a4e0 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -40,12 +40,19 @@ 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 + 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) + end + render "schemes/primary_client_group" end @@ -81,6 +88,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 render "schemes/details" end @@ -106,8 +118,7 @@ class SchemesController < ApplicationController private def clean_params - code = "S#{SecureRandom.alphanumeric(5)}".upcase - required_params = params.require(:scheme).permit(:service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed).merge(code: code) + required_params = params.require(:scheme).permit(:service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed) required_params[:sensitive] = required_params[:sensitive].to_i required_params end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 56cb8d3a7..8da1f3ca4 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -1,4 +1,6 @@ class Scheme < ApplicationRecord + before_create :create_code + belongs_to :organisation has_many :locations has_many :case_logs @@ -8,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 @@ -125,4 +127,10 @@ class Scheme < ApplicationRecord { name: "Intended length of stay", value: intended_stay }, ] end + + private + + def create_code + self.code = Scheme.last.nil? ? "S1" : "S#{Scheme.last.code[1..-1].to_i + 1}" + end end diff --git a/app/views/organisations/schemes.html.erb b/app/views/organisations/schemes.html.erb index 65f7e7f23..4c59601e6 100644 --- a/app/views/organisations/schemes.html.erb +++ b/app/views/organisations/schemes.html.erb @@ -11,6 +11,8 @@ ) %> <% end %> +<%= govuk_button_link_to "Create a new supported housing scheme", new_scheme_path, html: { method: :post } %> +

Supported housing schemes

<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name, code or postcode", value: @searched) %> diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 82d070ba0..998b59819 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -13,6 +13,7 @@ <% component.tab(label: 'Scheme') do %> <%= govuk_summary_list do |summary_list| %> <% @scheme.check_details_attributes.each do |attr| %> + <% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> <%= summary_list.row do |row| %> <% row.key { attr[:name].to_s } %> <% row.value { details_html(attr) } %> diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 19498feb4..84a1ca9f5 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -3,13 +3,13 @@ <% content_for :before_content do %> <%= govuk_back_link( text: "Back", - href: "/schemes/#{@scheme.id}/secondary", + href: :back, ) %> <% end %> <%= render partial: "organisations/headings", locals: { main: "Scheme details", sub: @scheme.service_name } %> -<%= form_for(@scheme, method: :patch, url: scheme_check_your_answers_path(scheme_id: @scheme.id)) do |f| %> +<%= form_for(@scheme, method: :patch, url: @path) do |f| %>
<%= f.govuk_error_summary %> @@ -26,13 +26,7 @@ label: { text: "This scheme contains confidential information" } %> <% if current_user.support? %> - <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %> - <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - <% answer_options = null_option + organisations %> - <% if @organisation_id %> - <% organisation = Organisation.find(@organisation_id) %> - <% answer_options = [OpenStruct.new(id: organisation.id, name: organisation.name)] %> - <% end %> + <% answer_options = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <%= f.govuk_collection_select :organisation_id, answer_options, @@ -40,10 +34,14 @@ :name, label: { text: "Which organisation manages this scheme", size: "m" }, hint: { text: "Enter organisation name" }, - options: { disabled: [""], selected: @scheme.organisation.name }, + options: { required: true }, "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> + <% if current_user.data_coordinator? %> + <%= f.hidden_field :organisation_id, value: current_user.organisation.id %> + <% end %> + <% 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, diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index 4ba910a5f..e94b1cdfb 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -32,10 +32,6 @@ <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %> <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <% answer_options = null_option + organisations %> - <% if @organisation_id %> - <% organisation = Organisation.find(@organisation_id) %> - <% answer_options = [OpenStruct.new(id: organisation.id, name: organisation.name)] %> - <% end %> <%= f.govuk_collection_select :organisation_id, answer_options, @@ -43,10 +39,13 @@ :name, label: { text: "Which organisation manages this scheme", size: "m" }, hint: { text: "Enter organisation name" }, - options: { disabled: [""], selected: selected_option(answer_options) }, "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> + <% if current_user.data_coordinator? %> + <%= f.hidden_field :organisation_id, value: current_user.organisation.id %> + <% end %> + <% 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, diff --git a/app/views/schemes/primary_client_group.html.erb b/app/views/schemes/primary_client_group.html.erb index 8c60f5ca3..98080d9b5 100644 --- a/app/views/schemes/primary_client_group.html.erb +++ b/app/views/schemes/primary_client_group.html.erb @@ -3,7 +3,7 @@ <% content_for :before_content do %> <%= govuk_back_link( text: "Back", - href: 'javascript:history.go(-1);', + href: "/schemes/#{@scheme.id}/details", ) %> <% end %> diff --git a/db/seeds.rb b/db/seeds.rb index fdf9d6172..654f1c777 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -71,7 +71,6 @@ unless Rails.env.test? if Rails.env.development? && Scheme.count.zero? scheme1 = Scheme.create!( - code: "S878", service_name: "Beulahside Care", sensitive: 0, registered_under_care_act: 0, @@ -86,7 +85,6 @@ unless Rails.env.test? ) scheme2 = Scheme.create!( - code: "S312", service_name: "Abdullahview Point", sensitive: 0, registered_under_care_act: 1, @@ -101,7 +99,6 @@ unless Rails.env.test? ) Scheme.create!( - code: "7XYZ", service_name: "Caspermouth Center", sensitive: 1, registered_under_care_act: 1,