@@ -15,13 +15,22 @@ class School < ApplicationRecord
1515 validates :website , presence : true , format : { with : VALID_URL_REGEX , message : I18n . t ( 'validations.school.website' ) }
1616 validates :address_line_1 , presence : true
1717 validates :municipality , presence : true
18+ validates :administrative_area , presence : true
19+ validates :postal_code , presence : true
1820 validates :country_code , presence : true , inclusion : { in : ISO3166 ::Country . codes }
19- validates :reference , uniqueness : { case_sensitive : false , allow_nil : true } , presence : false
20- validates :district_nces_id , uniqueness : { case_sensitive : false , allow_nil : true } , presence : false
21+ validates :reference ,
22+ uniqueness : { conditions : -> { where ( rejected_at : nil ) } , case_sensitive : false , allow_blank : true , message : I18n . t ( 'validations.school.reference_urn_exists' ) } ,
23+ format : { with : /\A \d {5,6}\z / , allow_nil : true , message : I18n . t ( 'validations.school.reference' ) } ,
24+ if : :united_kingdom?
25+ validates :district_nces_id ,
26+ format : { with : /\A \d {7}\z / , allow_nil : true , message : I18n . t ( 'validations.school.district_nces_id' ) } ,
27+ if : :united_states?
28+ validates :district_name , presence : true , if : :united_states?
2129 validates :school_roll_number ,
22- uniqueness : { conditions : -> { where ( rejected_at : nil ) } , case_sensitive : false , allow_nil : true } ,
23- presence : { if : :ireland? } ,
24- format : { with : /\A [0-9]+[A-Z]+\z / , allow_nil : true , message : I18n . t ( 'validations.school.school_roll_number' ) }
30+ uniqueness : { conditions : -> { where ( rejected_at : nil ) } , case_sensitive : false , allow_blank : true , message : I18n . t ( 'validations.school.school_roll_number_exists' ) } ,
31+ format : { with : /\A [0-9]+[A-Z]+\z / , allow_nil : true , message : I18n . t ( 'validations.school.school_roll_number' ) } ,
32+ presence : true ,
33+ if : :ireland?
2534 validates :creator_id , presence : true , uniqueness : true
2635 validates :creator_agree_authority , presence : true , acceptance : true
2736 validates :creator_agree_terms_and_conditions , presence : true , acceptance : true
@@ -30,8 +39,6 @@ class School < ApplicationRecord
3039 validates :verified_at , absence : { if : proc { |school | school . rejected? } }
3140 validates :code ,
3241 uniqueness : { allow_nil : true } ,
33- presence : { if : proc { |school | school . verified? } } ,
34- absence : { unless : proc { |school | school . verified? } } ,
3542 format : { with : /\d \d -\d \d -\d \d / , allow_nil : true }
3643 validate :verified_at_cannot_be_changed
3744 validate :code_cannot_be_changed
@@ -42,6 +49,9 @@ class School < ApplicationRecord
4249
4350 before_save :format_uk_postal_code , if : :should_format_uk_postal_code?
4451
52+ # TODO: Remove the conditional once the feature flag is retired
53+ after_create :generate_code! , if : -> { FeatureFlags . immediate_school_onboarding? }
54+
4555 def self . find_for_user! ( user )
4656 school = Role . find_by ( user_id : user . id ) &.school || find_by ( creator_id : user . id )
4757 raise ActiveRecord ::RecordNotFound unless school
@@ -62,9 +72,19 @@ def rejected?
6272 end
6373
6474 def verify!
75+ # TODO: Remove this line once the feature flag is retired
76+ generate_code! unless FeatureFlags . immediate_school_onboarding?
77+
78+ update! ( verified_at : Time . zone . now )
79+ end
80+
81+ def generate_code!
82+ return code if code . present?
83+
6584 attempts = 0
6685 begin
67- update! ( verified_at : Time . zone . now , code : ForEducationCodeGenerator . generate )
86+ new_code = ForEducationCodeGenerator . generate
87+ update! ( code : new_code )
6888 rescue ActiveRecord ::RecordInvalid => e
6989 raise unless e . record . errors [ :code ] . include? ( 'has already been taken' ) && attempts <= 5
7090
@@ -78,6 +98,8 @@ def reject
7898 end
7999
80100 def reopen
101+ return false unless rejected?
102+
81103 update ( rejected_at : nil )
82104 end
83105
@@ -122,13 +144,21 @@ def rejected_at_cannot_be_changed
122144 end
123145
124146 def code_cannot_be_changed
125- errors . add ( :code , 'cannot be changed after verification ' ) if code_was . present? && code_changed?
147+ errors . add ( :code , 'cannot be changed after onboarding ' ) if code_was . present? && code_changed?
126148 end
127149
128150 def should_format_uk_postal_code?
129151 country_code == 'GB' && postal_code . to_s . length >= 5
130152 end
131153
154+ def united_kingdom?
155+ country_code == 'GB'
156+ end
157+
158+ def united_states?
159+ country_code == 'US'
160+ end
161+
132162 def ireland?
133163 country_code == 'IE'
134164 end
0 commit comments