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
 class Api::GroupsController < ApiController
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
   # GET /api/groups
8
   # GET /api/groups
7
   # GET /api/groups.json
9
   # GET /api/groups.json
8
   def index
10
   def index
9
-    @api_groups = Api::Group.all
11
+    @api_groups = Group.all
10
   end
12
   end
11
 
13
 
12
   # GET /api/groups/1
14
   # GET /api/groups/1
13
   def show
15
   def show
14
   end
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
   private
36
   private
17
     # Use callbacks to share common setup or constraints between actions.
37
     # Use callbacks to share common setup or constraints between actions.
18
     def set_group
38
     def set_group

+ 16 - 13
app/models/group.rb

33
   end
33
   end
34
 
34
 
35
   # @return [Array<Activity>]
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
   def current_activities(reference = Time.zone.now)
37
   def current_activities(reference = Time.zone.now)
43
-    currently_active = self.activities
38
+    activities
44
       .where('start < ?', reference)
39
       .where('start < ?', reference)
45
       .where('end > ?', reference)
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
       .where('end < ?', reference)
47
       .where('end < ?', reference)
49
       .order(end: :desc)
48
       .order(end: :desc)
50
       .limit(3)
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
       .where('start > ?', reference)
56
       .where('start > ?', reference)
54
       .where('start < ?', reference.days_since(2))
57
       .where('start < ?', reference.days_since(2))
55
       .order(start: :asc)
58
       .order(start: :asc)
56
-
57
-    return {currently_active: currently_active, previous: previous, upcoming: upcoming}
58
   end
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
   def is_member?(person)
63
   def is_member?(person)
62
     Member.exists?(
64
     Member.exists?(
63
       person: person,
65
       person: person,
65
     ) || person.is_admin?
67
     ) || person.is_admin?
66
   end
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
   def is_leader?(person)
72
   def is_leader?(person)
70
     Member.exists?(
73
     Member.exists?(
71
       person: person,
74
       person: person,

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

1
 collection @activities
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
       get 'groups', to: 'me#groups'
75
       get 'groups', to: 'me#groups'
76
     end
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
     resources :activities, only: [:index, :show]
84
     resources :activities, only: [:index, :show]
81
     get 'activities/:id/response_summary', to: 'activities#response_summary'
85
     get 'activities/:id/response_summary', to: 'activities#response_summary'