Skip to content

Commit 5a829d9

Browse files
authored
normalize the page number for pagy (#598)
1 parent 3943f73 commit 5a829d9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

app/controllers/topics_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TopicsController < ApplicationController
77
def index
88
@topics = Topic.approved.with_talks.order(name: :asc)
99
@topics = @topics.where("lower(name) LIKE ?", "#{params[:letter].downcase}%") if params[:letter].present?
10-
@pagy, @topics = pagy(@topics, limit: 100, page: params[:page])
10+
@pagy, @topics = pagy(@topics, limit: 100, page: page_number)
1111
end
1212

1313
def show
@@ -17,7 +17,7 @@ def show
1717
gearbox_extra: true,
1818
gearbox_limit: [12, 24, 48, 96],
1919
overflow: :empty_page,
20-
page: params[:page]&.to_i || 1
20+
page: page_number
2121
)
2222
end
2323

@@ -26,4 +26,10 @@ def set_user_favorites
2626

2727
@user_favorite_talks_ids = Current.user.default_watch_list.talks.ids
2828
end
29+
30+
private
31+
32+
def page_number
33+
[params[:page]&.to_i, 1].compact.max
34+
end
2935
end

test/controllers/topics_controller_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class TopicsControllerTest < ActionDispatch::IntegrationTest
2222
assert_select "##{dom_id(@topic2)}", 0
2323
end
2424

25+
test "should get index with invalid page number" do
26+
get topics_url(page: "'")
27+
assert_response :success
28+
assert_select "h1", "Topics"
29+
assert_select "##{dom_id(@topic1)} > span", "1"
30+
assert_select "##{dom_id(@topic2)}", 0
31+
end
32+
2533
test "should get show" do
2634
get topic_url(@topic1)
2735
assert_response :success

0 commit comments

Comments
 (0)