Class: Pagy::Keyset

Inherits:
Pagy
  • Object
show all
Defined in:
lib/pagy/classes/keyset/keyset.rb,
lib/pagy/classes/keyset/keynav.rb,
lib/pagy/classes/keyset/adapters/sequel.rb,
lib/pagy/classes/keyset/adapters/active_record.rb

Overview

Implement wicked-fast keyset pagination for big data

Direct Known Subclasses

ActiveRecord, Keynav, Sequel

Defined Under Namespace

Modules: Adapters Classes: ActiveRecord, Keynav, Sequel, TypeError

Constant Summary

Constants inherited from Pagy

A_TAG, DEFAULT, DEFAULT_DATA_KEYS, DEFAULT_HEADERS_MAP, LABEL_TOKEN, LIMIT_TOKEN, Method, OPTIONS, PAGE_TOKEN, ROOT, SERIES_SLOTS, VERSION

Instance Attribute Summary

Attributes inherited from Pagy

#in, #limit, #options, #page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pagy

#data_hash, #headers_hash, #info_tag, #input_nav_js, #limit_tag_js, #next_tag, options, #page_url, #previous_tag, #series_nav, #series_nav_js, #urls_hash

Methods included from Configurable

#dev_tools, #sync_javascript, #translate_with_the_slower_i18n_gem!

Constructor Details

#initialize(set) ⇒ Keyset

Returns a new instance of Keyset.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pagy/classes/keyset/keyset.rb', line 51

def initialize(set, **)
  assign_options(**)
  assign_and_check(limit: 1)
  @set    = set
  @keyset = @options[:keyset] || extract_keyset
  raise InternalError, 'the set must be ordered' if @keyset.empty?

  @identifiers = quoted_identifiers(@set.model.table_name)

  assign_page
  self.next
end

Class Method Details

.mix_in_adapter(adapter) ⇒ Object

Helper to lazy-include the adapter module



46
47
48
49
# File 'lib/pagy/classes/keyset/keyset.rb', line 46

def self.mix_in_adapter(adapter)
  adapter_module = Adapters.const_get(adapter)
  include(adapter_module) unless self < adapter_module
end

.new(set) ⇒ Object

Factory method: detects the set type, configures the subclass, and instantiates



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pagy/classes/keyset/keyset.rb', line 25

def self.new(set, **)
  # 1. Handle direct subclass usage (e.g. Pagy::Keyset::ActiveRecord.new)
  if /::(?:ActiveRecord|Sequel)$/.match?(name)
    # Ensure the adapter is mixed in (lazy load)
    mix_in_adapter(name.split('::').last)
    return allocate.tap { _1.send(:initialize, set, **) }
  end

  # 2. Handle Factory usage (Pagy::Keyset.new)
  adapter = if defined?(::ActiveRecord) && set.is_a?(::ActiveRecord::Relation)
              :ActiveRecord
            elsif defined?(::Sequel) && set.is_a?(::Sequel::Dataset)
              :Sequel
            else
              raise TypeError, "expected an ActiveRecord::Relation or Sequel::Dataset; got #{set.class}"
            end

  const_get(adapter).tap { _1.mix_in_adapter(adapter) }.new(set, **)
end

Instance Method Details

#nextObject

Return the next page (i.e., the cutoff of the current page)



73
74
75
76
77
78
# File 'lib/pagy/classes/keyset/keyset.rb', line 73

def next
  records
  return unless @more

  @next ||= B64.urlsafe_encode(extract_cutoff.to_json)
end

#recordsObject

Return the array of records for the current page



65
66
67
68
69
70
# File 'lib/pagy/classes/keyset/keyset.rb', line 65

def records
  @records ||= begin
                 ensure_select
                 fetch_records
               end
end