Skip to content

Harden the Paginate constructor against missing/invalid config - #213

Open
pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/paginate-default-base-path
Open

pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/paginate-default-base-path

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Sep 8, 2026

Copy link
Copy Markdown

Two ways Perron::Paginate.new crashes on config that a caller can realistically hand it.

1. base_path omitted → NoMethodError

base_path: is an optional keyword (base_path: nil), but omitting it breaks next / previous once the collection has more than one page:

Perron::Paginate.new((1..10).to_a, page: 1, per_page: 5).next
# => NoMethodError: undefined method `sub' for nil

previous from page 2 also returns nil even though previous? is true. Fixed by defaulting base_path to "/" (and coercing an explicit nil), so an omitted base_path yields root-relative paths (/page/2/, /).

2. non-positive per_page → cryptic errors

config.pagination.per_page has no default (only path_template is set in Resource::Configuration), so an unconfigured collection reaches Paginate.new(per_page: nil):

per_page current behaviour
nil TypeError: nil can't be coerced into Float
0 FloatDomainError: Infinity
negative ArgumentError: min argument must be smaller than max argument (from clamp)

Now validated up front: ArgumentError: per_page must be a positive integer, got nil.

Tests

Four new cases in paginate_test.rb (next/previous with base_path omitted and explicitly nil; per_page of 0 / -5 / nil / 2.5). All fail on main. bundle exec rake is green (405 runs, 0 failures; standardrb clean).

🤖 Generated with Claude Code

`base_path` is an optional keyword argument, but when it is omitted (or
passed as `nil`) and the collection spans more than one page, `next` and
`previous` build the path with `@base_path.sub(...)` and raise
`NoMethodError: undefined method 'sub' for nil`.

Default it to `"/"` (and coerce an explicit `nil`), so an omitted
`base_path` yields root-relative paths like `/page/2/` instead of
crashing. Callers that pass a `base_path` are unaffected.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
@pcbeingused333

Copy link
Copy Markdown
Author

CI is on action_required (fork gate) — could you press "Approve and run workflows"? Verified locally with bundle exec rake (Docker, Ruby 3.4): 404 runs, 0 failures, standardrb clean. The three new tests fail on main (NoMethodError: undefined method 'sub' for nil, and previous returning nil).

`config.pagination.per_page` has no default, so an unconfigured collection
reaches `Paginate.new(per_page: nil)` and dies with
`TypeError: nil can't be coerced into Float`. `per_page: 0` gives
`FloatDomainError: Infinity`, and a negative value gives
`ArgumentError: min argument must be smaller than max argument` from the
`clamp`. Validate up front with a message that says what is wrong.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
@pcbeingused333 pcbeingused333 changed the title Default Paginate#base_path to "/" instead of nil Harden the Paginate constructor against missing/invalid config Sep 8, 2026
@pcbeingused333

Copy link
Copy Markdown
Author

Updated: added per_page validation (an unconfigured pagination.per_page currently reaches Paginate.new(per_page: nil)TypeError). Still one file of behaviour + one test file. bundle exec rake green — 405 runs, 0 failures, standardrb clean. CI is still action_required (fork gate) if a maintainer can approve the run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant