Sprankelprachtig aan/afmeldsysteem

activity.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 public_name
  5. # @return [String]
  6. # the name that users will see if there is no {#secret_name}, or if
  7. # {#show_hidden} is `false`.
  8. #
  9. # @!attribute secret_name
  10. # @return [String]
  11. # a name that is (be default) only visible to organizers and group
  12. # leaders. Can be shown or hidden to normal participants using
  13. # {#show_hidden}.
  14. #
  15. # @!attribute description
  16. # @return [String]
  17. # a short text describing the activity. This text is always visible to
  18. # all users.
  19. #
  20. # @!attribute location
  21. # @return [String]
  22. # a short text describing where the activity will take place. Always
  23. # visible to all participants.
  24. #
  25. # @!attribute show_hidden
  26. # @return [Boolean]
  27. # Whether or not the 'secret' attributes can be viewed by the people who
  28. # aren't organizers or group leaders. Currently, only {#secret_name} is
  29. # influenced by this. More attributes may be added in the future, and
  30. # will be controlled by this toggle as well.
  31. #
  32. # @!attribute start
  33. # @return [TimeWithZone]
  34. # when the activity starts.
  35. #
  36. # @!attribute end
  37. # @return [TimeWithZone]
  38. # when the activity ends.
  39. #
  40. # @!attribute deadline
  41. # @return [TimeWithZone]
  42. # when the normal participants (everyone who isn't an organizer or group
  43. # leader) may not change their own attendance anymore. Disabled if set to
  44. # nil.
  45. belongs_to :group
  46. has_many :participants
  47. has_many :people, through: :participants
  48. validates :public_name, presence: true
  49. validates :start, presence: true
  50. validate :deadline_before_start, unless: "self.deadline.blank?"
  51. validate :end_after_start, unless: "self.end.blank?"
  52. # Get all people (not participants) that are organizers. Does not include
  53. # group leaders, although they may modify the activity as well.
  54. def organizers
  55. self.participants.includes(:person).where(is_organizer: true)
  56. end
  57. # Determine whether the passed Person participates in the activity.
  58. def is_participant?(person)
  59. Participant.exists?(
  60. activity_id: self.id,
  61. person_id: person.id
  62. )
  63. end
  64. # Determine whether the passed Person is an organizer for the activity.
  65. def is_organizer?(person)
  66. Participant.exists?(
  67. person_id: person.id,
  68. activity_id: self.id,
  69. is_organizer: true
  70. )
  71. end
  72. # Determine whether the passed Person may change this activity.
  73. def may_change?(person)
  74. person.is_admin ||
  75. self.is_organizer(person) ||
  76. self.group.is_leader(person)
  77. end
  78. private
  79. # Assert that the deadline for participants to change the deadline, if any,
  80. # is set before the event starts.
  81. def deadline_before_start
  82. if self.deadline > self.start
  83. errors.add(:deadline, 'must be before start')
  84. end
  85. end
  86. # Assert that the activity's end, if any, occurs after the event's start.
  87. def end_after_start
  88. if self.end < self.start
  89. errors.add(:end, 'must be after start')
  90. end
  91. end
  92. end