Skip to content

Commit 009028b

Browse files
committed
feat: add School Roll Number field for Ireland schools Add optional school_roll_number field with alphanumeric validation, following the same pattern as UK URN and US district fields. Includes database migration, API endpoints, admin dashboard integration, and comprehensive test coverage.
1 parent 51654a5 commit 009028b

File tree

8 files changed

+75
-1
lines changed

8 files changed

+75
-1
lines changed

app/controllers/api/schools_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def school_params
5656
:reference,
5757
:district_name,
5858
:district_nces_id,
59+
:school_roll_number,
5960
:address_line_1,
6061
:address_line_2,
6162
:municipality,

app/dashboards/school_dashboard.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SchoolDashboard < Administrate::BaseDashboard
2828
reference: Field::String,
2929
district_name: Field::String,
3030
district_nces_id: Field::String,
31+
school_roll_number: Field::String,
3132
verified_at: Field::DateTime,
3233
rejected_at: Field::DateTime,
3334
created_at: Field::DateTime,
@@ -62,6 +63,7 @@ class SchoolDashboard < Administrate::BaseDashboard
6263
reference
6364
district_name
6465
district_nces_id
66+
school_roll_number
6567
website
6668
address_line_1
6769
address_line_2
@@ -86,6 +88,7 @@ class SchoolDashboard < Administrate::BaseDashboard
8688
reference
8789
district_name
8890
district_nces_id
91+
school_roll_number
8992
website
9093
address_line_1
9194
address_line_2

app/models/school.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class School < ApplicationRecord
1818
validates :country_code, presence: true, inclusion: { in: ISO3166::Country.codes }
1919
validates :reference, uniqueness: { case_sensitive: false, allow_nil: true }, presence: false
2020
validates :district_nces_id, uniqueness: { case_sensitive: false, allow_nil: true }, presence: false
21+
validates :school_roll_number,
22+
uniqueness: { case_sensitive: false, allow_nil: true },
23+
presence: false,
24+
format: { with: /\A[0-9]+[A-Z]+\z/, allow_nil: true, message: 'must be alphanumeric (e.g., 01572D)' }
2125
validates :creator_id, presence: true, uniqueness: true
2226
validates :creator_agree_authority, presence: true, acceptance: true
2327
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
@@ -34,6 +38,7 @@ class School < ApplicationRecord
3438

3539
before_validation :normalize_reference
3640
before_validation :normalize_district_fields
41+
before_validation :normalize_school_roll_number
3742

3843
before_save :format_uk_postal_code, if: :should_format_uk_postal_code?
3944

@@ -102,6 +107,11 @@ def normalize_district_fields
102107
self.district_nces_id = nil if district_nces_id.blank?
103108
end
104109

110+
# Ensure the school_roll_number is nil, not an empty string
111+
def normalize_school_roll_number
112+
self.school_roll_number = nil if school_roll_number.blank?
113+
end
114+
105115
def verified_at_cannot_be_changed
106116
errors.add(:verified_at, 'cannot be changed after verification') if verified_at_was.present? && verified_at_changed?
107117
end

app/views/api/schools/_school.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ json.call(
88
:reference,
99
:district_name,
1010
:district_nces_id,
11+
:school_roll_number,
1112
:address_line_1,
1213
:address_line_2,
1314
:municipality,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddSchoolRollNumberToSchools < ActiveRecord::Migration[7.2]
2+
def change
3+
add_column :schools, :school_roll_number, :string
4+
5+
add_index :schools, :school_roll_number, unique: true
6+
end
7+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/school.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
address_line_1 { 'Address Line 1' }
88
municipality { 'Greater London' }
99
country_code { 'GB' }
10+
school_roll_number { nil }
1011
creator_id { SecureRandom.uuid }
1112
creator_agree_authority { true }
1213
creator_agree_terms_and_conditions { true }

spec/models/school_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,55 @@
147147
expect(duplicate_school).not_to be_valid
148148
end
149149

150+
it 'does not require a school_roll_number' do
151+
create(:school, id: SecureRandom.uuid, school_roll_number: nil)
152+
153+
school.school_roll_number = nil
154+
expect(school).to be_valid
155+
end
156+
157+
it 'requires school_roll_number to be unique if provided' do
158+
school.school_roll_number = '01572D'
159+
school.save!
160+
161+
duplicate_school = build(:school, school_roll_number: '01572d')
162+
expect(duplicate_school).not_to be_valid
163+
end
164+
165+
it 'accepts a valid alphanumeric school_roll_number' do
166+
school.school_roll_number = '01572D'
167+
expect(school).to be_valid
168+
end
169+
170+
it 'accepts a school_roll_number with multiple letters' do
171+
school.school_roll_number = '12345ABC'
172+
expect(school).to be_valid
173+
end
174+
175+
it 'rejects a school_roll_number with only numbers' do
176+
school.school_roll_number = '01572'
177+
expect(school).not_to be_valid
178+
expect(school.errors[:school_roll_number]).to include('must be alphanumeric (e.g., 01572D)')
179+
end
180+
181+
it 'rejects a school_roll_number with only letters' do
182+
school.school_roll_number = 'ABCDE'
183+
expect(school).not_to be_valid
184+
expect(school.errors[:school_roll_number]).to include('must be alphanumeric (e.g., 01572D)')
185+
end
186+
187+
it 'rejects a school_roll_number with special characters' do
188+
school.school_roll_number = '01572-D'
189+
expect(school).not_to be_valid
190+
expect(school.errors[:school_roll_number]).to include('must be alphanumeric (e.g., 01572D)')
191+
end
192+
193+
it 'normalizes blank school_roll_number to nil' do
194+
school.school_roll_number = ' '
195+
school.save
196+
expect(school.school_roll_number).to be_nil
197+
end
198+
150199
it 'requires an address_line_1' do
151200
school.address_line_1 = ' '
152201
expect(school).not_to be_valid

0 commit comments

Comments
 (0)