From d7e6547ad51f4f59a8803330cfbcc4423458fbac Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Thu, 23 Jan 2025 17:41:22 +0700 Subject: [PATCH] WIP: Removed all extras --- CHANGELOG-10.md | 102 +++++++++--------- gem/apps/demo.ru | 4 - gem/apps/repro.ru | 2 +- gem/config/pagy.rb | 34 +++--- gem/lib/pagy.rb | 5 - gem/lib/pagy/console.rb | 8 +- gem/lib/pagy/frontend.rb | 6 ++ gem/lib/pagy/{extras => legacy}/gearbox.rb | 0 gem/lib/pagy/{extras => legacy}/size.rb | 0 test/pagy/console_test.rb | 7 -- test/pagy/{extras => frontend}/i18n_test.rb | 4 +- .../{extras => frontend}/i18n_test.rb.yaml | 5 + test/pagy/frontend_test.rb | 4 +- test/pagy/{extras => legacy}/gearbox_test.rb | 4 +- test/pagy/{extras => legacy}/size_test.rb | 4 +- .../i18n_calendar_localization_test.rb | 0 test/pagy/offset/overflow_test.rb | 2 +- 17 files changed, 88 insertions(+), 103 deletions(-) rename gem/lib/pagy/{extras => legacy}/gearbox.rb (100%) rename gem/lib/pagy/{extras => legacy}/size.rb (100%) rename test/pagy/{extras => frontend}/i18n_test.rb (93%) rename test/pagy/{extras => frontend}/i18n_test.rb.yaml (50%) rename test/pagy/{extras => legacy}/gearbox_test.rb (98%) rename test/pagy/{extras => legacy}/size_test.rb (98%) rename test/pagy/{extras => offset}/i18n_calendar_localization_test.rb (100%) diff --git a/CHANGELOG-10.md b/CHANGELOG-10.md index 6033a4b5d..c1cde33de 100644 --- a/CHANGELOG-10.md +++ b/CHANGELOG-10.md @@ -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. @@ -91,28 +92,34 @@ 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 @@ -120,16 +127,21 @@ Updated the support for the pagy helpers and keynav pagination. Added the plain #### 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/'` 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/'` from the initializer - Replace any existing `Pagy.new_from_` with `pagy_`. _(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 `:_pagy_search` variable from your code, and use the standard `pagy_search` method instead. _(the `pagy_search` name customization has been @@ -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 diff --git a/gem/apps/demo.ru b/gem/apps/demo.ru index 73cf9482f..a7a616eb9 100644 --- a/gem/apps/demo.ru +++ b/gem/apps/demo.ru @@ -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' diff --git a/gem/apps/repro.ru b/gem/apps/repro.ru index 073f8bce5..ee1418544 100644 --- a/gem/apps/repro.ru +++ b/gem/apps/repro.ru @@ -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 diff --git a/gem/config/pagy.rb b/gem/config/pagy.rb index 336960f98..51fc48d53 100644 --- a/gem/config/pagy.rb +++ b/gem/config/pagy.rb @@ -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. # @@ -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, diff --git a/gem/lib/pagy.rb b/gem/lib/pagy.rb index f5e53ebdb..f3770c546 100644 --- a/gem/lib/pagy.rb +++ b/gem/lib/pagy.rb @@ -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| diff --git a/gem/lib/pagy/console.rb b/gem/lib/pagy/console.rb index f08dc9467..d0a9a937a 100644 --- a/gem/lib/pagy/console.rb +++ b/gem/lib/pagy/console.rb @@ -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 diff --git a/gem/lib/pagy/frontend.rb b/gem/lib/pagy/frontend.rb index 47cb63195..bef6d61df 100644 --- a/gem/lib/pagy/frontend.rb +++ b/gem/lib/pagy/frontend.rb @@ -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) diff --git a/gem/lib/pagy/extras/gearbox.rb b/gem/lib/pagy/legacy/gearbox.rb similarity index 100% rename from gem/lib/pagy/extras/gearbox.rb rename to gem/lib/pagy/legacy/gearbox.rb diff --git a/gem/lib/pagy/extras/size.rb b/gem/lib/pagy/legacy/size.rb similarity index 100% rename from gem/lib/pagy/extras/size.rb rename to gem/lib/pagy/legacy/size.rb diff --git a/test/pagy/console_test.rb b/test/pagy/console_test.rb index f958393c5..80f75b345 100644 --- a/test/pagy/console_test.rb +++ b/test/pagy/console_test.rb @@ -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 @@ -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 diff --git a/test/pagy/extras/i18n_test.rb b/test/pagy/frontend/i18n_test.rb similarity index 93% rename from test/pagy/extras/i18n_test.rb rename to test/pagy/frontend/i18n_test.rb index da19b4517..c7355c344 100644 --- a/test/pagy/extras/i18n_test.rb +++ b/test/pagy/frontend/i18n_test.rb @@ -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 diff --git a/test/pagy/extras/i18n_test.rb.yaml b/test/pagy/frontend/i18n_test.rb.yaml similarity index 50% rename from test/pagy/extras/i18n_test.rb.yaml rename to test/pagy/frontend/i18n_test.rb.yaml index 048e659a0..cc89e1682 100644 --- a/test/pagy/extras/i18n_test.rb.yaml +++ b/test/pagy/frontend/i18n_test.rb.yaml @@ -4,3 +4,8 @@ pagy/extras/i18n___pagy_info_with_I18n_test_0001_renders_info: :info_1: Displaying 1 item :info_13: Displaying 13 items :info_100: Displaying items 41-60 of 100 in total +pagy/legacy/i18n___pagy_info_with_I18n_test_0001_renders_info: + :info_0: No items found + :info_1: Displaying 1 item + :info_13: Displaying 13 items + :info_100: Displaying items 41-60 of 100 in total diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb index f6e3a89c7..1ac3ca3b2 100644 --- a/test/pagy/frontend_test.rb +++ b/test/pagy/frontend_test.rb @@ -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 '3' end diff --git a/test/pagy/extras/gearbox_test.rb b/test/pagy/legacy/gearbox_test.rb similarity index 98% rename from test/pagy/extras/gearbox_test.rb rename to test/pagy/legacy/gearbox_test.rb index b00ab0a39..c86356c73 100644 --- a/test/pagy/extras/gearbox_test.rb +++ b/test/pagy/legacy/gearbox_test.rb @@ -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 diff --git a/test/pagy/extras/size_test.rb b/test/pagy/legacy/size_test.rb similarity index 98% rename from test/pagy/extras/size_test.rb rename to test/pagy/legacy/size_test.rb index 59a2cf8ff..a82529b93 100644 --- a/test/pagy/extras/size_test.rb +++ b/test/pagy/legacy/size_test.rb @@ -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, diff --git a/test/pagy/extras/i18n_calendar_localization_test.rb b/test/pagy/offset/i18n_calendar_localization_test.rb similarity index 100% rename from test/pagy/extras/i18n_calendar_localization_test.rb rename to test/pagy/offset/i18n_calendar_localization_test.rb diff --git a/test/pagy/offset/overflow_test.rb b/test/pagy/offset/overflow_test.rb index 29128c8fc..4c9338771 100644 --- a/test/pagy/offset/overflow_test.rb +++ b/test/pagy/offset/overflow_test.rb @@ -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