Skip to content

Commit bced740

Browse files
committed
Extract events loading from overloaded UsersController
1 parent 6a62df4 commit bced740

File tree

6 files changed

+66
-31
lines changed

6 files changed

+66
-31
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Users::EventsController < ApplicationController
2+
include FilterScoped
3+
4+
before_action :set_user, :set_filter, :set_user_filtering
5+
6+
def show
7+
@filter = Current.user.filters.new(creator_ids: [ @user.id ])
8+
@day_timeline = Current.user.timeline_for(day_param, filter: @filter)
9+
end
10+
11+
private
12+
def set_user
13+
@user = User.active.find(params[:user_id])
14+
end
15+
16+
def day_param
17+
if params[:day].present?
18+
Time.zone.parse(params[:day])
19+
else
20+
Time.current
21+
end
22+
end
23+
end

app/controllers/users_controller.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
class UsersController < ApplicationController
2-
include FilterScoped
3-
42
require_access_without_a_user only: %i[ new create ]
53

6-
before_action :set_join_code, only: %i[ new create]
7-
before_action :ensure_join_code_is_valid, only: %i[ new create ]
4+
before_action :set_join_code, :ensure_join_code_is_valid, only: %i[ new create ]
85
before_action :set_user, only: %i[ show edit update destroy ]
96
before_action :ensure_permission_to_change_user, only: %i[ update destroy ]
10-
before_action :set_filter, only: %i[ edit show ]
11-
before_action :set_user_filtering, only: %i[ edit show]
127

138
def new
149
end
@@ -25,8 +20,6 @@ def edit
2520
end
2621

2722
def show
28-
@filter = Current.user.filters.new(creator_ids: [ @user.id ])
29-
@day_timeline = Current.user.timeline_for(day_param, filter: @filter)
3023
end
3124

3225
def update
@@ -58,14 +51,6 @@ def ensure_permission_to_change_user
5851
head :forbidden unless Current.user.can_change?(@user)
5952
end
6053

61-
def day_param
62-
if params[:day].present?
63-
Time.zone.parse(params[:day])
64-
else
65-
Time.current
66-
end
67-
end
68-
6954
def user_params
7055
params.expect(user: [ :name, :avatar ])
7156
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<turbo-frame id="user_events">
2+
<h1 class="font-weight-black txt-large margin-block-double"><%= "What #{Current.user == @user ? "have you" : "has #{@user.first_name}"} been up to?" %></h1>
3+
4+
<div class="events margin-block-double" id="activity" data-controller="pagination" data-pagination-paginate-on-intersection-value="true">
5+
<%= day_timeline_pagination_frame_tag @day_timeline do %>
6+
<%= render "events/day", day_timeline: @day_timeline %>
7+
8+
<% if @day_timeline.next_day %>
9+
<%= link_to "Load more…", user_events_path(@user, day: @day_timeline.next_day.strftime("%Y-%m-%d"), **@filter.as_params),
10+
class: "day-timeline-pagination-link", data: { frame: day_timeline_pagination_frame_id_for(@day_timeline.next_day), pagination_target: "paginationLink" } %>
11+
<% end %>
12+
<% end %>
13+
</div>
14+
</turbo-frame>

app/views/users/show.html.erb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<% @page_title = @user.name %>
2-
3-
<% content_for :header do %>
4-
<%= render "my/menus/menu" %>
5-
<% end %>
2+
<% me_or_you = Current.user == @user ? "me" : @user.first_name %>
63

74
<div class="profile-layout">
85
<section class="panel shadow txt-align-center" style="--panel-size: 45ch;">
@@ -30,8 +27,10 @@
3027
</div>
3128

3229
<div class="flex-inline center justify-center flex-wrap gap">
33-
<%= link_to "Which cards are assigned to #{ Current.user == @user ? "me" : @user.first_name }?", cards_path(assignee_ids: [@user.id], sorted_by: "newest"), class: "btn", data: { turbo_frame: "_top" } %>
34-
<%= link_to "Which cards were added by #{ Current.user == @user ? "me" : @user.first_name }?", cards_path(creator_ids: [@user.id], sorted_by: "newest"), class: "btn", data: { turbo_frame: "_top" } %>
30+
<%= link_to "Which cards are assigned to #{me_or_you}?",
31+
cards_path(assignee_ids: [ @user.id ], sorted_by: "newest"), class: "btn", data: { turbo_frame: "_top" } %>
32+
<%= link_to "Which cards were added by #{me_or_you}?",
33+
cards_path(creator_ids: [ @user.id ], sorted_by: "newest"), class: "btn", data: { turbo_frame: "_top" } %>
3534
</div>
3635
</div>
3736
</section>
@@ -49,10 +48,4 @@
4948
<% end %>
5049
</div>
5150

52-
53-
54-
55-
56-
<h1 class="font-weight-black txt-large margin-block-double"><%= "What #{ Current.user == @user ? "have you" : "has #{ @user.first_name }" } been up to?" %></h1>
57-
58-
<%= render "users/activity_timeline", user: @user, day_timeline: @day_timeline, filter: @filter %>
51+
<%= turbo_frame_tag "user_events", src: user_events_path(@user) %>

config/routes.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
end
88

99
resources :users do
10-
resource :role, module: :users
11-
resources :push_subscriptions, module: :users
10+
scope module: :users do
11+
resource :role
12+
resource :events
13+
resources :push_subscriptions
14+
end
1215
end
1316

1417
resources :collections do
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test_helper"
2+
3+
class Users::EventsControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
sign_in_as :kevin
6+
end
7+
8+
test "show self" do
9+
get user_events_path(users(:kevin))
10+
assert_in_body "What have you been up to?"
11+
end
12+
13+
test "show other" do
14+
get user_events_path(users(:david))
15+
assert_in_body "What has David been up to?"
16+
end
17+
end

0 commit comments

Comments
 (0)