Browse Source

Subgroup mailing works

Maarten van den Berg 7 years ago
parent
commit
6e51dca39e

+ 1 - 1
app/controllers/activities_controller.rb

@@ -228,7 +228,7 @@ class ActivitiesController < ApplicationController
228 228
 
229 229
     # Never trust parameters from the scary internet, only allow the white list through.
230 230
     def activity_params
231
-      params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline, :reminder_at)
231
+      params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline, :reminder_at, :subgroup_division_enabled)
232 232
     end
233 233
 
234 234
     def subgroup_params

+ 28 - 0
app/mailers/participant_mailer.rb

@@ -7,4 +7,32 @@ class ParticipantMailer < ApplicationMailer
7 7
 
8 8
     mail(to: @person.email, subject: subject)
9 9
   end
10
+
11
+  def subgroup_notification(person, activity, participant)
12
+    @person = person
13
+    @activity = activity
14
+
15
+    @subgroup = participant.subgroup.name
16
+
17
+    @others = participant
18
+      .subgroup
19
+      .participants
20
+      .where.not(person: @person)
21
+      .map { |pp| pp.person.full_name }
22
+      .sort
23
+      .join(', ')
24
+
25
+    @subgroups = @activity
26
+      .subgroups
27
+      .order(name: :asc)
28
+
29
+    @organizers = @activity
30
+      .organizer_names
31
+      .sort
32
+      .join(', ')
33
+
34
+    subject = I18n.t('activities.emails.subgroup_notification.subject', subgroup: @subgroup, activity: @activity.name)
35
+
36
+    mail(to: @person.email, subject: subject)
37
+  end
10 38
 end

+ 29 - 2
app/models/activity.rb

@@ -79,6 +79,10 @@ class Activity < ApplicationRecord
79 79
     self.participants.includes(:person).where(is_organizer: true)
80 80
   end
81 81
 
82
+  def organizer_names
83
+    self.organizers.map { |o| o.person.full_name }
84
+  end
85
+
82 86
   # Determine whether the passed Person participates in the activity.
83 87
   def is_participant?(person)
84 88
     Participant.exists?(
@@ -205,11 +209,11 @@ class Activity < ApplicationRecord
205 209
   def schedule_subgroup_division
206 210
     return if self.deadline.nil? || self.subgroup_division_done
207 211
 
208
-    self.delay(run_at: self.deadline).assign_subgroups!
212
+    self.delay(run_at: self.deadline).assign_subgroups!(mail: true)
209 213
   end
210 214
 
211 215
   # Assign a subgroup to all attending participants without one.
212
-  def assign_subgroups!
216
+  def assign_subgroups!(mail= false)
213 217
     # Sanity check: we need subgroups to divide into.
214 218
     return unless self.subgroups.any?
215 219
 
@@ -240,6 +244,22 @@ class Activity < ApplicationRecord
240 244
       # Update the group's position in the list, will sort when next participant is processed.
241 245
       groups.first[0] += 1
242 246
     end
247
+
248
+    if mail
249
+      self.notify_subgroups!
250
+    end
251
+  end
252
+
253
+  # Notify participants of the current subgroups, if any.
254
+  def notify_subgroups!
255
+    ps = self
256
+      .participants
257
+      .joins(:person)
258
+      .where.not(subgroup: nil)
259
+
260
+    ps.each do |pp|
261
+      pp.send_subgroup_notification
262
+    end
243 263
   end
244 264
 
245 265
   private
@@ -266,4 +286,11 @@ class Activity < ApplicationRecord
266 286
       errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
267 287
     end
268 288
   end
289
+
290
+  # Assert that there is at least one divisible subgroup.
291
+  def subgroups_for_division_present
292
+    if self.subgroups.where(is_assignable: true).none?
293
+      errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups'))
294
+    end
295
+  end
269 296
 end

+ 7 - 0
app/models/participant.rb

@@ -57,4 +57,11 @@ class Participant < ApplicationRecord
57 57
     ParticipantMailer.attendance_reminder(self.person, self.activity).deliver_later
58 58
   end
59 59
 
60
+  # Send subgroup information email if person is attending.
61
+  def send_subgroup_notification
62
+    return unless self.attending && self.subgroup
63
+
64
+    ParticipantMailer.subgroup_notification(self.person, self.activity, self).deliver_later
65
+  end
66
+
60 67
 end

+ 8 - 0
app/models/subgroup.rb

@@ -4,4 +4,12 @@ class Subgroup < ApplicationRecord
4 4
 
5 5
   validates :name, presence: true, uniqueness: { scope: :activity, case_sensitive: false }
6 6
   validates :activity, presence: true
7
+
8
+  def participant_names
9
+    self
10
+      .participants
11
+      .joins(:person)
12
+      .map { |p| p.person.full_name }
13
+      .sort
14
+  end
7 15
 end

+ 34 - 0
app/views/participant_mailer/subgroup_notification.html.haml

@@ -0,0 +1,34 @@
1
+%p= t 'authentication.emails.greeting', name: @person.first_name
2
+
3
+%p= t 'activities.emails.subgroup_notification.yoursubgroupis', activity: @activity.name, subgroup: @subgroup
4
+
5
+- if !@others.empty?
6
+  %p= t 'activities.emails.subgroup_notification.subgroupmembers', others: @others
7
+- else
8
+  %p= t 'activities.emails.subgroup_notification.noothersinsubgroup'
9
+
10
+%p= t 'activities.emails.subgroup_notification.allsubgroups'
11
+%ul
12
+  - @subgroups.each do |sg|
13
+    %li
14
+      = succeed ':' do
15
+        = sg.name
16
+      = sg.participant_names.join ', '
17
+
18
+%p= t 'activities.emails.subgroup_notification.cannotdecline', organizers: @organizers
19
+
20
+%p
21
+  = link_to group_activity_url(@activity.group, @activity) do
22
+    = t 'activities.emails.open_activity'
23
+
24
+%p
25
+  = t('activities.emails.ending').sample
26
+  %br
27
+  Aardbei
28
+
29
+%hr
30
+%footer
31
+  %p
32
+    = t 'activities.emails.dont_want_mail'
33
+    %a{href: "https://aardbei.maartenberg.nl/settings"}
34
+      = t 'activities.emails.open_settings'

+ 26 - 0
app/views/participant_mailer/subgroup_notification.text.erb

@@ -0,0 +1,26 @@
1
+<%= t 'authentication.emails.greeting', name: @person.first_name %>
2
+
3
+<%= t 'activities.emails.subgroup_notification.yoursubgroupis', activity: @activity.name, subgroup: @subgroup %>
4
+
5
+<% if !@others.empty? %>
6
+<%= t 'activities.emails.subgroup_notification.subgroupmembers', others: @others %>
7
+<% else %>
8
+<%= t 'activities.emails.subgroup_notification.noothersinsubgroup' %>
9
+<% end %>
10
+
11
+<%= t 'activity.emails.subgroup_notification.allsubgroups' %>
12
+
13
+<% @subgroups.each do |sg| %>
14
+- <%= sg.name %>: <%= sg.participant_names.join(', ') %>
15
+<% end %>
16
+
17
+<%= t 'activity.emails.subgroup_notification.cannotdecline', organizers: @organizers %>
18
+
19
+<%= group_activity_url(@activity.group, @activity) %>
20
+
21
+<%= t('activities.emails.ending').sample %>
22
+Aardbei
23
+
24
+---
25
+<%= t 'activities.emails.dont_want_mail' %>
26
+https://aardbei.maartenberg.nl/settings

+ 7 - 0
config/locales/activities/en.yml

@@ -78,6 +78,13 @@ en:
78 78
         havenot_responded: "You have not yet indicated if you will be at %{activity}. Because we assume that you're present if you don't respond, your response has been set to 'attending'."
79 79
         if_cannot: "If you wish to change this, you can change your response until %{deadline} using the following link:"
80 80
 
81
+      subgroup_notification:
82
+        subject: "You have been assigned to subgroup %{subgroup} for %{activity}"
83
+        yoursubgroupis: "The upcoming activity %{activity} uses subgroups, and you have been assigned to subgroup %{subgroup}."
84
+        subgroupmembers: "The other people in this subgroup are: %{others}"
85
+        allsubgroups: "All subgroups (including yours):"
86
+        cannotdecline: "This email was sent when the deadline expired. If you find you cannot attend, please contact the Drerrie, or one of the organisers (%{organizers})."
87
+
81 88
     subgroups:
82 89
       manage: 'Manage subgroups'
83 90
       create: 'Create subgroup'

+ 7 - 0
config/locales/activities/nl.yml

@@ -84,6 +84,13 @@ nl:
84 84
         subject: "Je bent automatisch aangemeld voor %{activity}"
85 85
         havenot_responded: "Je hebt nog niet aangegeven of je bij %{activity} kunt zijn. Omdat we ervan uitgaan dat je er bent als je niks aangeeft, is je reactie automatisch op 'aanwezig' gezet."
86 86
         if_cannot: "Als je toch niet aanwezig kunt zijn, kan je dit tot %{deadline} aangeven via de volgende link:"
87
+      subgroup_notification:
88
+        subject: "Je bent ingedeeld in subgroep %{subgroup} voor %{activity}"
89
+        yoursubgroupis: "De aankomende opkomst %{activity} gebruikt subgroepen, en jij bent ingedeeld in subgroep %{subgroup}."
90
+        subgroupmembers: "De andere mensen in deze subgroep zijn: %{others}"
91
+        noothersinsubgroup: "Er zijn geen andere mensen in deze subgroep. :("
92
+        allsubgroups: "Alle groepjes (inclusief de jouwe) zijn:"
93
+        cannotdecline: "Deze email is verstuurd toen de deadline voor het afmelden verstreek. Als je om wat voor reden dan ook toch niet kan, neem dan contact op met de Drerrie (afmeld@maartenberg.nl) of een van de organisators van de opkomst (%{organizers})."
87 94
 
88 95
     subgroups:
89 96
       manage: 'Subgroepen aanpassen'