Sprankelprachtig aan/afmeldsysteem

member.rb 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. after_create :create_future_participants!
  10. before_destroy :delete_future_participants!
  11. validates :person_id,
  12. uniqueness: {
  13. scope: :group_id,
  14. message: I18n.t('groups.member.already_in')
  15. }
  16. # Create Participants for this Member for all the group's future activities, where the member isn't enrolled yet.
  17. # Intended to be called after the member is added to the group.
  18. def create_future_participants!
  19. activities = group.future_activities
  20. unless person.activities.empty?
  21. activities = activities.where(
  22. 'activities.id NOT IN (?)', person.activities.ids
  23. )
  24. end
  25. activities.each do |a|
  26. Participant.create!(
  27. activity: a,
  28. person: person
  29. )
  30. end
  31. end
  32. # Delete all Participants of this Member for Activities in the future.
  33. # Intended to be called before the member is deleted.
  34. def delete_future_participants!
  35. participants = Participant.where(
  36. person_id: person.id,
  37. activity: group.future_activities
  38. )
  39. participants.each(&:destroy!)
  40. end
  41. end