Sprankelprachtig aan/afmeldsysteem

person.rb 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class Person < ApplicationRecord
  2. has_one :user
  3. has_many :members
  4. has_many :participants
  5. has_many :groups, through: :members
  6. has_many :activities, through: :participants
  7. validates :email, presence: true, uniqueness: true
  8. validates :first_name, presence: true
  9. validates :last_name, presence: true
  10. validate :birth_date_cannot_be_in_future
  11. before_validation :not_admin_if_nil
  12. before_save :update_user_email, if: :email_changed?
  13. def full_name
  14. if self.infix
  15. [self.first_name, self.infix, self.last_name].join(' ')
  16. else
  17. [self.first_name, self.last_name].join(' ')
  18. end
  19. end
  20. def reversed_name
  21. if self.infix
  22. [self.last_name, self.infix, self.first_name].join(', ')
  23. else
  24. [self.last_name, self.first_name].join(', ')
  25. end
  26. end
  27. private
  28. def birth_date_cannot_be_in_future
  29. if self.birth_date && self.birth_date > Date.today
  30. errors.add(:birth_date, "can't be in the future.")
  31. end
  32. end
  33. def not_admin_if_nil
  34. self.is_admin ||= false
  35. end
  36. def update_user_email
  37. if not self.user.nil?
  38. self.user.update!(email: self.email)
  39. end
  40. end
  41. end