Sprankelprachtig aan/afmeldsysteem

person.rb 646B

1234567891011121314151617181920212223242526272829
  1. class Person < ApplicationRecord
  2. has_one :user
  3. validates :email, presence: true, uniqueness: true
  4. validates :first_name, presence: true
  5. validates :last_name, presence: true
  6. validates :is_admin, presence: true
  7. validate :birth_date_cannot_be_in_future
  8. before_validation :not_admin_if_nil
  9. before_save :update_user_email, if: :email_changed?
  10. private
  11. def birth_date_cannot_be_in_future
  12. if self.birth_date > Date.today
  13. errors.add(:birth_date, "can't be in the future.")
  14. end
  15. end
  16. def not_admin_if_nil
  17. self.is_admin ||= false
  18. end
  19. def update_user_email
  20. self.user.update!(email: self.email)
  21. end
  22. end