app/models/school.rb in scidea-schools-1.0.5 vs app/models/school.rb in scidea-schools-1.0.6
- old
+ new
@@ -12,10 +12,12 @@
validates_presence_of :zipcode
validates_presence_of :phone
validate :zipcode_formatting
validate :phone_formatting
+
+ before_destroy :do_not_delete_if_dependencies
ZIPCODE_PATTERN = /^[\d]{5}$/
# Class methods
@@ -86,8 +88,16 @@
errors[:phone] << "must be at least 10 digits long" unless self.phone.nil? || self.phone.empty? || self.phone.size >= 10
end
def zipcode_formatting
errors.add(:zipcode, "must be five numbers") unless zipcode && zipcode.match(ZIPCODE_PATTERN)
+ end
+
+ def do_not_delete_if_dependencies
+ # if any users have this school, do not delete it
+ if self.profiles.count > 0
+ self.errors.add(:base, "Educational institution #{self.name} was not deleted because it is tied to user profiles. Before deleting #{self.name} use the migration feature to migrate these users to other educational institutions.")
+ return false
+ end
end
end