Sprankelprachtig aan/afmeldsysteem

group.rb 2.2KB

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