Sprankelprachtig aan/afmeldsysteem

group.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_secure_token :api_token
  9. has_many :members,
  10. dependent: :destroy
  11. has_many :people, through: :members
  12. has_many :activities,
  13. dependent: :destroy
  14. has_many :default_subgroups,
  15. dependent: :destroy
  16. validates :name,
  17. presence: true,
  18. uniqueness: {
  19. case_sensitive: false
  20. }
  21. # @return [Array<Member>] the members in the group who are also group leaders.
  22. def leaders
  23. self.members.includes(:person).where(is_leader: true)
  24. end
  25. # @return [Array<Activity>] the activities that haven't started yet.
  26. def future_activities
  27. self.activities.where('start > ?', DateTime.now)
  28. end
  29. # @return [Array<Activity>]
  30. # all Activities that have started, and not yet ended.
  31. def current_activities(reference = Time.zone.now)
  32. activities
  33. .where('start < ?', reference)
  34. .where('end > ?', reference)
  35. end
  36. # @return [Array<Activity>]
  37. # at most 3 activities that ended recently.
  38. def previous_activities(reference = Time.zone.now)
  39. activities
  40. .where('end < ?', reference)
  41. .order(end: :desc)
  42. .limit(3)
  43. end
  44. # @return [Array<Activity>]
  45. # all Activities starting within the next 48 hours.
  46. def upcoming_activities(reference = Time.zone.now)
  47. activities
  48. .where('start > ?', reference)
  49. .where('start < ?', reference.days_since(2))
  50. .order(start: :asc)
  51. end
  52. # @return [Boolean]
  53. # whether the passed person is a member of the group.
  54. def is_member?(person)
  55. Member.exists?(
  56. person: person,
  57. group: self
  58. ) || person.is_admin?
  59. end
  60. # @return [Boolean]
  61. # whether the passed person is a group leader.
  62. def is_leader?(person)
  63. Member.exists?(
  64. person: person,
  65. group: self,
  66. is_leader: true
  67. ) || person.is_admin?
  68. end
  69. end