Sprankelprachtig aan/afmeldsysteem

person.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 calendar_token
  28. # @return [String]
  29. # the calendar token that can be used to open this Person's events as an
  30. # ICAL file.
  31. #
  32. # @!attribute is_admin
  33. # @return [Boolean]
  34. # whether or not the person has administrative rights.
  35. has_one :user
  36. has_many :members,
  37. dependent: :destroy
  38. has_many :participants,
  39. dependent: :destroy
  40. has_many :groups, through: :members
  41. has_many :activities, through: :participants
  42. has_secure_token :calendar_token
  43. validates :email, uniqueness: true
  44. validates :first_name, presence: true
  45. validates :last_name, presence: true
  46. validate :birth_date_cannot_be_in_future
  47. before_validation :not_admin_if_nil
  48. before_save :update_user_email, if: :email_changed?
  49. # The person's full name.
  50. def full_name
  51. if self.infix
  52. [self.first_name, self.infix, self.last_name].join(' ')
  53. else
  54. [self.first_name, self.last_name].join(' ')
  55. end
  56. end
  57. # The person's reversed name, to sort by surname.
  58. def reversed_name
  59. if self.infix
  60. [self.last_name, self.infix, self.first_name].join(', ')
  61. else
  62. [self.last_name, self.first_name].join(', ')
  63. end
  64. end
  65. # All activities where this person is an organizer.
  66. def organized_activities
  67. self.participants.includes(:activity).where(is_organizer: true)
  68. end
  69. # Create multiple Persons from data found in a csv file, return those.
  70. def self.from_csv(content)
  71. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  72. result = []
  73. reader.each do |row|
  74. p = Person.find_by(email: row['email'])
  75. if not p
  76. p = Person.new
  77. p.first_name = row['first_name']
  78. p.infix = row['infix']
  79. p.last_name = row['last_name']
  80. p.email = row['email']
  81. p.birth_date = Date.strptime(row['birth_date']) unless row['birth_date'].blank?
  82. p.save!
  83. end
  84. result << p
  85. end
  86. return result
  87. end
  88. private
  89. # Assert that the person's birth date, if any, lies in the past.
  90. def birth_date_cannot_be_in_future
  91. if self.birth_date && self.birth_date > Date.today
  92. errors.add(:birth_date, I18n.t('person.errors.cannot_future'))
  93. end
  94. end
  95. # Explicitly force nil to false in the admin field.
  96. def not_admin_if_nil
  97. self.is_admin ||= false
  98. end
  99. # Ensure the person's user email is updated at the same time as the person's
  100. # email.
  101. def update_user_email
  102. if not self.user.nil?
  103. self.user.update!(email: self.email)
  104. end
  105. end
  106. end