Sprankelprachtig aan/afmeldsysteem

group.rb 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # A Group contains Members, which can organize and participate in Activities.
  2. # Some of the Members may be group leaders, with the ability to see all
  3. # information and add or remove members from the group.
  4. class Group < ApplicationRecord
  5. # @!attribute name
  6. # @return [String]
  7. # the name of the group. Must be unique across all groups.
  8. has_many :members,
  9. dependent: :destroy
  10. has_many :people, through: :members
  11. has_many :activities,
  12. dependent: :destroy
  13. has_many :default_subgroups,
  14. dependent: :destroy
  15. validates :name,
  16. presence: true,
  17. uniqueness: {
  18. case_sensitive: false
  19. }
  20. # @return [Array<Member>] the members in the group who are also group leaders.
  21. def leaders
  22. self.members.includes(:person).where(is_leader: true)
  23. end
  24. # @return [Array<Activity>] the activities that haven't started yet.
  25. def future_activities
  26. self.activities.where('start > ?', DateTime.now)
  27. end
  28. # @return [Array<Activity>]
  29. # the Activity/Activities that are the closest to the given point in time.
  30. # Logic is as follows:
  31. # - If one or more Activities start before and end after the given point
  32. # in time, these are returned.
  33. # - Additionally, the last 3 activities that ended are returned, as well
  34. # as any activities starting within the next 48 hours.
  35. def current_activities(reference = Time.zone.now)
  36. currently_active = self.activities
  37. .where('start < ?', reference)
  38. .where('end > ?', reference)
  39. previous = self.activities
  40. .where('end < ?', reference)
  41. .order(end: :desc)
  42. .limit(3)
  43. upcoming = self.activities
  44. .where('start > ?', reference)
  45. .where('start < ?', reference.days_since(2))
  46. .order(start: :asc)
  47. return {currently_active: currently_active, previous: previous, upcoming: upcoming}
  48. end
  49. # Determine whether the passed person is a member of the group.
  50. def is_member?(person)
  51. Member.exists?(
  52. person: person,
  53. group: self
  54. ) || person.is_admin?
  55. end
  56. # Determine whether the passed person is a group leader.
  57. def is_leader?(person)
  58. Member.exists?(
  59. person: person,
  60. group: self,
  61. is_leader: true
  62. ) || person.is_admin?
  63. end
  64. end