Browse Source

WIP validations

juris_katrina_test
JG 3 years ago
parent
commit
dbd29de671
  1. 16
      app/controllers/schemes_controller.rb
  2. 5
      app/models/scheme.rb
  3. 5
      app/views/schemes/edit_name.html.erb
  4. 17
      config/locales/en.yml

16
app/controllers/schemes_controller.rb

@ -27,7 +27,9 @@ class SchemesController < ApplicationController
def create
@scheme = Scheme.new(scheme_params)
if @scheme.save
validation_errors scheme_params
if @scheme.errors.empty? && @scheme.save
if scheme_params[:support_services_provider] == "The same organisation that owns the housing stock"
redirect_to scheme_primary_client_group_path(@scheme)
else
@ -44,7 +46,9 @@ class SchemesController < ApplicationController
check_answers = params[:scheme][:check_answers]
page = params[:scheme][:page]
if @scheme.update(scheme_params)
validation_errors scheme_params
if @scheme.errors.empty? && @scheme.update(scheme_params)
if check_answers
if confirm_secondary_page? page
redirect_to scheme_secondary_client_group_path(@scheme, check_answers: "true")
@ -56,7 +60,7 @@ class SchemesController < ApplicationController
redirect_to next_page_path params[:scheme][:page]
end
else
render current_template(request.referer), status: :unprocessable_entity
render current_template(page), status: :unprocessable_entity
end
end
@ -102,6 +106,12 @@ class SchemesController < ApplicationController
private
def validation_errors scheme_params
scheme_params.keys.each do |key|
@scheme.errors.add(key.to_sym) if scheme_params[key].to_s.empty?
end
end
def confirm_secondary_page?(page)
page == "confirm-secondary" && @scheme.has_other_client_group == "Yes"
end

5
app/models/scheme.rb

@ -4,11 +4,6 @@ class Scheme < ApplicationRecord
has_many :locations
has_many :case_logs
# validates_presence_of :service_name
# validates_presence_of :scheme_type
# validates_presence_of :registered_under_care_act
# validates :primary_client_group, presence: true, on: :update
scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) }
scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") }
scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") }

5
app/views/schemes/edit_name.html.erb

@ -28,10 +28,13 @@
label: { text: "This scheme contains confidential information" } %>
<% end %>
<% 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 current_user.support? %>
<%= f.govuk_collection_select :owning_organisation_id,
organisations,
answer_options,
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },

17
config/locales/en.yml

@ -49,12 +49,25 @@ en:
blank: "Enter the existing organisation’s name"
service_name:
blank: "Enter the scheme’s name"
invalid: "Enter the scheme’s name"
scheme_type:
blank: "Select the scheme’s type"
invalid: "Select the scheme’s type"
registered_under_care_act:
blank: "You must tell us if this scheme is registered under the Care Standards Act 2000"
blank: "Select if this scheme is registered under the Care Standards Act 2000"
invalid: "Select if this scheme is registered under the Care Standards Act 2000"
primary_client_group:
blank: "Select what client group is this scheme intended for"
invalid: "Select what client group is this scheme intended for"
secondary_client_group:
invalid: "Select what is the other client group"
support_type:
invalid: "Select level of support given"
intended_stay:
invalid: "Select intended length of stay"
has_other_client_group:
invalid: "Select if this scheme provides for another client group"
support_services_provider:
invalid: "Select who provides the support services used by this scheme"
validations:

Loading…
Cancel
Save