Skip to content

Commit

Permalink
Use actual object instead of B64 encoded string for cutoffs
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 5, 2025
1 parent 7a93859 commit 6b2c4e6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
11 changes: 5 additions & 6 deletions gem/lib/pagy/keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def assign_filter_args

# Assign the page
def assign_page
@page = @prev_cutoff = @vars[:page]
@page = @vars[:page]
@prev_cutoff = JSON.parse(B64.urlsafe_decode(@page)) if @page
end

# Prepare the literal SQL string (complete with the placeholders for value interpolation)
Expand Down Expand Up @@ -109,8 +110,7 @@ def default
# Derive the cutoff from the last record
def derive_cutoff
attr = keyset_attributes_from(@records.last)
json = (@vars[:stringify_keyset_values]&.(attr) || attr).values.to_json
B64.urlsafe_encode(json)
(@vars[:stringify_keyset_values]&.(attr) || attr).values
end

# Fetch the records and set the @more flag
Expand All @@ -122,8 +122,7 @@ def fetch_records

# Return the filter arguments for a cutoff
def filter_args_for(cutoff)
values = JSON.parse(B64.urlsafe_decode(cutoff))
args = @keyset.keys.zip(values).to_h
args = @keyset.keys.zip(cutoff).to_h
typecast_args(args)
end

Expand All @@ -132,7 +131,7 @@ def next
records
return unless @more

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

# Fetch the array of records for the current page
Expand Down
12 changes: 0 additions & 12 deletions gem/lib/pagy/keyset_for_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ def default
{ **super, **DEFAULT.slice(:ends, :size) }
end

# Derive the cutoff from the last record
def derive_cutoff
attr = keyset_attributes_from(@records.last)
(@vars[:stringify_keyset_values]&.(attr) || attr).values
end

# Remove the LIMIT if @cutoff
def fetch_records
return super unless @cutoff # super for the last known page
Expand All @@ -117,12 +111,6 @@ def fetch_records
@set.limit(nil).to_a
end

# Return the filter arguments for a cutoff
def filter_args_for(cutoff)
args = @keyset.keys.zip(cutoff).to_h
typecast_args(args)
end

# Prepare the @update for the client when it's a new page/cutoff and return the next page number
def next
records
Expand Down

0 comments on commit 6b2c4e6

Please sign in to comment.