Sprankelprachtig aan/afmeldsysteem

activity.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # An Activity represents a single continuous event that the members of a group may attend.
  2. # An Activity belongs to a group, and has many participants.
  3. class Activity < ApplicationRecord
  4. # @!attribute name
  5. # @return [String]
  6. # a short name for the activity.
  7. #
  8. # @!attribute description
  9. # @return [String]
  10. # a short text describing the activity. This text is always visible to
  11. # all users.
  12. #
  13. # @!attribute location
  14. # @return [String]
  15. # a short text describing where the activity will take place. Always
  16. # visible to all participants.
  17. #
  18. # @!attribute start
  19. # @return [TimeWithZone]
  20. # when the activity starts.
  21. #
  22. # @!attribute end
  23. # @return [TimeWithZone]
  24. # when the activity ends.
  25. #
  26. # @!attribute deadline
  27. # @return [TimeWithZone]
  28. # when the normal participants (everyone who isn't an organizer or group
  29. # leader) may not change their own attendance anymore. Disabled if set to
  30. # nil.
  31. belongs_to :group
  32. has_many :participants,
  33. dependent: :destroy
  34. has_many :people, through: :participants
  35. validates :name, presence: true
  36. validates :start, presence: true
  37. validate :deadline_before_start, unless: "self.deadline.blank?"
  38. validate :end_after_start, unless: "self.end.blank?"
  39. after_create :create_missing_participants!
  40. # Get all people (not participants) that are organizers. Does not include
  41. # group leaders, although they may modify the activity as well.
  42. def organizers
  43. self.participants.includes(:person).where(is_organizer: true)
  44. end
  45. # Determine whether the passed Person participates in the activity.
  46. def is_participant?(person)
  47. Participant.exists?(
  48. activity_id: self.id,
  49. person_id: person.id
  50. )
  51. end
  52. # Determine whether the passed Person is an organizer for the activity.
  53. def is_organizer?(person)
  54. Participant.exists?(
  55. person_id: person.id,
  56. activity_id: self.id,
  57. is_organizer: true
  58. )
  59. end
  60. # Query the database to determine the amount of participants that are present/absent/unknown
  61. def state_counts
  62. self.participants.group(:attending).count
  63. end
  64. # Determine whether the passed Person may change this activity.
  65. def may_change?(person)
  66. person.is_admin ||
  67. self.is_organizer?(person) ||
  68. self.group.is_leader?(person)
  69. end
  70. # Create Participants for all People that
  71. # 1. are members of the group
  72. # 2. do not have Participants (and thus, no way to confirm) yet
  73. def create_missing_participants!
  74. people = self.group.people
  75. if not self.participants.empty?
  76. people = people.where('people.id NOT IN (?)', self.people.ids)
  77. end
  78. people.each do |p|
  79. Participant.create(
  80. activity: self,
  81. person: p,
  82. )
  83. end
  84. end
  85. private
  86. # Assert that the deadline for participants to change the deadline, if any,
  87. # is set before the event starts.
  88. def deadline_before_start
  89. if self.deadline > self.start
  90. errors.add(:deadline, 'must be before start')
  91. end
  92. end
  93. # Assert that the activity's end, if any, occurs after the event's start.
  94. def end_after_start
  95. if self.end < self.start
  96. errors.add(:end, 'must be after start')
  97. end
  98. end
  99. end