Skip to content

Commit 9fc0f94

Browse files
committed
add school type validation and remove singularization of types
1 parent 66f4f4e commit 9fc0f94

13 files changed

Lines changed: 139 additions & 86 deletions

app/models/school.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ class School < Organisation
66

77
validates :urn, uniqueness: true
88

9+
ACADEMY_TYPE = "Academies".freeze
10+
LA_SCHOOL_TYPE = "Local authority maintained schools".freeze
11+
FREE_SCHOOL_TYPE = "Free Schools".freeze
12+
INDEPENDENT_SCHOOL_TYPE = "Independent schools".freeze
13+
VALID_SCHOOL_TYPES = [LA_SCHOOL_TYPE, INDEPENDENT_SCHOOL_TYPE, "Special schools", "Universities", ACADEMY_TYPE, FREE_SCHOOL_TYPE, "Welsh schools", "Other types", "Colleges", "Online provider"].freeze
14+
15+
# This is direct from GIAS (with plurals removed via singularize)
16+
validates :school_type, inclusion: { in: VALID_SCHOOL_TYPES }
17+
918
EXCLUDED_DETAILED_SCHOOL_TYPES = [
1019
"Further education",
1120
"Other independent school",
@@ -69,10 +78,6 @@ def catholic_school?
6978
religious_character&.include?("Catholic") || false
7079
end
7180

72-
def school_type
73-
read_attribute(:school_type).singularize
74-
end
75-
7681
def key_stages
7782
return if phase == "not_applicable"
7883

app/queries/vacancy_filter_query.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def add_organisation_type_filters(filters, built_scope)
3939
selected_school_types = []
4040

4141
if filters[:organisation_types].include?("Academy")
42-
selected_school_types.push("Academy", "Academies", "Free schools", "Free school")
42+
selected_school_types.push(School::ACADEMY_TYPE, School::FREE_SCHOOL_TYPE)
4343
end
4444

4545
if filters[:organisation_types].include?("Local authority maintained schools")
46-
selected_school_types << "Local authority maintained schools"
46+
selected_school_types << School::LA_SCHOOL_TYPE
4747
end
4848

4949
built_scope.joins(organisation_vacancies: :organisation).where(organisations: { school_type: selected_school_types }).distinct

app/services/gias/import_schools_and_local_authorities.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ def initialize
1010
reset_data
1111
end
1212

13+
class ImportFailure < StandardError
14+
end
15+
1316
def call
1417
log_benchmark("Importing schools and local authorities") do
15-
Gias::Data.new(SCHOOLS_AND_LOCAL_AUTHORITIES_CSV).each_with_index do |row, index|
16-
local_authorities.add(group_data(row))
17-
schools.push(school_data(row))
18-
memberships.push(membership_data(row))
19-
20-
import_batch if (index % BATCH_SIZE).zero?
18+
import_errors = Gias::Data.new(SCHOOLS_AND_LOCAL_AUTHORITIES_CSV).each_slice(BATCH_SIZE).flat_map do |group|
19+
group.each do |row|
20+
local_authorities.add(group_data(row))
21+
schools.push(school_data(row))
22+
memberships.push(membership_data(row))
23+
end
24+
import_batch
2125
end
22-
23-
import_batch
26+
raise ImportFailure, import_errors.map(&:errors) if import_errors.any?
2427
end
2528
end
2629

@@ -35,11 +38,12 @@ def reset_data
3538
end
3639

3740
def import_batch
38-
import_local_authorities if local_authorities.any?
39-
import_schools if schools.any?
40-
import_memberships if memberships.any?
41-
42-
reset_data
41+
failures = import_local_authorities.failed_instances +
42+
import_schools.failed_instances +
43+
import_memberships.failed_instances
44+
failures.tap do
45+
reset_data
46+
end
4347
end
4448

4549
def import_local_authorities
@@ -59,7 +63,6 @@ def import_schools
5963
conflict_target: [:urn],
6064
columns: schools.first.keys,
6165
},
62-
batch_size: 1000,
6366
)
6467
end
6568

app/services/search/school_search.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def apply_organisation_type_filter(scope)
9494
selected_school_types = []
9595

9696
if organisation_types.include?("Academy")
97-
selected_school_types.push("Academy", "Academies", "Free schools", "Free school")
97+
selected_school_types.push(School::ACADEMY_TYPE, School::FREE_SCHOOL_TYPE)
9898
end
9999

100100
if organisation_types.include?("Local authority maintained schools")
101-
selected_school_types << "Local authority maintained schools"
101+
selected_school_types << School::LA_SCHOOL_TYPE
102102
end
103103

104104
scope.where(school_type: selected_school_types)

db/seeds.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
# Church of England school
2222
osmaston_cofe = School.find_by!(urn: "112847")
2323

24+
# academy special converter
25+
oakwoods = School.find_by!(urn: "139468")
26+
# community special school
27+
northcott = School.find_by!(urn: "118138")
28+
# Special free school
29+
martinbacon = School.find_by!(urn: "147661")
30+
2431
# Team users
2532
users = [
2633
{ email: "alex.lee@education.gov.uk", family_name: "Lee", given_name: "Alex" },
@@ -48,6 +55,9 @@
4855
southampton_la.schools.detect { |s| s.phase != "not_applicable" && s.phase.exclude?("middle") },
4956
abraham_moss,
5057
st_anthony,
58+
oakwoods,
59+
northcott,
60+
martinbacon,
5161
osmaston_cofe]
5262

5363
user_emails = users.map { |u| u.fetch(:email) }
@@ -65,14 +75,14 @@
6575

6676
schools.each do |school|
6777
attrs = { organisations: [school],
68-
phases: [school.phase],
78+
phases: (school.phase == "not_applicable" ? %w[secondary] : [school.phase]),
6979
publisher_organisation: school,
7080
publisher: Publisher.all.sample }
71-
3.times { FactoryBot.create(:vacancy, :for_seed_data, **attrs) }
81+
2.times { FactoryBot.create(:vacancy, :for_seed_data, **attrs) }
7282
FactoryBot.create(:vacancy, :for_seed_data, :no_tv_applications, **attrs)
73-
2.times { FactoryBot.create(:vacancy, :for_seed_data, :future_publish, **attrs) }
83+
FactoryBot.create(:vacancy, :for_seed_data, :future_publish, **attrs)
7484
FactoryBot.create(:draft_vacancy, :for_seed_data, **attrs)
75-
2.times { FactoryBot.build(:vacancy, :for_seed_data, :expired, **attrs).save(validate: false) }
85+
FactoryBot.build(:vacancy, :for_seed_data, :expired, **attrs).save(validate: false)
7686
end
7787

7888
# Vacancies at Weydon trust central office

spec/factories/schools.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
region { "South-East England" }
3535
safeguarding_information { Faker::Lorem.paragraph(sentence_count: 1) }
3636
sequence(:slug) { |n| "#{name.parameterize}-#{n}" }
37-
school_type { "LA maintained school" }
37+
school_type { School::INDEPENDENT_SCHOOL_TYPE }
3838
postcode { FFaker::AddressUK.postcode }
3939
town { Faker::Address.city.delete("'") }
4040
# URN is validated unique for a school
@@ -101,7 +101,11 @@
101101
end
102102
end
103103

104+
trait :free_school do
105+
school_type { School::FREE_SCHOOL_TYPE }
106+
end
107+
104108
factory :academy, parent: :school do
105-
school_type { "Academy" }
109+
school_type { School::ACADEMY_TYPE }
106110
end
107111
end

0 commit comments

Comments
 (0)