Browse Source

Create Participants after Activity saved

Maarten van den Berg 8 years ago
parent
commit
86f6ddb073
1 changed files with 19 additions and 0 deletions
  1. 19 0
      app/models/activity.rb

+ 19 - 0
app/models/activity.rb

53
   validate  :deadline_before_start, unless: "self.deadline.blank?"
53
   validate  :deadline_before_start, unless: "self.deadline.blank?"
54
   validate  :end_after_start,       unless: "self.end.blank?"
54
   validate  :end_after_start,       unless: "self.end.blank?"
55
 
55
 
56
+  after_create :create_missing_participants!
57
+
56
   # Get all people (not participants) that are organizers. Does not include
58
   # Get all people (not participants) that are organizers. Does not include
57
   # group leaders, although they may modify the activity as well.
59
   # group leaders, although they may modify the activity as well.
58
   def organizers
60
   def organizers
88
     self.group.is_leader(person)
90
     self.group.is_leader(person)
89
   end
91
   end
90
 
92
 
93
+  # Create Participants for all People that
94
+  #  1. are members of the group
95
+  #  2. do not have Participants (and thus, no way to confirm) yet
96
+  def create_missing_participants!
97
+    people = self.group.people
98
+    if not self.participants.empty?
99
+      people = people.where('people.id NOT IN (?)', self.people.ids)
100
+    end
101
+
102
+    people.each do |p|
103
+      Participant.create(
104
+        activity: self,
105
+        person: p,
106
+      )
107
+    end
108
+  end
109
+
91
   private
110
   private
92
   # Assert that the deadline for participants to change the deadline, if any,
111
   # Assert that the deadline for participants to change the deadline, if any,
93
   # is set before the event starts.
112
   # is set before the event starts.