Ver Código Fonte

Expose 'reminder_at' to users and schedule

Maarten van den Berg 7 anos atrás
pai
commit
3974dbccda

+ 1 - 1
app/controllers/activities_controller.rb

@@ -178,6 +178,6 @@ class ActivitiesController < ApplicationController
178 178
 
179 179
     # Never trust parameters from the scary internet, only allow the white list through.
180 180
     def activity_params
181
-      params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline)
181
+      params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline, :reminder_at)
182 182
     end
183 183
 end

+ 19 - 0
app/models/activity.rb

@@ -49,8 +49,10 @@ class Activity < ApplicationRecord
49 49
   validates :start, presence: true
50 50
   validate  :deadline_before_start, unless: "self.deadline.blank?"
51 51
   validate  :end_after_start,       unless: "self.end.blank?"
52
+  validate  :reminder_before_deadline, unless: "self.reminder_at.blank?"
52 53
 
53 54
   after_create :create_missing_participants!
55
+  after_commit :schedule_reminder, if: Proc.new {|a| a.previous_changes["reminder_at"] }
54 56
 
55 57
   # Get all people (not participants) that are organizers. Does not include
56 58
   # group leaders, although they may modify the activity as well.
@@ -154,6 +156,15 @@ class Activity < ApplicationRecord
154 156
 
155 157
     participants = self.participants.where(attending: nil)
156 158
     participants.each { |p| p.send_reminder }
159
+
160
+    self.reminder_done = true
161
+    self.save
162
+  end
163
+
164
+  def schedule_reminder
165
+    return if self.reminder_at.nil? || self.reminder_done
166
+
167
+    self.delay(run_at: self.reminder_at).send_reminder
157 168
   end
158 169
 
159 170
   private
@@ -171,4 +182,12 @@ class Activity < ApplicationRecord
171 182
       errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
172 183
     end
173 184
   end
185
+
186
+  # Assert that the reminder for non-response is sent while participants still
187
+  # can change their response.
188
+  def reminder_before_deadline
189
+    if self.reminder_at > self.deadline
190
+      errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
191
+    end
192
+  end
174 193
 end

+ 3 - 0
app/models/participant.rb

@@ -47,6 +47,9 @@ class Participant < ApplicationRecord
47 47
     return unless self.attending.nil?
48 48
 
49 49
     self.attending = true
50
+    notes = self.notes || ""
51
+    notes << '[auto]'
52
+    self.notes = notes
50 53
     self.save
51 54
 
52 55
     ParticipantMailer.attendance_reminder(self.person, self.activity).deliver_later

+ 4 - 0
app/views/activities/_form.html.erb

@@ -40,6 +40,10 @@
40 40
       <%= f.label :deadline %>
41 41
       <%= f.datetime_field :deadline, class: 'form-control' %>
42 42
     </div>
43
+    <div class="form-group">
44
+      <%= f.label :reminder_at %>
45
+      <%= f.datetime_field :reminder_at, class: 'form-control' %>
46
+    </div>
43 47
     <%= f.submit %>
44 48
   </div>
45 49
 <% end %>

+ 1 - 1
app/views/api/activities/show.rabl

@@ -1,6 +1,6 @@
1 1
 object @activity
2 2
 
3
-attributes :id, :name, :start, :end, :deadline, :location
3
+attributes :id, :name, :start, :end, :deadline, :location, :reminder_at
4 4
 
5 5
 node :response_counts do
6 6
   c = @activity.state_counts

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

@@ -19,6 +19,7 @@ nl:
19 19
     errors:
20 20
       must_be_before_start: "moet voor start zijn"
21 21
       must_be_after_start: "moet na start zijn"
22
+      must_be_before_deadline: "moet voor deadline zijn"
22 23
       already_in: "persoon doet al mee aan activiteit"
23 24
 
24 25
     participant:

+ 2 - 0
config/locales/translation_nl.yml

@@ -35,6 +35,8 @@ nl:
35 35
         participants: Deelnemers  #g
36 36
         people: Mensen  #g
37 37
         start: Start  #g
38
+        reminder_at: Herinnering om
39
+        reminder_done: Herinnering verstuurd
38 40
 
39 41
       group:
40 42
         activities: Activiteiten  #g