Browse Source

Expose 'reminder_at' to users and schedule

Maarten van den Berg 7 years ago
parent
commit
3974dbccda

+ 1 - 1
app/controllers/activities_controller.rb

178
 
178
 
179
     # Never trust parameters from the scary internet, only allow the white list through.
179
     # Never trust parameters from the scary internet, only allow the white list through.
180
     def activity_params
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
     end
182
     end
183
 end
183
 end

+ 19 - 0
app/models/activity.rb

49
   validates :start, presence: true
49
   validates :start, presence: true
50
   validate  :deadline_before_start, unless: "self.deadline.blank?"
50
   validate  :deadline_before_start, unless: "self.deadline.blank?"
51
   validate  :end_after_start,       unless: "self.end.blank?"
51
   validate  :end_after_start,       unless: "self.end.blank?"
52
+  validate  :reminder_before_deadline, unless: "self.reminder_at.blank?"
52
 
53
 
53
   after_create :create_missing_participants!
54
   after_create :create_missing_participants!
55
+  after_commit :schedule_reminder, if: Proc.new {|a| a.previous_changes["reminder_at"] }
54
 
56
 
55
   # Get all people (not participants) that are organizers. Does not include
57
   # Get all people (not participants) that are organizers. Does not include
56
   # group leaders, although they may modify the activity as well.
58
   # group leaders, although they may modify the activity as well.
154
 
156
 
155
     participants = self.participants.where(attending: nil)
157
     participants = self.participants.where(attending: nil)
156
     participants.each { |p| p.send_reminder }
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
   end
168
   end
158
 
169
 
159
   private
170
   private
171
       errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
182
       errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
172
     end
183
     end
173
   end
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
 end
193
 end

+ 3 - 0
app/models/participant.rb

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

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

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

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

1
 object @activity
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
 node :response_counts do
5
 node :response_counts do
6
   c = @activity.state_counts
6
   c = @activity.state_counts

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

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

+ 2 - 0
config/locales/translation_nl.yml

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