@@ -16,12 +16,21 @@ class School < ApplicationRecord
1616 validates :address_line_1 , presence : true
1717 validates :municipality , presence : true
1818 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
19+ validates :reference ,
20+ uniqueness : { conditions : -> { where ( rejected_at : nil ) } , case_sensitive : false , allow_blank : true , message : I18n . t ( 'validations.school.reference_urn_exists' ) } ,
21+ format : { with : /\A \d {5,6}\z / , allow_nil : true , message : I18n . t ( 'validations.school.reference' ) } ,
22+ if : :united_kingdom?
23+ validates :district_nces_id ,
24+ uniqueness : { conditions : -> { where ( rejected_at : nil ) } , case_sensitive : false , allow_blank : true , message : I18n . t ( 'validations.school.district_nces_id_exists' ) } ,
25+ format : { with : /\A \d {12}\z / , allow_nil : true , message : I18n . t ( 'validations.school.district_nces_id' ) } ,
26+ presence : true ,
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
@@ -129,6 +138,14 @@ def should_format_uk_postal_code?
129138 country_code == 'GB' && postal_code . to_s . length >= 5
130139 end
131140
141+ def united_kingdom?
142+ country_code == 'GB'
143+ end
144+
145+ def united_states?
146+ country_code == 'US'
147+ end
148+
132149 def ireland?
133150 country_code == 'IE'
134151 end
0 commit comments