Skip to content

Commit

Permalink
WIP: small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Feb 6, 2025
1 parent 8da6d54 commit 21817be
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 161 deletions.
37 changes: 37 additions & 0 deletions .idea/runConfigurations/Test_Only.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions CHANGELOG-10.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ Added `Pagy::Javascript.install` function to avoid messing up with complicated j

No setup required. The locales and their pluralization are autoloaded when you app use them.

The code is simpler and lighter, you can also override the lookup of dictionary files with `Pagy::I18n::PATHNAMES.unshift(Pathname.new('my/customized/dictionaries'))`.
The code is simpler and lighter, you can also override the lookup of dictionary files with
`Pagy::I18n::PATHNAMES.unshift(Pathname.new('my/customized/dictionaries'))`.

### Breaking Changes

Expand Down Expand Up @@ -137,10 +138,10 @@ to start with the new version of the file.

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

- The `Pagy::DEFAULT` is now an internal hash and it's frozen. If you had any default set there, you should pass the options to
the paginator constructors.
- As an alternative to avoid repetitions, define your own default hash and pass it to the different paginator methods/helpers.
- See the new initializer for details.
- The `Pagy::DEFAULT` is now an internal hash and it's frozen. If you set any option with it, you should pass them to the
paginator methods.
- As an alternative to avoid repetitions, define your own PAGY_OPTIONS hash and pass it to the different paginator
methods/helpers. See the new initializer for details.

#### Core changes

Expand Down
6 changes: 3 additions & 3 deletions gem/apps/demo.ru
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ require 'sinatra/base'
# Sinatra application
class PagyDemo < Sinatra::Base
include Pagy::Backend
PAGY_DEFAULT = { requestable_limit: 100 }.freeze
PAGY_OPTIONS = { requestable_limit: 100 }.freeze

get '/' do
redirect '/pagy'
end

get '/template' do
collection = MockCollection.new
@pagy, @records = pagy_offset(collection, **PAGY_DEFAULT)
@pagy, @records = pagy_offset(collection, **PAGY_OPTIONS)

erb :template, locals: { pagy: @pagy, style: 'pagy' }
end
Expand All @@ -79,7 +79,7 @@ class PagyDemo < Sinatra::Base

get("/#{style}") do
collection = MockCollection.new
@pagy, @records = pagy_offset(collection, **PAGY_DEFAULT)
@pagy, @records = pagy_offset(collection, **PAGY_OPTIONS)

erb :helpers, locals: { style:, prefix: }
end
Expand Down
4 changes: 2 additions & 2 deletions gem/apps/keynav.ru
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require 'sinatra/base'
# Sinatra application
class PagyKeynav < Sinatra::Base
include Pagy::Backend
PAGY_DEFAULT = { limit: 4, requestable_limit: 100 }.freeze
PAGY_OPTIONS = { limit: 4, requestable_limit: 100 }.freeze

get('/javascripts/:file') do
format = params[:file].split('.').last
Expand All @@ -52,7 +52,7 @@ class PagyKeynav < Sinatra::Base
Time.zone = 'UTC'

@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
@pagy, @pets = pagy_keynav_js(Pet.order(@order), **PAGY_DEFAULT)
@pagy, @pets = pagy_keynav_js(Pet.order(@order), **PAGY_OPTIONS)
@ids = @pets.pluck(:id)
erb :main
end
Expand Down
2 changes: 1 addition & 1 deletion gem/apps/keyset.ru
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require 'sinatra/base'
# Sinatra application
class PagyKeyset < Sinatra::Base
include Pagy::Backend
PAGY_DEFAULT = { limit: 10, requestable_limit: 100 }.freeze
PAGY_OPTIONS = { limit: 10, requestable_limit: 100 }.freeze

# Root route/action
get '/' do
Expand Down
2 changes: 1 addition & 1 deletion gem/apps/keyset_sequel.ru
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require 'logger'
# Sinatra application
class PagyKeysetSequel < Sinatra::Base
include Pagy::Backend
PAGY_DEFAULT = { limit: 10, requestable_limit: 100 }.freeze
PAGY_OPTIONS = { limit: 10, requestable_limit: 100 }.freeze

# Root route/action
get '/' do
Expand Down
4 changes: 2 additions & 2 deletions gem/apps/rails.ru
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ end
class CommentsController < ActionController::Base # :nodoc:
include Rails.application.routes.url_helpers
include Pagy::Backend
PAGY_DEFAULT = { limit: 10,
PAGY_OPTIONS = { limit: 10,
requestable_limit: 100 }.freeze

def index
@pagy, @comments = pagy_offset(Comment.all, **PAGY_DEFAULT)
@pagy, @comments = pagy_offset(Comment.all, **PAGY_OPTIONS)
# pagy_headers_merge(@pagy)
render inline: TEMPLATE
end
Expand Down
8 changes: 4 additions & 4 deletions gem/apps/repro.ru
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require 'sinatra/base'
# Sinatra application
class PagyRepro < Sinatra::Base
include Pagy::Backend
PAGY_DEFAULT = { requestable_limit: 100 }.freeze
PAGY_OPTIONS = { requestable_limit: 100 }.freeze

get('/javascripts/:file') do
format = params[:file].split('.').last
Expand All @@ -52,9 +52,9 @@ class PagyRepro < Sinatra::Base
# Edit this action as needed
get '/' do
collection = MockCollection.new
@pagy, @records = pagy_offset(collection, **PAGY_DEFAULT)
# @pagy, @records = pagy_countless(collection, **PAGY_DEFAULT)
# @pagy, @records = pagy_array(Array(1..1000), **PAGY_DEFAULT)
@pagy, @records = pagy_offset(collection, **PAGY_OPTIONS)
# @pagy, @records = pagy_countless(collection, **PAGY_OPTIONS)
# @pagy, @records = pagy_array(Array(1..1000), **PAGY_OPTIONS)
erb :main
end

Expand Down
22 changes: 9 additions & 13 deletions gem/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,31 @@
###################################### DEFAULT #######################################
# Customizing the static and frozen Pagy::DEFAULT is NOT SUPPORTED anymore!
#
# As an alternative to avoid repetitions, define your own default hash and pass it
# As an alternative to avoid repetitions, define your own options hash and pass it
# to the different paginator methods.
# For example:
#
# PAGY_DEFAULT = { limit: 10 }.freeze
# pagy_offset(collection, **PAGY_DEFAULT, **other_vars)
# pagy_keyset(set, **PAGY_DEFAULT, **other_vars)
# PAGY_OPTIONS = { limit: 10 }.freeze
# pagy_offset(collection, **PAGY_OPTIONS, **other_options)
# pagy_keyset(set, **PAGY_OPTIONS, **other_options)


############ Install Pagy Javascript #####################################################
# If you use any pagy method ending with '*_js',
# uncomment/customize one of the following:
# If you use any pagy method ending with '*_js', uncomment/customize one of the following:

# Generic reference to customize
# Available file formats: 'pagy.mjs', 'pagy.js', 'pagy.js.map', 'pagy.min.js'
# Pagy::Javascript.install('pagy.mjs', 'pagy.js', ..., javascript_path) if env_dev

# Example for Rails
# javascript_path = Rails.root.join('app/javascript')
# Install all files...
# Pagy::Javascript.install(javascript_path)) if Rails.env.development?
# Install only 'pagy.mjs'...
# Pagy::Javascript.install('pagy.mjs', javascript_path) if Rails.env.development?
# Install 'pagy.mjs' in the dir 'app/javascript'
# Pagy::Javascript.install('pagy.mjs', 'app/javascript') if Rails.env.development?


############# Overriding Pagy::I18n dictionary lookup ######################################
# Override the dictionary lookup for customization. Just drop your customized
# dictionary/dictionaries in a dir and add its pathname to the lookup:
# Pagy::I18n::PATHNAMES.unshift(Pathname.new('my/customized/dictionaries'))
# dictionary/dictionaries in a dir and unshift its pathname to the lookup:
# Pagy::I18n::LOOKUP_PATHNAMES.unshift(Pathname.new('my/customized/dictionaries'))


############# I18n gem translation ######################################
Expand Down
9 changes: 5 additions & 4 deletions gem/lib/pagy/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module I18n
extend self

# rubocop:disable Style/MutableConstant
PATHNAMES = [ROOT.join('locales')]
DATA = {}
LOOKUP_PATHNAMES = [ROOT.join('locales')]
DATA = {}
# rubocop:enable Style/MutableConstant

# Translate and pluralize the key with the locale DATA
Expand All @@ -24,7 +24,7 @@ def translate(key, locale:, **options)
private

def load(locale)
path = PATHNAMES.map { |p| p.join("#{locale}.yml") }.find(&:exist?)
path = LOOKUP_PATHNAMES.map { |p| p.join("#{locale}.yml") }.find(&:exist?)
raise Errno::ENOENT, "missing dictionary file for #{locale.inspect} locale" unless path

dictionary = YAML.load_file(path)[locale]
Expand All @@ -33,8 +33,9 @@ def load(locale)
end

# Create a flat hash with dotted notation keys
# e.g. { 'a' => { 'b' => {'c' => 3, 'd' => 4 }}} -> { 'a.b.c' => 3, 'a.b.d' => 4 }
def flatten_to_dot_keys(initial, prefix = '')
initial.each.reduce({}) do |hash, (key, value)|
initial.reduce({}) do |hash, (key, value)|
hash.merge!(value.is_a?(Hash) ? flatten_to_dot_keys(value, "#{prefix}#{key}.") : { "#{prefix}#{key}" => value })
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/backend/keynav_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

describe 'Keynav' do
[Pet, PetSequel].each do |model|
describe 'pagy_augmented' do
describe "pagy_augmented #{model}" do
it 'works for page 1' do
app = MockApp.new(params: {})
pagy, records = app.send(:pagy_keynav_js,
Expand Down
4 changes: 2 additions & 2 deletions test/pagy/backend/keyset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe 'keyset' do
[Pet, PetSequel].each do |model|
describe '#pagy_keyset' do
describe "#pagy_keyset #{model}" do
it 'returns Pagy::Keyset object and records' do
app = MockApp.new(params: { page: nil })
pagy, records = app.send(:pagy_keyset,
Expand All @@ -27,7 +27,7 @@
_(pagy.next).must_equal "WzIwXQ"
end
end
describe 'URL helpers' do
describe "URL helpers #{model}" do
it 'returns the URLs for first page' do
app = MockApp.new(params: { page: nil, limit: 10 })
pagy, _records = app.send(:pagy_keyset,
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/frontend/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative '../../test_helper'
require_relative '../../mock_helpers/app'

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

##### pagy.rb initializer ###############
Expand Down
5 changes: 5 additions & 0 deletions test/pagy/frontend/i18n_test.rb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pagy/legacy/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/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>
Loading

0 comments on commit 21817be

Please sign in to comment.