Skip to content

Commit

Permalink
WIP: Removed all extras
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 23, 2025
1 parent 2122c10 commit d7e6547
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 103 deletions.
102 changes: 53 additions & 49 deletions CHANGELOG-10.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ the legacy version was requiring more code)_
The class hierarchy, the mixin files, and the test file structure have been deeply reorganized. The code is cleaner, more concise
and more readable, adding fewer modules to the `ancestors` array of your app.

#### The extras are almost all gone
#### The extras are all gone

They have been converted to autoloaded mixins, or integrated in the core code at zero-cost. You can use the methods that you need,
and they will just work without the need of any explicit `require`.
They have been converted to autoloaded mixins, or integrated in the core code at zero-cost and a few are discontinued.

You can now use the methods that you need, and they will just work without the need of any explicit `require`.

The only extras that are left _(for different compelling reasons)_ are: `gearbox`, `i18n` and `size`. They must be required in the
initializer as usual.
Expand All @@ -91,45 +92,56 @@ Updated the support for the pagy helpers and keynav pagination. Added the plain

### Breaking Changes

#### The `Pagy::DEFAULT` is now frozen

- Remove all the `Pagy::DEFAULT` variables from your code and pass their variables to the paginator method.
- As an alternative to avoid repetitions, define your own `PAGY_DEFAULT = {...}` hash and pass it to the different paginator
methods:
For example: `pagy_offset(scope, **PAGY_DEFAULT, ...)`

#### Direct instantiation of the pagy paginator method classes is not recommended

- Use the provided paginator method helpers for easier usage, maintenance and forward compatibility.

#### Simple renaming (without logic changes)

| Type | Search | Replace | Notes |
|-------------|------------------|-----------------|----------------------------------------------------------|
| Constructor | `pagy(` | `pagy_offset(` | Consistent with the other old and new constructors |
| Function | `Pagy.root` | `Pagy::ROOT` | It's just a Pathname object |
| Variable | `:page_param` | `:page_sym` | It was confusing |
| Variable | `:limit_param` | `:limit_sym` | It was confusing |
| Function | `Pagy.root` | `Pagy::ROOT` | It's just a Pathname object |
| Constructor | `pagy(` | `pagy_offset(` | Consistent with the other old and new constructors |
| Method | `pagy_url_for` | `pagy_page_url` | The legacy naming was causing rails-related expectations |
| Method/args | `label_for(page` | `label(page: ` | The name has changed and `page` is a keywork argument |

#### Direct instantiation of the pagy classes is not recommended

- Use the provided paginators for easier usage, maintenance and forward compatibility.
- Use the classes only if the documentation suggests you to do so, or if you really you know what you are doing.

#### The `Pagy::DEFAULT` is now frozen

- Remove all the `Pagy::DEFAULT` statements and pass their variables to the paginator constructors that you use.
- As an alternative to avoid repetitions, define your own (arbitrarily named) `PAGY_DEFAULT = {...}.freeze` hash and pass it to
the different paginator methods. See the new initializer for details.

#### Replace your `pagy.rb` config file

With no more `Pagy::DEFAULT` and no more extras to require, the statements in your old version are all obsolete but any existing
`Pagy::I18n` configuration, so it's better to start with the new version of the file, and copy over the `Pagy::I18n` (if you used
it).

#### Core changes

- If you used the `:params` variable set to a lambda, ensure that it modifies the passed `query_params` directly. The returned
value is now ignored for a sligtly better performance.

#### Extras Changes

##### `arel`, `array`, `boostrap`, `bulma`, `calendar`, `countless`, `pagy`
All the extras are gone: here is what to

##### `arel`, `array`, `boostrap`, `bulma`, `countless`, `pagy`

- Just use their features without further changes.

##### `calendar`

- Just remove the `require 'pagy/extras/<extra-name>'` from the initializer, and continue to use their features without further
changes.
If you need the I18n localization, discard your old config, and uncomment/add this line to your initializer:
`Pagy::Offset::Calendar.localize_with_rails_i18n_gem(*your_locales)`

##### `elasticsearch_rails`, `meilisearch`, `searchkick`

- Remove the `require 'pagy/extras/<extra-name>'` from the initializer
- Replace any existing `Pagy.new_from_<extra-name>` with `pagy_<extra-name>`. _(Active and passive modes are now handled by the
same helper.)_
same paginator.)_
- Replace any existing `extend Pagy::Search` statement in your models with `extend Pagy::Offset::Search`.
- Remove any existing `:<extra-name>_pagy_search`
variable from your code, and use the standard `pagy_search` method instead. _(the `pagy_search` name customization has been
Expand All @@ -138,70 +150,62 @@ Updated the support for the pagy helpers and keynav pagination. Added the plain

##### `headers`

- Remove the `require 'pagy/extras/headers'` from the initializer.
- Rename any existing `:scaffold_url` to `url_template`
- Remove any existing `Pagy::DEFAULT[:metadata]` variable and pass it to the paginator method

##### `i18n` (discontinued)

- Remove the `require 'pagy/extras/headers'` from the initializer.
- Ucomment the last 2 lines of the `pagy.rb` new initializer, or add them to yours:
```ruby
Pagy::Frontend.prepend(Module.new { def pagy_t(...) = I18n.t(...) })
I18n.load_path += Dir[Pagy::ROOT.join('locales/*.yml')]
```
- Pass any existing `:headers` variable to the paginator method

##### `jsonapi`

- Remove the `require 'pagy/extras/jsonapi'` from the initializer.
- Rename `pagy_jsonapi_links` as `pagy_links`. Notice that the `nil` links are now removed as the `JSON:API`
- Rename any existing `pagy_jsonapi_links` as `pagy_links`. Notice that the `nil` links are now removed as the `JSON:API`
requires.
- You should pass the `:jsonapi` variable to the paginator method when you want to trigger the feature.

##### `keyset`

- Remove the `require 'pagy/extras/keyset'` from the initializer.
- Replace any existing `:jsonify_keyset_attributes` with `stringify_keyset_values`. The lambda receives the same
`keyset_attributes` but it must return the array of attribute values `->(keyset_attributes) { ...; keyset_attributes.values }`.
- Remove any existing`:filter_newest`. Override the `after_cutoff_sql` method instead.

##### `limit`

- Remove the `require 'pagy/extras/limit'` from the initializer.
- You should pass the `:requestable_limit` variable - set to the max limit requestable by the client - to the paginator method
when you want to trigger the feature.

##### `metadata`

- Remove the `require 'pagy/extras/metadata'` from the initializer.
- Rename any existing `:scaffold_url` to `url_template`
- Remove any existing `Pagy::DEFAULT[:metadata]` variable and pass it to the paginator method
- Pass any existing `:metadata` variable to the paginator method

##### `overflow`

- Remove the `require 'pagy/extras/overflow'` from the initializer.
- You should pass the `:overflow: ...` variable to the paginator method when you want to trigger the feature.

#### `standlone`

- Remove the `require 'pagy/extras/standalone'` from the initializer.
- Replace the `:url` variable with `:request`, and its content with `{ url_prefix: 'the-previous-value-of-url' }`. You can also
optionally add your `query_params` hash. For example `{ query_params: { param1: 'abc', param2: 'def' }` for complete control
over the generated url.
- Replace the `:url` variable with `:request` and its content with `{ url_prefix: 'the-previous-value-of-url' }`. You can also
optionally add your `query_params` hash. For example:
`request: { url_prefix: 'the-previous-value-of-url', query_params: { param1: 'abc', param2: 'def' }}` for complete control over
the generated url.

##### `trim` (discontinued)
##### `i18n` (discontinued)

- It was mostly useless and half backed, causing a lot of complications in the ruby and javascript code.
- Use a proper way to address your requirement, like using URL rewriting at the HTTP server level.
- If you REALLY need it, uncomment/add this line to your initializer: `Pagy::Frontend.translate_with_the_slower_i18n_gem!`

##### `gearbox`
##### `gearbox` (discontinued)

- It's in working order, but it will be removed after version 10.0.0, so you will need to keep it up-to-date.
- Copy the file at `Pagy::ROOT.join('pagy/legacy/gearbox')` in your app and require the copy in the initializer.
- Remove the `:gearbox_extra` variable from your code
- You should pass the `gearbox_limit: [...]` variable to the paginator method when you want to trigger the feature.

##### `i18n`, `size`
##### `size` (discontinued)

- It's in working order, but it will be removed after version 10.0.0, so you will need to keep it up-to-date.
- Copy the file at `Pagy::ROOT.join('pagy/legacy/size')` in your app and require the copy in the initializer.

- No change required.
##### `trim` (discontinued)

- It was mostly useless and half backed, causing a lot of complications in the ruby and javascript code.
- Use a proper way to address your requirement, like using URL rewriting at the HTTP server level.

#### Possibly Breaking Overridings

Expand Down
4 changes: 0 additions & 4 deletions gem/apps/demo.ru
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ STYLES = { pagy: { extra: 'pagy', prefix: '', css_anchor: 'pagy-scss' },
bulma: {},
tailwind: { extra: 'pagy', prefix: '', css_anchor: 'pagy-tailwind-css' } }.freeze

# STYLES.each_key do |style|
# require "pagy/extras/#{STYLES[style][:extra] || style}"
# end

# Sinatra setup
require 'sinatra/base'

Expand Down
2 changes: 1 addition & 1 deletion gem/apps/repro.ru
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
gem 'sinatra'
end

# Edit this section adding the extras as needed
# Edit this section adding the legacy as needed
# pagy initializer

# Sinatra setup
Expand Down
34 changes: 13 additions & 21 deletions gem/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@
# Pagy initializer file (9.3.3)


################################ Pagy::DEFAULT #################################
###################################### DEFAULT #######################################
# Customizing the static and frozen Pagy::DEFAULT is NOT SUPPORTED anymore!
# Pass the variables to the paginator method, or pass your own PAGY_DEFAULT hash.
#
# As an alternative to avoid repetitions, define your own (arbitrarily named)
# `PAGY_DEFAULT = {...}.freeze` hash and pass it to the different paginator methods.
# For example:
#
# PAGY_DEFAULT = { limit: 10 }
# PAGY_DEFAULT = { limit: 10 }.freeze
# pagy_offset(collection, **PAGY_DEFAULT, **other_vars)
# pagy_keyset(set, **PAGY_DEFAULT, **other_vars)


########## Extras ##############################################################
# Gearbox extra: Automatically change the limit depending on the page number
# See https://ddnexus.github.io/pagy/docs/extras/gearbox
# require 'pagy/extras/gearbox'
# Then pass e.g.: `gearbox_limit: [15, 30, 60, 100]` to the paginator method to activate the feature

# Size extra: Enable the legacy deprecated Array type for the `:size` variable (e.g. `size: [1,4,4,1]`)
# See https://ddnexus.github.io/pagy/docs/extras/size
# require 'pagy/extras/size'



# Notice that it's just a hash that you can name and define how and where you prefer:
# just remember to pass it along when you need it.


################################# IMPORTANT ####################################
#################################### IMPORTANT #######################################
# Do not configure anything below this line if you use only the :en locale


########## Pagy Translation Besides :en ########################################
############# Pagy Translation Besides :en ###########################################
# Use the pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
# If you want to use the slower I18n gem, skip this and look at the end of this file.
#
Expand All @@ -57,14 +49,14 @@
# pluralize: lambda{ |count| ... } )


########## I18n gem Translation Besides :en ###################################
############# I18n gem Translation Besides :en ######################################
# Uncomment the following line if you REALLY want to switch
# to the standard I18n gem translation:
#
# Pagy.translate_with_the_slower_i18n_gem!
# Pagy::Frontend.translate_with_the_slower_i18n_gem!


########## Calendar Localization Besides :en ###################################
############# Calendar Localization Besides :en ######################################
# The calendar localization data beside :en (which is included), is provided
# by the rails-i18n gem, which should be requested/added by your Gemfile.
# Add the list of locale symbols and comment the following line to enable it,
Expand Down
5 changes: 0 additions & 5 deletions gem/lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ class Pagy

alias pages last

def self.translate_with_the_slower_i18n_gem!
Frontend.prepend(Module.new { def pagy_t(...) = ::I18n.t(...) })
::I18n.load_path += Dir[ROOT.join('locales/*.yml')]
end

# Validates and assign the passed vars: var must be present and value.to_i must be >= to min
def assign_and_check(name_min)
name_min.each do |name, min|
Expand Down
8 changes: 1 addition & 7 deletions gem/lib/pagy/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ def self.included(main)
# :nocov:
main.define_method(:default_request) do
{ request: { url_prefix: 'http://www.example.com/subdir',
query_params: { example: '123' } } }
query_params: { example: '123' } } }
end
# :nocov:
end

# Require the extras passed as arguments
def pagy_extras(*extras)
extras.each { |extra| require "pagy/extras/#{extra}" }
puts "Required extras: #{extras.map(&:inspect).join(', ')}"
end
end
end
6 changes: 6 additions & 0 deletions gem/lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class Pagy
module Frontend
include Url

# Override pagy_t to use ::I18n.t
def self.translate_with_the_slower_i18n_gem!
prepend(Module.new { def pagy_t(...) = ::I18n.t(...) })
::I18n.load_path += Dir[ROOT.join('locales/*.yml')]
end

# Return a performance optimized lambda to generate the HTML anchor element (a tag)
# Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails' link_to
def pagy_anchor(pagy, anchor_string: nil, **vars)
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions test/pagy/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

module PagyConsole
include Pagy::Console
# we are not in the console so we need module_function
module_function :pagy_extras
end

describe 'pagy/console' do
Expand All @@ -15,10 +13,5 @@ module PagyConsole
assert_operator(PagyConsole, :<, Pagy::Backend)
assert_operator(PagyConsole, :<, Pagy::Frontend)
end
it 'requires extras' do
_ { PagyConsole.pagy_extras :gearbox }.must_output "Required extras: :gearbox\n"
_(Pagy::Backend.method_defined?(:pagy_array))
_(Pagy::Frontend.method_defined?(:pagy_nav_js))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require_relative '../../test_helper'
require_relative '../../mock_helpers/app'

describe 'pagy/extras/i18n' do
describe 'pagy/legacy/i18n' do
let(:app) { MockApp.new }

##### pagy.rb initializer ###############
Pagy.translate_with_the_slower_i18n_gem!
Pagy::Frontend.translate_with_the_slower_i18n_gem!
#########################################

describe '#pagy_t with I18n' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ pagy/extras/i18n___pagy_info_with_I18n_test_0001_renders_info:
:info_1: <span class="pagy info">Displaying 1 item</span>
:info_13: <span class="pagy info">Displaying 13 items</span>
:info_100: <span class="pagy info">Displaying items 41-60 of 100 in total</span>
pagy/legacy/i18n___pagy_info_with_I18n_test_0001_renders_info:
:info_0: <span class="pagy info">No items found</span>
:info_1: <span class="pagy info">Displaying 1 item</span>
:info_13: <span class="pagy info">Displaying 13 items</span>
:info_100: <span class="pagy info">Displaying items 41-60 of 100 in total</span>
4 changes: 2 additions & 2 deletions test/pagy/frontend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def i18n_load(*locales)
describe 'pagy/frontend' do
let(:app) { MockApp.new }

# #pagy_nav helper tests in the test/extras/offset_test.rb
# #pagy_nav helper tests in the test/legacy/offset_test.rb

describe '#pagy_anchor' do
it 'renders with extras' do
it 'renders with legacy' do
pagy = Pagy::Offset.new(count: 103, page: 1)
_(app.pagy_anchor(pagy, anchor_string: 'X').call(3)).must_equal '<a X href="/foo?page=3">3</a>'
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require 'pagy/extras/gearbox'
require 'pagy/legacy/gearbox'
require 'pagy/offset/countless'

describe 'pagy/extras/gearbox' do
describe 'pagy/legacy/gearbox' do
describe '#assign_limit' do
it 'raises VariableErrors for wrong limit types' do
_ { Pagy::Offset.new(count: 3, page: 1, gearbox_limit: [-1, 10]) }.must_raise Pagy::VariableError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require 'pagy/extras/size'
require 'pagy/legacy/size'

describe 'pagy/extras/size' do
describe 'pagy/legacy/size' do
describe '#series (size = Array)' do
before do
@vars0 = { count: 103,
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/offset/overflow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Lazy trick to load ActiveSupport and crap while silencing the warnings
calendar = Pagy::Offset::Calendar
puts calendar
calendar.is_a?(Pagy)

Time.zone = 'EST'
Date.beginning_of_week = :sunday
Expand Down

0 comments on commit d7e6547

Please sign in to comment.