Skip to content

Commit

Permalink
WIP: Super refactoring 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 9, 2025
1 parent 57d02fa commit f28f576
Show file tree
Hide file tree
Showing 60 changed files with 335 additions and 467 deletions.
26 changes: 4 additions & 22 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,8 @@ SimpleCov.start do
merge_timeout 120
enable_coverage :branch

add_group 'All Extras', %w[gem/lib/pagy/extras]
add_group 'Core', %w[gem/lib/pagy.rb
gem/lib/pagy/b64.rb
gem/lib/pagy/backend.rb
gem/lib/pagy/console.rb
gem/lib/pagy/countless.rb
gem/lib/pagy/init_vars.rb
gem/lib/pagy/exceptions.rb
gem/lib/pagy/frontend.rb
gem/lib/pagy/i18n.rb
gem/lib/pagy/url_helpers.rb]
add_group 'Countless', %w[gem/lib/pagy/countless.rb
gem/lib/pagy/extras/countless.rb]
add_group 'Calendar', %w[gem/lib/pagy/calendar
gem/lib/pagy/extras/calendar.rb]
add_group 'Keyset', %w[gem/lib/pagy/keyset.rb
gem/lib/pagy/keyset_for_ui.rb
gem/lib/pagy/keyset/active_record.rb
gem/lib/pagy/keyset/sequel.rb
gem/lib/pagy/extras/keyset.rb
gem/lib/pagy/extras/keyset_for_ui.rb]
add_group 'Tests', %w[test]
add_group 'Core', %w[gem/lib/pagy.rb gem/lib/pagy]
add_group 'Extras', 'gem/lib/pagy/extras'
add_group 'Mixins', 'gem/lib/pagy/mixins'
add_group 'Tests', 'test'
end
2 changes: 0 additions & 2 deletions gem/apps/calendar.ru
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
end

# Pagy initializer
require 'pagy/extras/calendar'
require 'pagy/extras/bootstrap'
Pagy::DEFAULT.freeze

# Sinatra setup
Expand Down
10 changes: 5 additions & 5 deletions gem/apps/demo.ru
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ 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
# STYLES.each_key do |style|
# require "pagy/extras/#{STYLES[style][:extra] || style}"
# end
require 'pagy/extras/limit'

# Sinatra setup
Expand All @@ -57,7 +57,7 @@ class PagyDemo < Sinatra::Base

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

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

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

erb :helpers, locals: { style:, prefix: }
end
Expand Down
4 changes: 1 addition & 3 deletions gem/apps/keyset.ru
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ end

# Pagy initializer
require 'pagy/extras/limit'
require 'pagy/extras/keyset'
require 'pagy/extras/pagy'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
# Sinatra application
class PagyKeysetAugmented < Sinatra::Base
class PagyKeyset < Sinatra::Base
include Pagy::Backend
# Root route/action
get '/' do
Expand Down
6 changes: 2 additions & 4 deletions gem/apps/keyset_sequel.ru
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ end

# Pagy initializer
require 'pagy/extras/limit'
require 'pagy/extras/keyset'
require 'pagy/extras/pagy'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT.freeze

# Sinatra setup
require 'sinatra/base'
require 'logger'
# Sinatra application
class PagyKeysetAugmented < Sinatra::Base
class PagyKeysetSequel < Sinatra::Base
include Pagy::Backend
# Root route/action
get '/' do
Expand Down Expand Up @@ -216,4 +214,4 @@ data.each_line(chomp: true) do |pet|
dataset.insert(name:, animal:, birthdate:)
end

run PagyKeysetAugmented
run PagyKeysetSequel
10 changes: 3 additions & 7 deletions gem/apps/rails.ru
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,9 @@ unless File.writable?(dir)
end

# Pagy initializer
require 'pagy/extras/pagy'
require 'pagy/extras/limit'
require 'pagy/extras/overflow'
require 'pagy/extras/headers'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT[:overflow] = :empty_page
Pagy::DEFAULT.freeze
Pagy::DEFAULT[:limit] = 10
Pagy::Offset::DEFAULT[:overflow] = :empty_page

# Activerecord initializer
ActiveRecord::Base.logger = Logger.new(OUTPUT)
Expand Down Expand Up @@ -113,7 +109,7 @@ class CommentsController < ActionController::Base # :nodoc:
include Pagy::Backend

def index
@pagy, @comments = pagy(Comment.all)
@pagy, @comments = pagy_offset(Comment.all)
pagy_headers_merge(@pagy)
render inline: TEMPLATE
end
Expand Down
6 changes: 2 additions & 4 deletions gem/apps/repro.ru
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ end

# Edit this section adding/removing the extras and Pagy::DEFAULT as needed
# pagy initializer
require 'pagy/extras/pagy'
require 'pagy/extras/limit'
require 'pagy/extras/overflow'
Pagy::DEFAULT[:overflow] = :empty_page
Pagy::Offset::DEFAULT[:overflow] = :empty_page
Pagy::DEFAULT.freeze

# Sinatra setup
Expand All @@ -56,7 +54,7 @@ class PagyRepro < Sinatra::Base
# Edit this action as needed
get '/' do
collection = MockCollection.new
@pagy, @records = pagy(collection)
@pagy, @records = pagy_offset(collection)
erb :main
end

Expand Down
8 changes: 6 additions & 2 deletions gem/lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require 'pathname'
require_relative 'pagy/autoload'
require_relative 'pagy/loader'

# Top superclass: it should define only what's common to all the subclasses
class Pagy
Expand All @@ -15,6 +15,7 @@ class Pagy
autoload :ElasticsearchRails, 'pagy/mixins/elasticsearch_rails'
autoload :Meilisearch, 'pagy/mixins/meilisearch'
autoload :Searchkick, 'pagy/mixins/searchkick'
autoload :Console, 'pagy/console'

VERSION = '9.3.3'
PAGE_TOKEN = 'P '
Expand All @@ -28,7 +29,10 @@ def self.root
@root ||= Pathname.new(__dir__).parent.freeze
end

attr_reader :page, :limit, :vars
attr_reader :page, :prev, :next, :in, :limit, :vars, :last

alias pages last
def self.predict_last? = true

# Validates and assign the passed vars: var must be present and value.to_i must be >= to min
def assign_and_check(name_min)
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Pagy
# or any collection by overriding any of the `pagy_*` methods in your controller.
# See also the extras if you need specialized methods to paginate Arrays or other collections
module Backend
include Autoload
include Loader

private

Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# frozen_string_literal: true

require_relative '../pagy' # so you can require just the extra in the console
require_relative 'extras/standalone'

class Pagy
# Provide a ready to use pagy environment when included in irb/rails console
Expand All @@ -11,6 +10,7 @@ module Console
def self.included(main)
main.include(Backend)
main.include(Frontend)
main.define_method(:params) { {} }
DEFAULT[:url] = 'http://www.example.com/subdir'
end

Expand Down
9 changes: 0 additions & 9 deletions gem/lib/pagy/extras/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ def pagy_t(key, **)
end
end
Frontend.prepend I18nExtra::FrontendOverride

# Calendar overriding for localization (see also the block in the calendar section of the config/pagy.rb initializer)
module CalendarOverride
def localize(time, opts)
::I18n.l(time, **opts)
end
end
end
Offset::Calendar::Unit.prepend I18nExtra::CalendarOverride if defined?(::Pagy::Offset::Calendar::Unit)

# Add the pagy locales to the I18n.load_path
::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
end
32 changes: 2 additions & 30 deletions gem/lib/pagy/extras/limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Pagy
DEFAULT[:limit_max] = 100
DEFAULT[:limit_extra] = true # extra enabled by default

# Allow the client to request a custom limit per page with an optional selector UI
# Allow the client to request a custom limit per page
# with an optional selector UI
module LimitExtra
# Additions for the Backend module
module BackendAddOn
Expand All @@ -27,34 +28,5 @@ def pagy_get_limit_param(vars)
end
end
Backend.prepend LimitExtra::BackendAddOn

# Additions for the Frontend module
module FrontendAddOn
# Return the limit selector HTML. For example "Show [20] items per page"
def pagy_limit_selector_js(pagy, id: nil, item_name: nil)
return '' unless pagy.vars[:limit_extra]

id = %( id="#{id}") if id
vars = pagy.vars
limit = vars[:limit]
vars[:limit] = LIMIT_TOKEN # limit token replaced in the javascript
url_token = pagy_page_url(pagy, PAGE_TOKEN)
vars[:limit] = limit # restore the limit

limit_input = %(<input name="limit" type="number" min="1" max="#{vars[:limit_max]}" value="#{
limit}" style="padding: 0; text-align: center; width: #{limit.to_s.length + 1}rem;">#{
A_TAG})

%(<span#{id} class="pagy limit-selector-js" #{
pagy_data(pagy, :sj, pagy.from, url_token)
}><label>#{
pagy_t('pagy.limit_selector_js',
item_name: item_name || pagy_t('pagy.item_name', count: limit),
limit_input:,
count: limit)
}</label></span>)
end
end
Frontend.prepend LimitExtra::FrontendAddOn
end
end
15 changes: 0 additions & 15 deletions gem/lib/pagy/extras/standalone.rb

This file was deleted.

26 changes: 1 addition & 25 deletions gem/lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Pagy
# Frontend modules are specially optimized for performance.
# The resulting code may not look very elegant, but produces the best benchmarks
module Frontend
include Autoload
include Loader
include UrlHelpers

# Return a performance optimized lambda to generate the HTML anchor element (a tag)
Expand Down Expand Up @@ -43,30 +43,6 @@ def pagy_info(pagy, id: nil, item_name: nil)
}</span>)
end

# Generic pagination: it returns the html with the series of links to the pages
def pagy_nav(pagy, id: nil, aria_label: nil, **vars)
id = %( id="#{id}") if id
a = pagy_anchor(pagy, **vars)
data = %( #{pagy_data(pagy, :n)}) if defined?(::Pagy::Keyset::Augmented) && pagy.is_a?(Keyset::Augmented)

html = %(<nav#{id} class="pagy nav" #{nav_aria_label(pagy, aria_label:)}#{data}>#{
prev_a(pagy, a)})
pagy.series(**vars).each do |item|
# series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
html << case item
when Integer
a.(item)
when String
%(<a role="link" aria-disabled="true" aria-current="page" class="current">#{pagy.label_for(item)}</a>)
when :gap
%(<a role="link" aria-disabled="true" class="gap">#{pagy_t('pagy.gap')}</a>)
else
raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
end
end
html << %(#{next_a(pagy, a)}</nav>)
end

# Similar to I18n.t: just ~18x faster using ~10x less memory
# (@pagy_locale explicitly initialized in order to avoid warning)
def pagy_t(key, **)
Expand Down
2 changes: 2 additions & 0 deletions gem/lib/pagy/keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def initialize(set, **vars) # rubocop:disable Lint/MissingSuper
assign_filter_args
end

def self.predict_last? = false

# Assign the filter_args
def assign_filter_args
return unless @prev_cutoff
Expand Down
9 changes: 3 additions & 6 deletions gem/lib/pagy/links_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ module LinksHelper
def pagy_links(pagy, **)
url_str = pagy_page_url(pagy, PAGE_TOKEN, **)
{ first: url_str.sub(PAGE_TOKEN, '1') }.tap do |links|
links[:next] = url_str.sub(PAGE_TOKEN, pagy.next.to_s) if pagy.next
next if defined?(::Pagy::Keyset) && pagy.is_a?(Keyset)

links[:prev] = url_str.sub(PAGE_TOKEN, pagy.prev.to_s) if pagy.prev
links[:last] = url_str.sub(PAGE_TOKEN, pagy.last.to_s) if pagy.last \
&& !(defined?(::Pagy::Offset::Countless) && pagy.is_a?(Offset::Countless))
end.slice(:first, :prev, :next, :last)
links[:next] = url_str.sub(PAGE_TOKEN, pagy.next.to_s) if pagy.next
links[:last] = url_str.sub(PAGE_TOKEN, pagy.last.to_s) if pagy.class.predict_last?
end
end
end
end
16 changes: 9 additions & 7 deletions gem/lib/pagy/autoload.rb → gem/lib/pagy/loader.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Pagy
module Autoload
module Loader
METHOD_MIXINS = { pagy_arel: :arel,
pagy_array: :array,
pagy_bootstrap_nav: :bootstrap,
Expand All @@ -18,18 +18,20 @@ module Autoload
pagy_headers_merge: :headers,
pagy_keyset: :keyset,
pagy_keyset_augmented_js: :keyset_augmented,
pagy_limit_selector_js: :limit_selector,
new_from_meilisearch: :meilisearch,
pagy_meilisearch: :meilisearch,
pagy_metadata: :metadata,
pagy_offset: :offset,
pagy_nav: :pagy,
pagy_nav_js: :pagy,
pagy_combo_nav_js: :pagy,
pagy_prev_url: :pagy,
pagy_next_url: :pagy,
pagy_prev_a: :pagy,
pagy_next_a: :pagy,
pagy_prev_link: :pagy,
pagy_next_link: :pagy,
pagy_prev_url: :pagy_helpers,
pagy_next_url: :pagy_helpers,
pagy_prev_a: :pagy_helpers,
pagy_next_a: :pagy_helpers,
pagy_prev_link: :pagy_helpers,
pagy_next_link: :pagy_helpers,
new_from_searchkick: :searchkick,
pagy_searchkick: :searchkick }.freeze

Expand Down
Loading

0 comments on commit f28f576

Please sign in to comment.