Skip to content

Commit

Permalink
Validate community clinic ODS code doesn't match organisation
Browse files Browse the repository at this point in the history
The generic clinic location will use the ODS code of the organisation,
so the community clinics cannot use this ODS code as there's a unique
index on the column.
  • Loading branch information
thomasleese committed Jan 10, 2025
1 parent f83f3b7 commit 1eb9a47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 6 additions & 2 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ class Location < ApplicationRecord
validates :team, presence: true
end

with_options if: :community_clinic? do
validates :ods_code, exclusion: { in: :organisation_ods_code }
end

with_options if: :generic_clinic? do
validates :ods_code, comparison: { equal_to: :organisation_ods_code }
validates :ods_code, inclusion: { in: :organisation_ods_code }
end

with_options if: :gp_practice? do
Expand All @@ -93,6 +97,6 @@ def dfe_number
private

def organisation_ods_code
team&.organisation&.ods_code
[team&.organisation&.ods_code]
end
end
14 changes: 11 additions & 3 deletions spec/models/location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@
it { should validate_presence_of(:name) }

context "with a community clinic" do
subject(:location) { build(:community_clinic, ods_code: "abc") }
subject(:location) { build(:community_clinic, organisation:) }

let(:organisation) { create(:organisation) }

it { should_not validate_presence_of(:gias_establishment_number) }
it { should_not validate_presence_of(:gias_local_authority_code) }

it { should validate_presence_of(:ods_code) }
it { should validate_uniqueness_of(:ods_code).ignoring_case_sensitivity }

it do
expect(location).to validate_exclusion_of(:ods_code).in_array(
[organisation.ods_code]
)
end

it { should_not validate_presence_of(:urn) }
it { should validate_uniqueness_of(:urn) }
end
Expand All @@ -75,8 +83,8 @@
it { should validate_uniqueness_of(:ods_code).ignoring_case_sensitivity }

it do
expect(location).to validate_comparison_of(:ods_code).is_equal_to(
organisation.ods_code
expect(location).to validate_inclusion_of(:ods_code).in_array(
[organisation.ods_code]
)
end

Expand Down

0 comments on commit 1eb9a47

Please sign in to comment.