Sprankelprachtig aan/afmeldsysteem

person.rb 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # A person represents a human being. A Person may be a Member in one or more
  2. # Groups, and be a Participant in any number of events of those Groups.
  3. # A Person may access the system by creating a User, and may have at most one
  4. # User.
  5. class Person < ApplicationRecord
  6. # @!attribute first_name
  7. # @return [String]
  8. # the person's first name. ('Vincent' in 'Vincent van Gogh'.)
  9. #
  10. # @!attribute infix
  11. # @return [String]
  12. # the part of a person's surname that is not taken into account when
  13. # sorting by surname. ('van' in 'Vincent van Gogh'.)
  14. #
  15. # @!attribute last_name
  16. # @return [String]
  17. # the person's surname. ('Gogh' in 'Vincent van Gogh'.)
  18. #
  19. # @!attribute birth_date
  20. # @return [Date]
  21. # the person's birth date.
  22. #
  23. # @!attribute email
  24. # @return [String]
  25. # the person's email address.
  26. #
  27. # @!attribute is_admin
  28. # @return [Boolean]
  29. # whether or not the person has administrative rights.
  30. has_one :user
  31. has_many :members,
  32. dependent: :destroy
  33. has_many :participants,
  34. dependent: :destroy
  35. has_many :groups, through: :members
  36. has_many :activities, through: :participants
  37. validates :email, presence: true, uniqueness: true
  38. validates :first_name, presence: true
  39. validates :last_name, presence: true
  40. validate :birth_date_cannot_be_in_future
  41. before_validation :not_admin_if_nil
  42. before_save :update_user_email, if: :email_changed?
  43. # The person's full name.
  44. def full_name
  45. if self.infix
  46. [self.first_name, self.infix, self.last_name].join(' ')
  47. else
  48. [self.first_name, self.last_name].join(' ')
  49. end
  50. end
  51. # The person's reversed name, to sort by surname.
  52. def reversed_name
  53. if self.infix
  54. [self.last_name, self.infix, self.first_name].join(', ')
  55. else
  56. [self.last_name, self.first_name].join(', ')
  57. end
  58. end
  59. # All activities where this person is an organizer.
  60. def organized_activities
  61. self.participants.includes(:activity).where(is_leader: true)
  62. end
  63. private
  64. # Assert that the person's birth date, if any, lies in the past.
  65. def birth_date_cannot_be_in_future
  66. if self.birth_date && self.birth_date > Date.today
  67. errors.add(:birth_date, "can't be in the future.")
  68. end
  69. end
  70. # Explicitly force nil to false in the admin field.
  71. def not_admin_if_nil
  72. self.is_admin ||= false
  73. end
  74. # Ensure the person's user email is updated at the same time as the person's
  75. # email.
  76. def update_user_email
  77. if not self.user.nil?
  78. self.user.update!(email: self.email)
  79. end
  80. end
  81. end