Browse Source

Delete future participants on group leave

Maarten van den Berg 8 years ago
parent
commit
7e7585eef3
1 changed files with 18 additions and 0 deletions
  1. 18 0
      app/models/member.rb

+ 18 - 0
app/models/member.rb

@@ -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