Browse Source

Rework api/activities/index to show p/c/u, add routes

Maarten van den Berg 6 years ago
parent
commit
efde08d01e

+ 24 - 4
app/controllers/api/groups_controller.rb

@@ -1,18 +1,38 @@
1 1
 class Api::GroupsController < ApiController
2
-  before_action :set_group, only: [:show]
3
-  before_action :require_membership!, only: [:show]
4
-  before_action :api_require_admin!, only: [:index]
2
+  has_no_group = [:index]
3
+
4
+  before_action :set_group, except: has_no_group
5
+  before_action :require_membership!, except: has_no_group
6
+  before_action :api_require_admin!, only: has_no_group
5 7
 
6 8
   # GET /api/groups
7 9
   # GET /api/groups.json
8 10
   def index
9
-    @api_groups = Api::Group.all
11
+    @api_groups = Group.all
10 12
   end
11 13
 
12 14
   # GET /api/groups/1
13 15
   def show
14 16
   end
15 17
 
18
+  # GET /api/groups/1/current_activities
19
+  def current_activities
20
+    @activities = @group.current_activities
21
+    render 'api/activities/index'
22
+  end
23
+
24
+  # GET /api/groups/1/upcoming_activities
25
+  def upcoming_activities
26
+    @activities = @group.upcoming_activities
27
+    render 'api/activities/index'
28
+  end
29
+
30
+  # GET /api/groups/1/previous_activities
31
+  def previous_activities
32
+    @activities = @group.previous_activities
33
+    render 'api/activities/index'
34
+  end
35
+
16 36
   private
17 37
     # Use callbacks to share common setup or constraints between actions.
18 38
     def set_group

+ 16 - 13
app/models/group.rb

@@ -33,31 +33,33 @@ class Group < ApplicationRecord
33 33
   end
34 34
 
35 35
   # @return [Array<Activity>]
36
-  #   the Activity/Activities that are the closest to the given point in time.
37
-  #   Logic is as follows:
38
-  #     - If one or more Activities start before and end after the given point
39
-  #       in time, these are returned.
40
-  #     - Additionally, the last 3 activities that ended are returned, as well
41
-  #       as any activities starting within the next 48 hours.
36
+  #   all Activities that have started, and not yet ended.
42 37
   def current_activities(reference = Time.zone.now)
43
-    currently_active = self.activities
38
+    activities
44 39
       .where('start < ?', reference)
45 40
       .where('end > ?', reference)
41
+  end
46 42
 
47
-    previous = self.activities
43
+  # @return [Array<Activity>]
44
+  #   at most 3 activities that ended recently.
45
+  def previous_activities(reference = Time.zone.now)
46
+    activities
48 47
       .where('end < ?', reference)
49 48
       .order(end: :desc)
50 49
       .limit(3)
50
+  end
51 51
 
52
-    upcoming = self.activities
52
+  # @return [Array<Activity>]
53
+  #   all Activities starting within the next 48 hours.
54
+  def upcoming_activities(reference = Time.zone.now)
55
+    activities
53 56
       .where('start > ?', reference)
54 57
       .where('start < ?', reference.days_since(2))
55 58
       .order(start: :asc)
56
-
57
-    return {currently_active: currently_active, previous: previous, upcoming: upcoming}
58 59
   end
59 60
 
60
-  # Determine whether the passed person is a member of the group.
61
+  # @return [Boolean]
62
+  #   whether the passed person is a member of the group.
61 63
   def is_member?(person)
62 64
     Member.exists?(
63 65
       person: person,
@@ -65,7 +67,8 @@ class Group < ApplicationRecord
65 67
     ) || person.is_admin?
66 68
   end
67 69
 
68
-  # Determine whether the passed person is a group leader.
70
+  # @return [Boolean]
71
+  #   whether the passed person is a group leader.
69 72
   def is_leader?(person)
70 73
     Member.exists?(
71 74
       person: person,

+ 8 - 1
app/views/api/activities/index.rabl

@@ -1,3 +1,10 @@
1 1
 collection @activities
2 2
 
3
-attributes :id, :name
3
+attributes :id, :name, :description, :location, :start, :end, :deadline
4
+
5
+node :no_response_action do |a|
6
+  {
7
+    true => "present",
8
+    false => "absent"
9
+  }[a.no_response_action]
10
+end

+ 5 - 1
config/routes.rb

@@ -75,7 +75,11 @@ Rails.application.routes.draw do
75 75
       get 'groups', to: 'me#groups'
76 76
     end
77 77
 
78
-    resources :groups, only: [:index, :show]
78
+    resources :groups, only: [:index, :show] do
79
+      get :current_activities, on: :member
80
+      get :previous_activities, on: :member
81
+      get :upcoming_activities, on: :member
82
+    end
79 83
 
80 84
     resources :activities, only: [:index, :show]
81 85
     get 'activities/:id/response_summary', to: 'activities#response_summary'