From 0c2303172965b9e57882d0c3b3d63595ea5f5b41 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 28 Jun 2022 08:50:44 +0100 Subject: [PATCH] code to implement correct validation --- app/models/scheme.rb | 2 -- config/locales/en.yml | 11 ++++++++--- spec/models/scheme_spec.rb | 5 +++++ spec/requests/schemes_controller_spec.rb | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index ea2f883a7..e2036c7ed 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -5,8 +5,6 @@ class Scheme < ApplicationRecord has_many :locations has_many :case_logs - validates :organisation_id, presence: { message: I18n.t("validations.scheme.id_missing") } - scope :search_by_code, ->(code) { where("code ILIKE ?", "%#{code}%") } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } diff --git a/config/locales/en.yml b/config/locales/en.yml index 420902aec..71fb99454 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -38,10 +38,15 @@ en: create_password: "Create a password to finish setting up your account" reset_password: "Reset your password" - validations: - scheme: - id_missing: "Enter the organisation’s name" + activerecord: + errors: + models: + scheme: + attributes: + organisation: + required: "Enter the organisation’s name" + validations: organisation: name_missing: "Enter the organisation’s name" provider_type_missing: "Select the organisation’s type" diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index d6e39ae2e..79f3d0e2a 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -8,6 +8,11 @@ RSpec.describe Scheme, type: :model do expect(scheme.organisation).to be_a(Organisation) end + it "validates organisation_id presence" do + expect { FactoryBot.create(:scheme, organisation: nil) } + .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.scheme.id_missing')}") + end + describe "scopes" do let!(:scheme_1) { FactoryBot.create(:scheme) } let!(:scheme_2) { FactoryBot.create(:scheme) } diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 863a0bc8c..d40a313bb 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -594,7 +594,7 @@ RSpec.describe SchemesController, type: :request do it "displays the new page with an error message" do post "/schemes", params: params expect(response).to have_http_status(:unprocessable_entity) - expect(page).to have_content(I18n.t("validations.organisation.name_missing")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.organisation.required")) end end end