class Location
Schema Information
Table name: locations
id :bigint not null, primary key
address_line_1 :text
address_line_2 :text
address_postcode :text
address_town :text
gias_establishment_number :integer
gias_local_authority_code :integer
gias_phase :integer
gias_year_groups :integer default([]), not null, is an Array
name :text not null
ods_code :string
position :geography point, 4326
site :string
status :integer default("unknown"), not null
systm_one_code :string
type :integer not null
url :text
urn :string
created_at :datetime not null
updated_at :datetime not null
Indexes
index_locations_on_ods_code (ods_code) UNIQUE index_locations_on_systm_one_code (systm_one_code) UNIQUE index_locations_on_urn (urn) UNIQUE WHERE ((type = 0) AND (site IS NULL)) index_locations_on_urn_and_site (urn,site) UNIQUE
Constants
- GIAS_PHASE_MAPPINGS
- URN_HOME_EDUCATED
- URN_UNKNOWN
Public Class Methods
Source
# File app/models/location.rb, line 208 def self.find_by_urn_and_site(urn_and_site) where_urn_and_site(urn_and_site).take end
Source
# File app/models/location.rb, line 212 def self.find_by_urn_and_site!(urn_and_site) where_urn_and_site(urn_and_site).take! end
Public Instance Methods
Source
# File app/models/location.rb, line 250 def as_json super.except( "created_at", "systm_one_code", "updated_at", "position" ).merge( "is_attached_to_team" => team_locations.any? { it.academic_year == AcademicYear.pending }, "position" => position ? [position.x, position.y] : nil ) end
Calls superclass method
Source
# File app/models/location.rb, line 267 def attach_to_team!(team, academic_year:, subteam: nil) team_locations .find_or_initialize_by(team:, academic_year:) .tap { it.update!(subteam:) } end
Source
# File app/models/location.rb, line 234 def clinic? = generic_clinic? || community_clinic? def school? = gias_school? || generic_school? def dfe_number "#{gias_local_authority_code}#{gias_establishment_number}" if gias_school? end def phase if gias_phase GIAS_PHASE_MAPPINGS .find { |_, values| values.include?(gias_phase) } &.first end end def as_json super.except( "created_at", "systm_one_code", "updated_at", "position" ).merge( "is_attached_to_team" => team_locations.any? { it.academic_year == AcademicYear.pending }, "position" => position ? [position.x, position.y] : nil ) end def teams_for_academic_year(academic_year) team_locations.where(academic_year:).includes(:team).map(&:team) end def attach_to_team!(team, academic_year:, subteam: nil) team_locations .find_or_initialize_by(team:, academic_year:) .tap { it.update!(subteam:) } end def import_year_groups!(values, academic_year:, source:) Location::YearGroup.import!( %i[location_id academic_year value source], values.map { |value| [id, academic_year, value, source] }, on_duplicate_key_ignore: true ) end def import_year_groups_from_gias!(academic_year:) import_year_groups!(gias_year_groups, academic_year:, source: "gias") end def import_default_programme_year_groups!(programmes, academic_year:) year_group_ids = location_year_groups.where(academic_year:).pluck(:value, :id).to_h rows = programmes.flat_map do |programme| programme.default_year_groups.filter_map do |year_group| if (year_group_id = year_group_ids[year_group]) [year_group_id, programme.type] end end end Location::ProgrammeYearGroup.import!( %i[location_year_group_id programme_type], rows, on_duplicate_key_ignore: true ) end private def organisation_ods_codes = Organisation.pluck(:ods_code) GIAS_PHASE_MAPPINGS = { "nursery" => %w[nursery], "primary" => %w[primary middle_deemed_primary], "secondary" => %w[secondary middle_deemed_secondary], "other" => %w[sixteen_plus all_through not_applicable] }.freeze def fhir_mapper @fhir_mapper ||= FHIRMapper::Location.new(self)
Source
# File app/models/location.rb, line 238 def dfe_number "#{gias_local_authority_code}#{gias_establishment_number}" if gias_school? end
Source
# File app/models/location.rb, line 316 def fhir_mapper @fhir_mapper ||= FHIRMapper::Location.new(self) end
Source
# File app/models/location.rb, line 285 def import_default_programme_year_groups!(programmes, academic_year:) year_group_ids = location_year_groups.where(academic_year:).pluck(:value, :id).to_h rows = programmes.flat_map do |programme| programme.default_year_groups.filter_map do |year_group| if (year_group_id = year_group_ids[year_group]) [year_group_id, programme.type] end end end Location::ProgrammeYearGroup.import!( %i[location_year_group_id programme_type], rows, on_duplicate_key_ignore: true ) end
Source
# File app/models/location.rb, line 273 def import_year_groups!(values, academic_year:, source:) Location::YearGroup.import!( %i[location_id academic_year value source], values.map { |value| [id, academic_year, value, source] }, on_duplicate_key_ignore: true ) end
Source
# File app/models/location.rb, line 281 def import_year_groups_from_gias!(academic_year:) import_year_groups!(gias_year_groups, academic_year:, source: "gias") end
Source
# File app/models/location.rb, line 307 def organisation_ods_codes = Organisation.pluck(:ods_code) GIAS_PHASE_MAPPINGS = { "nursery" => %w[nursery], "primary" => %w[primary middle_deemed_primary], "secondary" => %w[secondary middle_deemed_secondary], "other" => %w[sixteen_plus all_through not_applicable] }.freeze def fhir_mapper @fhir_mapper ||= FHIRMapper::Location.new(self) end end
Source
# File app/models/location.rb, line 242 def phase if gias_phase GIAS_PHASE_MAPPINGS .find { |_, values| values.include?(gias_phase) } &.first end end
Source
# File app/models/location.rb, line 230 def programmes location_programme_year_groups.map(&:programme).sort.uniq end
Source
# File app/models/location.rb, line 236 def school? = gias_school? || generic_school? def dfe_number "#{gias_local_authority_code}#{gias_establishment_number}" if gias_school? end def phase if gias_phase GIAS_PHASE_MAPPINGS .find { |_, values| values.include?(gias_phase) } &.first end end def as_json super.except( "created_at", "systm_one_code", "updated_at", "position" ).merge( "is_attached_to_team" => team_locations.any? { it.academic_year == AcademicYear.pending }, "position" => position ? [position.x, position.y] : nil ) end def teams_for_academic_year(academic_year) team_locations.where(academic_year:).includes(:team).map(&:team) end def attach_to_team!(team, academic_year:, subteam: nil) team_locations .find_or_initialize_by(team:, academic_year:) .tap { it.update!(subteam:) } end def import_year_groups!(values, academic_year:, source:) Location::YearGroup.import!( %i[location_id academic_year value source], values.map { |value| [id, academic_year, value, source] }, on_duplicate_key_ignore: true ) end def import_year_groups_from_gias!(academic_year:) import_year_groups!(gias_year_groups, academic_year:, source: "gias") end def import_default_programme_year_groups!(programmes, academic_year:) year_group_ids = location_year_groups.where(academic_year:).pluck(:value, :id).to_h rows = programmes.flat_map do |programme| programme.default_year_groups.filter_map do |year_group| if (year_group_id = year_group_ids[year_group]) [year_group_id, programme.type] end end end Location::ProgrammeYearGroup.import!( %i[location_year_group_id programme_type], rows, on_duplicate_key_ignore: true ) end private def organisation_ods_codes = Organisation.pluck(:ods_code) GIAS_PHASE_MAPPINGS = { "nursery" => %w[nursery], "primary" => %w[primary middle_deemed_primary], "secondary" => %w[secondary middle_deemed_secondary], "other" => %w[sixteen_plus all_through not_applicable] }.freeze def fhir_mapper @fhir_mapper ||= FHIRMapper::Location.new(self) end
Source
# File app/models/location.rb, line 263 def teams_for_academic_year(academic_year) team_locations.where(academic_year:).includes(:team).map(&:team) end
Source
# File app/models/location.rb, line 216 def to_param # ODS code and URN+site are uniquely indexed. ods_code || urn_and_site || id end
Source
# File app/models/location.rb, line 221 def urn_and_site return nil if urn.nil? && site.nil? site.nil? ? urn : urn + site end
Source
# File app/models/location.rb, line 226 def year_groups @year_groups ||= location_year_groups.pluck_values end