Skip to content

Commit 63898e1

Browse files
authored
Merge branch 'main' into add-properties-to-existing-responses
2 parents 50f261e + a7f49d2 commit 63898e1

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ Also avilable to override are: `SEEDING_TEACHER_ID`.
111111

112112
The `*-to-local.sh` scripts will backup the database in your local terminal, then run an instance of the Docker container and run commands to populate your development DB with that data - see [./bin/db-sync/load-local-db.sh](./bin/db-sync/load-local-db.sh)
113113

114+
### Testing
115+
116+
Run the entire test suite using:
117+
118+
```
119+
docker-compose run api rspec
120+
```
121+
122+
Or individual specs using:
123+
124+
```
125+
docker-compose run api rspec spec/path/to/spec.rb
126+
```
127+
114128
### CORS Allowed Origins
115129

116130
Add a comma separated list to the relevant enviroment settings. E.g for development in the `.env` file:

app/models/school.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class School < ApplicationRecord
3030

3131
before_validation :normalize_reference
3232

33+
before_save :format_uk_postal_code, if: :should_format_uk_postal_code?
34+
3335
def self.find_for_user!(user)
3436
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id)
3537
raise ActiveRecord::RecordNotFound unless school
@@ -65,6 +67,10 @@ def reject
6567
update(rejected_at: Time.zone.now)
6668
end
6769

70+
def postal_code=(str)
71+
super(str.to_s.upcase)
72+
end
73+
6874
private
6975

7076
# Ensure the reference is nil, not an empty string
@@ -83,4 +89,15 @@ def rejected_at_cannot_be_changed
8389
def code_cannot_be_changed
8490
errors.add(:code, 'cannot be changed after verification') if code_was.present? && code_changed?
8591
end
92+
93+
def should_format_uk_postal_code?
94+
country_code == 'GB' && postal_code.to_s.length >= 5
95+
end
96+
97+
def format_uk_postal_code
98+
cleaned_postal_code = postal_code.delete(' ')
99+
# insert a space as the third-from-last character in the postcode, eg. SW1A1AA -> SW1A 1AA
100+
# ensures UK postcodes are always formatted correctly (as the inward code is always 3 chars long)
101+
self.postal_code = "#{cleaned_postal_code[0..-4]} #{cleaned_postal_code[-3..]}"
102+
end
86103
end

spec/models/school_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,51 @@
331331
end
332332
end
333333

334+
describe '#format_uk_postal_code' do
335+
it 'retains correctly formatted UK postal_code' do
336+
school.country_code = 'GB'
337+
school.postal_code = 'SW1A 1AA'
338+
school.save
339+
expect(school.postal_code).to eq('SW1A 1AA')
340+
end
341+
342+
it 'corrects incorrectly formatted UK postal_code' do
343+
school.country_code = 'GB'
344+
school.postal_code = 'SW1 A1AA'
345+
expect { school.save }.to change(school, :postal_code).to('SW1A 1AA')
346+
end
347+
348+
it 'formats UK postal_code with 4 char outcode' do
349+
school.country_code = 'GB'
350+
school.postal_code = 'SW1A1AA'
351+
expect { school.save }.to change(school, :postal_code).to('SW1A 1AA')
352+
end
353+
354+
it 'formats UK postal_code with 3 char outcode' do
355+
school.country_code = 'GB'
356+
school.postal_code = 'SW11AA'
357+
expect { school.save }.to change(school, :postal_code).to('SW1 1AA')
358+
end
359+
360+
it 'formats UK postal_code with 2 char outcode' do
361+
school.country_code = 'GB'
362+
school.postal_code = 'SW1AA'
363+
expect { school.save }.to change(school, :postal_code).to('SW 1AA')
364+
end
365+
366+
it 'does not format UK postal_code for short / invalid codes' do
367+
school.country_code = 'GB'
368+
school.postal_code = 'SW1A'
369+
expect { school.save }.not_to change(school, :postal_code)
370+
end
371+
372+
it 'does not format postal_code for non-UK countries' do
373+
school.country_code = 'FR'
374+
school.postal_code = '123456'
375+
expect { school.save }.not_to change(school, :postal_code)
376+
end
377+
end
378+
334379
describe '#reject' do
335380
it 'sets rejected_at to the current time' do
336381
school.reject

0 commit comments

Comments
 (0)