Sprankelprachtig aan/afmeldsysteem

person.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, 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_organizer: true)
  62. end
  63. # Create multiple Persons from data found in a csv file, return those.
  64. def self.from_csv(content)
  65. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  66. result = []
  67. reader.each do |row|
  68. p = Person.find_by(email: row['email'])
  69. if not p
  70. p = Person.new
  71. p.first_name = row['first_name']
  72. p.infix = row['infix']
  73. p.last_name = row['last_name']
  74. p.email = row['email']
  75. p.birth_date = Date.strptime(row['birth_date']) unless row['birth_date'].blank?
  76. p.save!
  77. end
  78. result << p
  79. end
  80. return result
  81. end
  82. private
  83. # Assert that the person's birth date, if any, lies in the past.
  84. def birth_date_cannot_be_in_future
  85. if self.birth_date && self.birth_date > Date.today
  86. errors.add(:birth_date, I18n.t('person.errors.cannot_future'))
  87. end
  88. end
  89. # Explicitly force nil to false in the admin field.
  90. def not_admin_if_nil
  91. self.is_admin ||= false
  92. end
  93. # Ensure the person's user email is updated at the same time as the person's
  94. # email.
  95. def update_user_email
  96. if not self.user.nil?
  97. self.user.update!(email: self.email)
  98. end
  99. end
  100. end