Browse Source

Add changing presence in API, style fixes

Maarten van den Berg 6 years ago
parent
commit
bac8fef0c0
2 changed files with 28 additions and 21 deletions
  1. 23 20
      app/controllers/api/activities_controller.rb
  2. 5 1
      config/routes.rb

+ 23 - 20
app/controllers/api/activities_controller.rb

@@ -18,47 +18,40 @@ class Api::ActivitiesController < ApiController
18 18
   end
19 19
 
20 20
   # GET /api/activities/1
21
-  def show
22
-  end
21
+  def show; end
23 22
 
24 23
   # GET /api/activities/1/response_summary
25 24
   def response_summary
26 25
     as = @activity
27
-      .participants
28
-      .joins(:person)
29
-      .order('people.first_name ASC')
26
+         .participants
27
+         .joins(:person)
28
+         .order('people.first_name ASC')
30 29
 
31
-    present = as
32
-      .where(attending: true)
30
+    present = as.where(attending: true)
33 31
 
34
-    unknown = as
35
-      .where(attending: nil)
32
+    unknown = as.where(attending: nil)
36 33
 
37
-    absent = as
38
-      .where(attending: false)
34
+    absent = as.where(attending: false)
39 35
 
40
-    presentnames = present
41
-      .map{|p| p.person.first_name }
36
+    presentnames = present.map{|p| p.person.first_name }
42 37
 
43
-    unknownnames = unknown
44
-      .map{|p| p.person.first_name }
38
+    unknownnames = unknown.map{|p| p.person.first_name }
45 39
 
46
-    absentnames = absent
47
-      .map{|p| p.person.first_name }
40
+    absentnames = absent.map{|p| p.person.first_name }
48 41
 
49
-    if presentnames.count > 0
42
+    if presentnames.positive?
50 43
       present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
51 44
     else
52 45
       present_mess = I18n.t('activities.participant.none_present')
53 46
     end
54 47
 
55
-    if unknownnames.count > 0
48
+    if unknownnames.positive?
56 49
       unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
57 50
     else
58 51
       unknown_mess = I18n.t('activities.participant.none_unknown')
59 52
     end
60 53
 
61
-    if absentnames.count > 0
54
+    if absentnames.positive?
62 55
       absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
63 56
     else
64 57
       absent_mess = I18n.t('activities.participant.none_absent')
@@ -83,6 +76,16 @@ class Api::ActivitiesController < ApiController
83 76
     }
84 77
   end
85 78
 
79
+  def presence
80
+    participant = Participant.find_by(
81
+      person_id: params[:person_id],
82
+      activity: @activity
83
+    )
84
+
85
+    participant.update_attributes(params.permit(:attending))
86
+    head :no_content
87
+  end
88
+
86 89
   private
87 90
 
88 91
   # Set activity from the :id-parameter

+ 5 - 1
config/routes.rb

@@ -81,7 +81,11 @@ Rails.application.routes.draw do
81 81
       get :upcoming_activities, on: :member
82 82
     end
83 83
 
84
-    resources :activities, only: [:index, :show]
84
+    resources :activities, only: [:index, :show] do
85
+      get 'response_summary'
86
+      put 'presence'
87
+      patch 'presence'
88
+    end
85 89
     get 'activities/:id/response_summary', to: 'activities#response_summary'
86 90
 
87 91
     resources :people, only: [:index, :show]