Sprankelprachtig aan/afmeldsysteem

participant.rb 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # A Participant represents the many-to-many relation between People and
  2. # Activities, and contains the information on whether or not the person plans
  3. # to be present at the activity.
  4. class Participant < ApplicationRecord
  5. # @!attribute is_organizer
  6. # @return [Boolean]
  7. # whether the person is an organizer for this event.
  8. #
  9. # @!attribute attending
  10. # @return [Boolean]
  11. # whether or not the person plans to attend the activity.
  12. #
  13. # @!attribute notes
  14. # @return [String]
  15. # a short text indicating any irregularities, such as arriving later or
  16. # leaving earlier.
  17. belongs_to :person
  18. belongs_to :activity
  19. belongs_to :subgroup, optional: true
  20. after_validation :clear_subgroup, if: 'self.attending != true'
  21. validates :person_id,
  22. uniqueness: {
  23. scope: :activity_id,
  24. message: I18n.t('activities.errors.already_in')
  25. }
  26. HUMAN_ATTENDING = {
  27. true => I18n.t('activities.state.present'),
  28. false => I18n.t('activities.state.absent'),
  29. nil => I18n.t('activities.state.unknown')
  30. }
  31. # @return [String]
  32. # the name for the Participant's current state in the current locale.
  33. def human_attending
  34. HUMAN_ATTENDING[self.attending]
  35. end
  36. # TODO: Move to a more appropriate place
  37. # @return [String]
  38. # the class for a row containing this activity.
  39. def row_class
  40. if self.attending
  41. "success"
  42. elsif self.attending == false
  43. "danger"
  44. else
  45. "warning"
  46. end
  47. end
  48. def may_change?(person)
  49. self.activity.may_change?(person) ||
  50. self.person == person
  51. end
  52. # Set attending to true if nil, and notify the Person via email.
  53. def send_reminder
  54. return unless self.attending.nil?
  55. self.attending = true
  56. notes = self.notes || ""
  57. notes << '[auto]'
  58. self.notes = notes
  59. self.save
  60. return unless self.person.send_attendance_reminder
  61. ParticipantMailer.attendance_reminder(self.person, self.activity).deliver_later
  62. end
  63. # Send subgroup information email if person is attending.
  64. def send_subgroup_notification
  65. return unless self.attending && self.subgroup
  66. ParticipantMailer.subgroup_notification(self.person, self.activity, self).deliver_later
  67. end
  68. # Clear subgroup if person is set to 'not attending'.
  69. def clear_subgroup
  70. self.subgroup = nil
  71. end
  72. end