skip to content

When upgrading a Rails app from Pagy 9 to Pagy 43, which controller, view and initializer code must change, and how does out-of-range page handling differ?

level: seniorimportance: should knowfreq 24%

answer

  1. the version jumped from 9.3 to 43
  2. Backend becomes Method
  3. pagy_nav(@pagy) becomes @pagy.series_nav
  4. DEFAULT becomes OPTIONS, then freeze
  5. empty page unless raise_range_error

basics

~10 s

Replace include Pagy::Backend with Pagy::Method, drop Pagy::Frontend, turn pagy_nav(@pagy)-style helpers into @pagy methods, move Pagy::DEFAULT settings to Pagy::OPTIONS and delete the extras. Out-of-range pages now render empty by default instead of raising.

solid answer

~30 s

Pagy 43 (released after 9.3.4) is a complete redesign. In controllers, `include Pagy::Backend` becomes `include Pagy::Method`, and the per-technique methods fold into one: `pagy_countless(scope)` becomes `pagy(:countless, scope)`, `pagy_array` becomes `pagy(:offset, array)`, `pagy_keyset` becomes `pagy(:keyset, ...)`. In views `include Pagy::Frontend` goes, and helpers become methods on the object: `pagy_nav(@pagy)` to `@pagy.series_nav`, `pagy_info` to `info_tag`, `pagy_headers` to `headers_hash`, `pagy_metadata` to `data_hash`. In the initializer, `Pagy::DEFAULT[...]` becomes `Pagy::OPTIONS[...]` followed by `Pagy::OPTIONS.freeze`, and every `require 'pagy/extras/...'` goes. Out of range, Pagy 43 serves an empty page by default; `raise_range_error: true` raises `Pagy::RangeError`, which replaces `Pagy::OverflowError`.

code

ruby · 6 lines
ruby
# config/initializers/pagy.rb (Pagy 43)
Pagy::OPTIONS[:limit]        = 25
Pagy::OPTIONS[:client_limit] = 100
Pagy::OPTIONS[:slots]        = 9      # was size:
Pagy::OPTIONS.freeze
# require 'pagy/extras/overflow'      # delete: the extras are gone

go deeper

for a junior

Recognise pre-43 Pagy code on sight: pagy_nav(@pagy), Pagy::Backend and require 'pagy/extras/...' all mean an old version.

for a middle

Map each old helper and option to its Pagy 43 form, and explain why Pagy::DEFAULT writes fail and where Pagy::OPTIONS goes.

for a senior

Plan the upgrade end to end: the Ruby 3.3 floor, the overflow change and its effect on error pages, the renamed headers for API clients, and pagy/next in CI.

for a principal

Weigh upgrading a gem that redesigned its API against swapping libraries, and set how far dependency majors may lag behind.

## Why this upgrade is not a version bump Pagy's numbering jumped from **9.3.4 to 43.0.0** to signal a **complete redesign**: different module, different helper style, no extras. Pagy 43 also requires **Ruby 3.3 or newer** (the gemspec's `required_ruby_version`). Pagy's upgrade guide suggests renaming the old initializer to `pagy-old.rb`, adding the new one, and working through the old file until it is empty. ## Controllers | Pagy 9 | Pagy 43 | |---|---| | `include Pagy::Backend` | `include Pagy::Method` | | `pagy(scope)` | `pagy(:offset, scope)` (`:offset` is also the default) | | `pagy_countless(scope)` | `pagy(:countless, scope)` | | `pagy_array(array)` | `pagy(:offset, array)` | | `pagy_arel(scope)` | `pagy(:offset, scope, count_over: true)` | | `pagy_keyset(set)` | `pagy(:keyset, set)` | | `response.headers.merge!(pagy_headers(@pagy))` | `response.headers.merge!(@pagy.headers_hash)` | ## Views `include Pagy::Frontend` is removed: helpers are **instance methods of `@pagy`**. | Pagy 9 | Pagy 43 | |---|---| | `pagy_nav(@pagy)` | `@pagy.series_nav` | | `pagy_bootstrap_nav(@pagy)` | `@pagy.series_nav(:bootstrap)` | | `pagy_nav_js(@pagy)` | `@pagy.series_nav_js` | | `pagy_combo_nav_js(@pagy)` | `@pagy.input_nav_js` | | `pagy_info(@pagy)` | `@pagy.info_tag` | | `pagy_prev_url(@pagy)` / `pagy_next_url(@pagy)` | `@pagy.page_url(:previous)` / `@pagy.page_url(:next)` | | `pagy_metadata(@pagy)` | `@pagy.data_hash` | ## Initializer and option renames 1. `Pagy::DEFAULT[:limit] = 25` becomes `Pagy::OPTIONS[:limit] = 25`. `Pagy::DEFAULT` still exists but is a **frozen** internal constant, so writing to it raises `FrozenError`. The new initializer ends with `Pagy::OPTIONS.freeze`. 2. Delete every `require 'pagy/extras/...'`: the files no longer exist, so leaving one raises `LoadError` at boot. Their features are in the core, behind options. 3. Option renames include `size:` to `slots:`, `ends: false` to `compact: true`, `page_param: :p` to `page_key: 'p'` (now a String), `@pagy.vars` to `@pagy.options`, `VariableError` to `OptionError`, and `prev` to `previous` in every name. The `:params` option gives way to a `querify:` lambda that edits the string-keyed params hash. 4. The old `limit` extra becomes `client_limit: 100`. In the latest releases the interim names `:max_limit` and `:client_max_limit` still work but print a deprecation warning. 5. `headers_hash` names the limit header `page-limit`, not `page-items`; set `headers_map:` to keep the old name for existing API clients. `:items` is a separate, older trap: it became `:limit` in **Pagy 9.0**, so an app still on Pagy 8 has that rename to do as well. ## Out-of-range pages - Pagy 9 without the `overflow` extra **raised** `Pagy::OverflowError` for `?page=60` on a 50-page list. - Pagy 43 replaces it with **`Pagy::RangeError`** and does **not** raise by default. It serves an **empty page**: no records, `from`/`to` at 0, and `previous` pointing at the last page. That matches the old `overflow: :empty_page` setting. - `raise_range_error: true` restores raising, for example to `rescue_from Pagy::RangeError` and redirect. - `overflow: :last_page` is **discontinued**. `@pagy.overflow?` became `@pagy.in_range?`, with the opposite truth value. ## An order of work that keeps the app booting 1. Bump the gem and check the Ruby floor (3.3 or newer). 2. Swap the initializer: move each `Pagy::DEFAULT` line to `Pagy::OPTIONS`, delete the extras' `require` lines, and end with `Pagy::OPTIONS.freeze`. 3. Replace `include Pagy::Backend` with `include Pagy::Method` and remove `include Pagy::Frontend` from helpers. 4. Rewrite each controller call with an explicit paginator symbol and each view helper as an `@pagy` method. 5. Decide on out-of-range pages: keep the empty page, or set `raise_range_error: true` and handle `Pagy::RangeError` where the app used to rescue `Pagy::OverflowError`. 6. Re-run request and system tests that click through pagination, and compare any API headers against what clients expect. ## Finding what you missed - Grep for `pagy_`, `Pagy::DEFAULT`, `Backend`, `Frontend` and `extras`. - Require `pagy/next` (`gem 'pagy', require: 'pagy/next'`) in development or CI: options discontinued for the next major, such as `:max_pages`, `:max_limit` and `:client_max_limit`, then raise `Pagy::NextError` instead of only warning. - Re-check custom CSS: the default nav markup and its selectors changed, while the Bootstrap and Bulma styles need no change.

  • The upgraded app now shows a blank list for stale bookmarked page links instead of an error page. Is that a bug?
    No. Pagy 43 rescues its own `Pagy::RangeError` and serves an empty page by default, and `previous` points at the last real page so the nav still leads back. If the product wants the old behaviour, or a redirect to the last page, set `raise_range_error: true` and handle `Pagy::RangeError` in the controller.
  • How can CI catch options that Pagy 43 still accepts but will drop in its next major?
    Load Pagy through `require: 'pagy/next'` (or set `PAGY_NEXT=true`). That entry point skips the deprecation layer, so discontinued options such as `:max_pages`, `:max_limit` and `:client_max_limit` raise `Pagy::NextError` instead of printing a warning, and a test run surfaces every call site.

  • Keeps require 'pagy/extras/overflow' in the initializer after upgrading to 43.
  • Sets Pagy::DEFAULT[:limit] in the 43 initializer instead of Pagy::OPTIONS.
  • Expects Pagy 43 to raise Pagy::OverflowError for a page past the end.
  • Assumes Pagy::Frontend must stay included for the view helpers.
  • Believes the jump to 43 only renamed :items to :limit.

Assumes an upgrade from Pagy 9.3 to Pagy 43.7 on Rails 8.1; Pagy 43 needs Ruby 3.3 or newer, and :items had already become :limit in Pagy 9.0.