Browse Source

code to implement correct validation

pull/671/head
JG 3 years ago
parent
commit
0c23031729
  1. 2
      app/models/scheme.rb
  2. 9
      config/locales/en.yml
  3. 5
      spec/models/scheme_spec.rb
  4. 2
      spec/requests/schemes_controller_spec.rb

2
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(' ')}%") }

9
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:
activerecord:
errors:
models:
scheme:
id_missing: "Enter the organisation’s name"
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"

5
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) }

2
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

Loading…
Cancel
Save