Skip to content

Commit a772c50

Browse files
committed
Implement phase override
Use overriden value where present, else default to gias_phase
1 parent a500041 commit a772c50

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

app/models/location.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,23 @@ class Location < ApplicationRecord
126126
)
127127
end
128128

129+
GIAS_PHASE_MAPPINGS = {
130+
"nursery" => %w[nursery],
131+
"primary" => %w[primary middle_deemed_primary],
132+
"secondary" => %w[secondary middle_deemed_secondary],
133+
"other" => %w[sixteen_plus all_through not_applicable]
134+
}.freeze
135+
136+
PHASES = GIAS_PHASE_MAPPINGS.keys.freeze
137+
129138
scope :where_phase,
130-
->(phase) { where(gias_phase: GIAS_PHASE_MAPPINGS.fetch(phase)) }
139+
->(phase) do
140+
where(phase:).or(
141+
where(phase: nil).where(
142+
gias_phase: GIAS_PHASE_MAPPINGS.fetch(phase)
143+
)
144+
)
145+
end
131146

132147
scope :with_team,
133148
->(academic_year:) do
@@ -191,6 +206,7 @@ class Location < ApplicationRecord
191206
validates :gias_local_authority_code, presence: true
192207
validates :gias_phase, inclusion: Location.gias_phases.keys
193208
validates :ods_code, absence: true
209+
validates :phase, inclusion: { in: PHASES }, allow_nil: true
194210
validates :site, uniqueness: { scope: :urn }, allow_nil: true
195211
validates :urn, presence: true, uniqueness: { unless: :site }
196212
end
@@ -241,11 +257,10 @@ def dfe_number
241257
end
242258

243259
def phase
244-
if gias_phase
260+
super.presence ||
245261
GIAS_PHASE_MAPPINGS
246262
.find { |_, values| values.include?(gias_phase) }
247263
&.first
248-
end
249264
end
250265

251266
def as_json
@@ -307,13 +322,6 @@ def import_default_programme_year_groups!(programmes, academic_year:)
307322

308323
def organisation_ods_codes = Organisation.pluck(:ods_code)
309324

310-
GIAS_PHASE_MAPPINGS = {
311-
"nursery" => %w[nursery],
312-
"primary" => %w[primary middle_deemed_primary],
313-
"secondary" => %w[secondary middle_deemed_secondary],
314-
"other" => %w[sixteen_plus all_through not_applicable]
315-
}.freeze
316-
317325
def fhir_mapper
318326
@fhir_mapper ||= FHIRMapper::Location.new(self)
319327
end

spec/models/location_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@
407407
it { should eq("other") }
408408
end
409409

410+
context "with a phase override" do
411+
let(:location) do
412+
build(:gias_school, gias_phase: "secondary", phase: "primary")
413+
end
414+
415+
it { should eq("primary") }
416+
end
417+
410418
context "with something other than a school" do
411419
let(:location) { build(:community_clinic) }
412420

@@ -434,6 +442,7 @@
434442
"is_attached_to_team" => false,
435443
"name" => location.name,
436444
"ods_code" => location.ods_code,
445+
"phase" => nil,
437446
"position" => [location.position.x, location.position.y],
438447
"site" => location.site,
439448
"status" => "unknown",

0 commit comments

Comments
 (0)