Repository navigation
Harden the Paginate constructor against missing/invalid config - #213
Conversation
|
CI is on |
Paginate#base_path to "/" instead of nilPaginate constructor against missing/invalid config
|
Updated: added |
|
Hi, when you have a moment, could you take a look at this one? It's the same kind of small hardening as the ones you merged last week, and I'm happy to change anything. |
|
Friendly ping: this is ready for review whenever someone has a moment. Happy to adjust anything. |
`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.
`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.
8f5fcb5 to
a35f186
Compare
|
Thanks for these (and the ping) @pcbeingused333 ❤️ |
|
Perron is getting more solid because of your tokens! 💪 |
Two ways
Perron::Paginate.newcrashes on config that a caller can realistically hand it.1.
base_pathomitted →NoMethodErrorbase_path:is an optional keyword (base_path: nil), but omitting it breaksnext/previousonce the collection has more than one page:previousfrom page 2 also returnsnileven thoughprevious?istrue. Fixed by defaultingbase_pathto"/"(and coercing an explicitnil), so an omittedbase_pathyields root-relative paths (/page/2/,/).2. non-positive
per_page→ cryptic errorsconfig.pagination.per_pagehas no default (onlypath_templateis set inResource::Configuration), so an unconfigured collection reachesPaginate.new(per_page: nil):per_pagenilTypeError: nil can't be coerced into Float0FloatDomainError: InfinityArgumentError: min argument must be smaller than max argument(fromclamp)Now validated up front:
ArgumentError: per_page must be a positive integer, got nil.Tests
Four new cases in
paginate_test.rb(next/previouswithbase_pathomitted and explicitlynil;per_pageof0/-5/nil/2.5). All fail onmain.bundle exec rakeis green (405 runs, 0 failures;standardrbclean).🤖 Generated with Claude Code