diff --git a/gem/lib/pagy/keyset.rb b/gem/lib/pagy/keyset.rb index ad161295e..d65986f7d 100644 --- a/gem/lib/pagy/keyset.rb +++ b/gem/lib/pagy/keyset.rb @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/gem/lib/pagy/keyset_for_ui.rb b/gem/lib/pagy/keyset_for_ui.rb index 8b39b5d45..d7a2d8695 100644 --- a/gem/lib/pagy/keyset_for_ui.rb +++ b/gem/lib/pagy/keyset_for_ui.rb @@ -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 @@ -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