Skip to content

Commit

Permalink
WIP: Countless keep last
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 15, 2025
1 parent 5f18af0 commit fe3061d
Show file tree
Hide file tree
Showing 53 changed files with 321 additions and 369 deletions.
1 change: 0 additions & 1 deletion gem/lib/pagy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy API documentation: https://ddnexus.github.io/pagy/docs/api/pagy
# frozen_string_literal: true

require 'pathname'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/backend.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/docs/api/backend
# frozen_string_literal: true

require_relative 'helpers/url'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/console.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Console API documentation: https://ddnexus.github.io/pagy/docs/api/console
# frozen_string_literal: true

require_relative '../pagy' # so you can require just the extra in the console
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/extras/gearbox.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/gearbox
# frozen_string_literal: true

require_relative '../offset'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/extras/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/i18n
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/extras/size.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/size
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Frontend API documentation: https://ddnexus.github.io/pagy/docs/api/frontend
# frozen_string_literal: true

require_relative 'helpers/b64'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/helpers/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::I18n API documentation https://ddnexus.github.io/pagy/docs/api/i18n
# frozen_string_literal: true

require 'yaml'
Expand Down
1 change: 1 addition & 0 deletions gem/lib/pagy/helpers/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def escape(str)
def pagy_page_url(pagy, page, absolute: false, fragment: nil, **)
request_var, pagy_params = (vars = pagy.vars).values_at(:request, :params)
page_name, limit_name = vars.values_at(:page_sym, :limit_sym).map(&:to_s)
page = "#{page}+#{pagy.last}" if /^Pagy::Offset::Countless/.match?(pagy.class.name)
query_params = request_var ? (request_var[:query_params] || {}) : request.GET.clone(freeze: false)
page_and_limit = { page_name => page }.tap { |h| h[limit_name] = vars[:limit] if vars[:limit_requestable] }
query_params.merge!(vars[:jsonapi] ? { 'page' => page_and_limit } : page_and_limit)
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/keyset.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy API documentation: https://ddnexus.github.io/pagy/docs/api/keyset
# frozen_string_literal: true

require 'json'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/keyset/adapters/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy API documentation: https://ddnexus.github.io/pagy/docs/api/keyset
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/keyset/adapters/sequel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy API documentation: https://ddnexus.github.io/pagy/docs/api/keyset
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/keyset/augmented.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/keyset_for_ui
# frozen_string_literal: true

class Pagy
Expand Down
32 changes: 16 additions & 16 deletions gem/lib/pagy/loaders/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ def pagy_load_backend(...)
send(method_sym, ...)
end

BACKEND_METHOD_MIXINS = { pagy_arel: PAGY_PATH.join('mixins/arel').to_s,
pagy_array: PAGY_PATH.join('mixins/array').to_s,
pagy_calendar: PAGY_PATH.join('mixins/calendar').to_s,
pagy_countless: PAGY_PATH.join('mixins/countless').to_s,
pagy_elasticsearch_rails: PAGY_PATH.join('mixins/elasticsearch_rails').to_s,
pagy_headers: PAGY_PATH.join('mixins/headers').to_s,
pagy_headers_merge: PAGY_PATH.join('mixins/headers').to_s,
pagy_keyset: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_first_url: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_next_url: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_augmented_js: PAGY_PATH.join('mixins/keyset_augmented').to_s,
pagy_meilisearch: PAGY_PATH.join('mixins/meilisearch').to_s,
pagy_links: PAGY_PATH.join('mixins/links'),
pagy_metadata: PAGY_PATH.join('mixins/metadata').to_s,
pagy_offset: PAGY_PATH.join('mixins/offset').to_s,
pagy_searchkick: PAGY_PATH.join('mixins/searchkick').to_s }.freeze
BACKEND_METHOD_MIXINS = { pagy_arel: PAGY_PATH.join('mixins/arel').to_s,
pagy_array: PAGY_PATH.join('mixins/array').to_s,
pagy_calendar: PAGY_PATH.join('mixins/calendar').to_s,
pagy_countless: PAGY_PATH.join('mixins/countless').to_s,
pagy_elasticsearch_rails: PAGY_PATH.join('mixins/elasticsearch_rails').to_s,
pagy_headers: PAGY_PATH.join('mixins/headers').to_s,
pagy_headers_merge: PAGY_PATH.join('mixins/headers').to_s,
pagy_keyset: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_first_url: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_next_url: PAGY_PATH.join('mixins/keyset').to_s,
pagy_keyset_augmented_js: PAGY_PATH.join('mixins/keyset_augmented').to_s,
pagy_meilisearch: PAGY_PATH.join('mixins/meilisearch').to_s,
pagy_links: PAGY_PATH.join('mixins/links'),
pagy_metadata: PAGY_PATH.join('mixins/metadata').to_s,
pagy_offset: PAGY_PATH.join('mixins/offset').to_s,
pagy_searchkick: PAGY_PATH.join('mixins/searchkick').to_s }.freeze

BACKEND_METHOD_MIXINS.each_key do |method|
class_eval "alias #{method} pagy_load_backend", __FILE__, __LINE__ # alias pagy_* pagy_load_backend
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/arel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/arel
# frozen_string_literal: true

require_relative 'offset' # require the offset mixin
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/array
# frozen_string_literal: true

require_relative '../offset'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/bootstrap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/bootstrap
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/bulma.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/bulma
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/calendar
# frozen_string_literal: true

class Pagy
Expand Down
12 changes: 10 additions & 2 deletions gem/lib/pagy/mixins/countless.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/countless
# frozen_string_literal: true

require_relative '../offset/countless'
Expand All @@ -10,8 +9,17 @@ class Pagy

# Return Pagy object and records
def pagy_countless(collection, **vars)
if vars[:page].nil?
page = pagy_get_page(vars, force_integer: false) # accept nil and strings
if page.is_a?(String)
p, l = page.split.map(&:to_i)
vars[:page] = p
vars[:last] = l
else
vars[:page] = 1
end
end
vars[:limit] ||= pagy_get_limit(vars)
vars[:page] ||= pagy_get_page(vars)
pagy = Offset::Countless.new(**vars)
[pagy, pagy_countless_get_items(collection, pagy)]
end
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/elasticsearch_rails
# frozen_string_literal: true

require_relative '../offset/elasticsearch_rails'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/headers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/headers
# frozen_string_literal: true

require_relative '../helpers/url'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/keyset.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/keyset
# frozen_string_literal: true

require_relative '../keyset'
Expand Down
3 changes: 1 addition & 2 deletions gem/lib/pagy/mixins/keyset_augmented.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/keyset_frontendble
# frozen_string_literal: true

class Pagy
Expand All @@ -7,7 +6,7 @@ class Pagy
# Return Pagy::Keyset::Augmented object and paginated records
def pagy_keyset_augmented_js(set, **vars)
page = pagy_get_page(vars, force_integer: false) # allow nil
if page.to_i.positive? # numeric page -> no augmentation -> fallback
if page&.match(' ') # countless page -> no augmentation -> fallback
return pagy_countless(set, **vars)
elsif page.is_a?(String) # augmented page param
page_args = JSON.parse(B64.urlsafe_decode(page))
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/meilisearch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/meilisearch
# frozen_string_literal: true

require_relative '../offset/meilisearch'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/metadata
# frozen_string_literal: true

require_relative '../helpers/url'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/pagy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/pagy
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/pagy_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/pagy
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/mixins/searchkick.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/searchkick
# frozen_string_literal: true

require_relative '../offset/searchkick'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset API documentation: https://ddnexus.github.io/pagy/docs/api/pagy/offset
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

require 'active_support'
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar/day.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar/month.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

class Pagy # :nodoc:
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar/quarter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

class Pagy # :nodoc:
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar/week.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

class Pagy
Expand Down
1 change: 0 additions & 1 deletion gem/lib/pagy/offset/calendar/year.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
# frozen_string_literal: true

class Pagy # :nodoc:
Expand Down
7 changes: 3 additions & 4 deletions gem/lib/pagy/offset/countless.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# See Pagy::Offset::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/offset/countless
# frozen_string_literal: true

class Pagy
Expand All @@ -13,7 +12,7 @@ def initialize(**vars) # rubocop:disable Lint/MissingSuper
assign_and_check(page: 1, outset: 0)
assign_limit
assign_offset
@last = @vars[:last]
@last = vars[:last].to_i
end

def self.predict_last? = false
Expand All @@ -22,8 +21,8 @@ def self.predict_last? = false
def finalize(fetched_size)
raise OverflowError.new(self, :page, "to be < #{@page}", @page) if fetched_size.zero? && @page > 1

@last ||= fetched_size > @limit ? @page + 1 : @page
@last = @vars[:max_pages] if @vars[:max_pages] && @last > @vars[:max_pages]
@last = fetched_size > @limit ? @page + 1 : @page unless @last && @page < @last
@last = @vars[:max_pages] if @vars[:max_pages] && @last > @vars[:max_pages]
raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last

@in = [fetched_size, @limit].min
Expand Down
2 changes: 2 additions & 0 deletions test/helpers/nav_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require_relative '../mock_helpers/app'
require_relative '../files/models'

# required because we use the class that does not load the mixin, so wenmiss the extra pagy_data
require 'pagy/mixins/keyset_augmented'

module NavTests
def app
MockApp.new
Expand Down
1 change: 0 additions & 1 deletion test/pagy/backend/jsonapi_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../../test_helper'

require_relative '../../mock_helpers/collection'
require_relative '../../files/models'
require_relative '../../mock_helpers/app'
Expand Down
1 change: 0 additions & 1 deletion test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../test_helper'

require_relative '../mock_helpers/collection'
require_relative '../mock_helpers/app'

Expand Down
3 changes: 1 addition & 2 deletions test/pagy/extras/i18n_extra_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require_relative '../../mock_helpers/app'
require 'i18n'
require 'pagy/extras/i18n'

require_relative '../../mock_helpers/app'

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

Expand Down
1 change: 0 additions & 1 deletion test/pagy/frontend_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../test_helper'

require_relative '../mock_helpers/pagy_buggy'
require_relative '../mock_helpers/app'

Expand Down
1 change: 0 additions & 1 deletion test/pagy/mixins/arel_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../../test_helper'

require_relative '../../mock_helpers/arel'
require_relative '../../mock_helpers/collection'
require_relative '../../mock_helpers/app'
Expand Down
1 change: 0 additions & 1 deletion test/pagy/mixins/array_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../../test_helper'

require_relative '../../mock_helpers/app'

describe 'pagy/mixins/array' do
Expand Down
Loading

0 comments on commit fe3061d

Please sign in to comment.