Sprankelprachtig aan/afmeldsysteem

group.rb 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # all Activities that have started, and not yet ended.
  30. def current_activities(reference = Time.zone.now)
  31. activities
  32. .where('start < ?', reference)
  33. .where('end > ?', reference)
  34. end
  35. # @return [Array<Activity>]
  36. # at most 3 activities that ended recently.
  37. def previous_activities(reference = Time.zone.now)
  38. activities
  39. .where('end < ?', reference)
  40. .order(end: :desc)
  41. .limit(3)
  42. end
  43. # @return [Array<Activity>]
  44. # all Activities starting within the next 48 hours.
  45. def upcoming_activities(reference = Time.zone.now)
  46. activities
  47. .where('start > ?', reference)
  48. .where('start < ?', reference.days_since(2))
  49. .order(start: :asc)
  50. end
  51. # @return [Boolean]
  52. # whether the passed person is a member of the group.
  53. def is_member?(person)
  54. Member.exists?(
  55. person: person,
  56. group: self
  57. ) || person.is_admin?
  58. end
  59. # @return [Boolean]
  60. # whether the passed person is a group leader.
  61. def is_leader?(person)
  62. Member.exists?(
  63. person: person,
  64. group: self,
  65. is_leader: true
  66. ) || person.is_admin?
  67. end
  68. end