@@ -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
0 commit comments