|
@@ -8,9 +8,27 @@ class Member < ApplicationRecord
|
8
|
8
|
belongs_to :person
|
9
|
9
|
belongs_to :group
|
10
|
10
|
|
|
11
|
+ before_destroy :delete_future_participants!
|
|
12
|
+
|
11
|
13
|
validates :person_id,
|
12
|
14
|
uniqueness: {
|
13
|
15
|
scope: :group_id,
|
14
|
16
|
message: "is already a member of this group"
|
15
|
17
|
}
|
|
18
|
+
|
|
19
|
+ # Delete all Participants of this Member for Activities in the future.
|
|
20
|
+ # Intended to be called before the member is deleted.
|
|
21
|
+ def delete_future_participants!
|
|
22
|
+ activities = self.group.activities
|
|
23
|
+ .where('start > ?', DateTime.now)
|
|
24
|
+
|
|
25
|
+ participants = Participant.where(
|
|
26
|
+ person_id: self.person.id,
|
|
27
|
+ activity: activities
|
|
28
|
+ )
|
|
29
|
+
|
|
30
|
+ participants.each do |p|
|
|
31
|
+ p.destroy!
|
|
32
|
+ end
|
|
33
|
+ end
|
16
|
34
|
end
|