Sprankelprachtig aan/afmeldsysteem

member.rb 897B

1234567891011121314151617181920212223242526272829303132333435
  1. # A Member represents the many-to-many relation of Groups to People. At most
  2. # one member may exist for each Person-Group combination.
  3. class Member < ApplicationRecord
  4. # @!attribute is_leader
  5. # @return [Boolean]
  6. # whether the person is a leader in the group.
  7. belongs_to :person
  8. belongs_to :group
  9. before_destroy :delete_future_participants!
  10. validates :person_id,
  11. uniqueness: {
  12. scope: :group_id,
  13. message: "is already a member of this group"
  14. }
  15. # Delete all Participants of this Member for Activities in the future.
  16. # Intended to be called before the member is deleted.
  17. def delete_future_participants!
  18. activities = self.group.activities
  19. .where('start > ?', DateTime.now)
  20. participants = Participant.where(
  21. person_id: self.person.id,
  22. activity: activities
  23. )
  24. participants.each do |p|
  25. p.destroy!
  26. end
  27. end
  28. end