Skip to content

Commit

Permalink
normalize the page number for pagy (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly authored Jan 23, 2025
1 parent 3943f73 commit 5a829d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TopicsController < ApplicationController
def index
@topics = Topic.approved.with_talks.order(name: :asc)
@topics = @topics.where("lower(name) LIKE ?", "#{params[:letter].downcase}%") if params[:letter].present?
@pagy, @topics = pagy(@topics, limit: 100, page: params[:page])
@pagy, @topics = pagy(@topics, limit: 100, page: page_number)
end

def show
Expand All @@ -17,7 +17,7 @@ def show
gearbox_extra: true,
gearbox_limit: [12, 24, 48, 96],
overflow: :empty_page,
page: params[:page]&.to_i || 1
page: page_number
)
end

Expand All @@ -26,4 +26,10 @@ def set_user_favorites

@user_favorite_talks_ids = Current.user.default_watch_list.talks.ids
end

private

def page_number
[params[:page]&.to_i, 1].compact.max
end
end
8 changes: 8 additions & 0 deletions test/controllers/topics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class TopicsControllerTest < ActionDispatch::IntegrationTest
assert_select "##{dom_id(@topic2)}", 0
end

test "should get index with invalid page number" do
get topics_url(page: "'")
assert_response :success
assert_select "h1", "Topics"
assert_select "##{dom_id(@topic1)} > span", "1"
assert_select "##{dom_id(@topic2)}", 0
end

test "should get show" do
get topic_url(@topic1)
assert_response :success
Expand Down

0 comments on commit 5a829d9

Please sign in to comment.