Sprankelprachtig aan/afmeldsysteem

participant.rb 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }.freeze
  31. # @return [String]
  32. # the name for the Participant's current state in the current locale.
  33. def human_attending
  34. HUMAN_ATTENDING[attending]
  35. end
  36. ICAL_ATTENDING = {
  37. true => 'ATTENDING',
  38. false => 'CANCELLED',
  39. nil => 'TENTATIVE'
  40. }.freeze
  41. # @return [String]
  42. # the ICal attending response.
  43. def ical_attending
  44. ICAL_ATTENDING[attending]
  45. end
  46. # TODO: Move to a more appropriate place
  47. # @return [String]
  48. # the class for a row containing this activity.
  49. def row_class
  50. if attending
  51. "success"
  52. elsif attending == false
  53. "danger"
  54. else
  55. "warning"
  56. end
  57. end
  58. def may_change?(person)
  59. activity.may_change?(person) ||
  60. self.person == person
  61. end
  62. # Set attending to true if nil, and notify the Person via email.
  63. def send_reminder
  64. return unless attending.nil?
  65. self.attending = activity.no_response_action
  66. notes = self.notes || ""
  67. notes << '[auto]'
  68. self.notes = notes
  69. save
  70. return unless person.send_attendance_reminder
  71. ParticipantMailer.attendance_reminder(person, activity).deliver_later
  72. end
  73. # Send subgroup information email if person is attending.
  74. def send_subgroup_notification
  75. return unless attending && subgroup
  76. ParticipantMailer.subgroup_notification(person, activity, self).deliver_later
  77. end
  78. # Clear subgroup if person is set to 'not attending'.
  79. def clear_subgroup
  80. self.subgroup = nil
  81. end
  82. end