Explorar el Código

Add changing presence in API, style fixes

Maarten van den Berg %!s(int64=6) %!d(string=hace) años
padre
commit
bac8fef0c0
Se han modificado 2 ficheros con 28 adiciones y 21 borrados
  1. 23 20
      app/controllers/api/activities_controller.rb
  2. 5 1
      config/routes.rb

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

18
   end
18
   end
19
 
19
 
20
   # GET /api/activities/1
20
   # GET /api/activities/1
21
-  def show
22
-  end
21
+  def show; end
23
 
22
 
24
   # GET /api/activities/1/response_summary
23
   # GET /api/activities/1/response_summary
25
   def response_summary
24
   def response_summary
26
     as = @activity
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
       present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
43
       present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
51
     else
44
     else
52
       present_mess = I18n.t('activities.participant.none_present')
45
       present_mess = I18n.t('activities.participant.none_present')
53
     end
46
     end
54
 
47
 
55
-    if unknownnames.count > 0
48
+    if unknownnames.positive?
56
       unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
49
       unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
57
     else
50
     else
58
       unknown_mess = I18n.t('activities.participant.none_unknown')
51
       unknown_mess = I18n.t('activities.participant.none_unknown')
59
     end
52
     end
60
 
53
 
61
-    if absentnames.count > 0
54
+    if absentnames.positive?
62
       absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
55
       absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
63
     else
56
     else
64
       absent_mess = I18n.t('activities.participant.none_absent')
57
       absent_mess = I18n.t('activities.participant.none_absent')
83
     }
76
     }
84
   end
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
   private
89
   private
87
 
90
 
88
   # Set activity from the :id-parameter
91
   # Set activity from the :id-parameter

+ 5 - 1
config/routes.rb

81
       get :upcoming_activities, on: :member
81
       get :upcoming_activities, on: :member
82
     end
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
     get 'activities/:id/response_summary', to: 'activities#response_summary'
89
     get 'activities/:id/response_summary', to: 'activities#response_summary'
86
 
90
 
87
     resources :people, only: [:index, :show]
91
     resources :people, only: [:index, :show]