From 3837c600352e3fdf256f98a66adef8c99fb716e8 Mon Sep 17 00:00:00 2001
From: Domizio Demichelis
Date: Sun, 17 Nov 2024 18:42:26 +0700
Subject: [PATCH] Remove the :typecast_latest variable
---
docs/api/keyset.md | 36 +++++-------------------------------
gem/lib/pagy/keyset.rb | 2 +-
test/pagy/keyset_test.rb | 8 --------
3 files changed, 6 insertions(+), 40 deletions(-)
diff --git a/docs/api/keyset.md b/docs/api/keyset.md
index c3e9edfa7..a9ec4502b 100644
--- a/docs/api/keyset.md
+++ b/docs/api/keyset.md
@@ -181,21 +181,6 @@ end
Pagy::Keyset(set, filter_newest:)
```
-==- `:typecast_latest`
-
-A lambda to override the automatic typecasting of your ORM. For example: `SQLite` stores date and times as strings, and
-the query interpolation may fail composing and comparing string dates. The `typecast_latest` is an effective last-resort
-option when fixing the typecasting in your models and/or the data in your storage is not possible.
-
-```ruby
-typecast_latest = lambda do |latest|
- latest[:timestamp] = Time.parse(latest[:timestamp]).strftime('%F %T')
- latest
-end
-
-Pagy::Keyset(set, typecast_latest:)
-```
-
==- `:jsonify_keyset_attributes`
A lambda to override the generic json encoding of the `keyset` attributes. Use it when the generic `to_json` method would lose
@@ -237,7 +222,7 @@ _(Notice that it doesn't work with `Sequel::Dataset` sets)_
==- Records may repeat or be missing from successive pages
-!!!danger Your set is not `uniquely ordered`
+!!!danger The set may not be `uniquely ordered`
```rb
# Neither columns are unique
@@ -251,30 +236,19 @@ Product.order(:name, :production_date)
Product.order(:name, :production_date, :id)
```
!!!
-
+
!!!danger You may have an encoding problem
-The generic `to_json` method used to encode the `page` loses some information when decoded.
+The generic `to_json` method used to encode the `page` may lose some information when decoded
!!!success
- Check the actual executed DB query and the actual stored value
- Identify the column that have a format that doesn't match with the keyset
-- Use your custom encoding with the [:jsonify_keyset_attributes](#jsonify-keyset-attributes) variable
+- Override the encoding with the [:jsonify_keyset_attributes](#jsonify-keyset-attributes) variable
!!!
-!!!danger You may have a typecasting problem
-Your ORM and the storage formats don't match for one or more columns. It's a common case with `SQLite` and Time columns.
-They may have been stored as strings formatted differently than the default format used by your current ORM.
-
-!!!success
-- Check the actual executed DB query and the actual stored value
-- Identify the column that have a format that doesn't match with the keyset
-- Fix the typecasting consistence of your ORM with your DB or consider using your custom typecasting with the
- [:typecast_latest](#typecast-latest) variable
-!!!
-
==- The order is OK, but the DB is still slow
-!!!danger Most likely your index is not right, or your case needs a custom query
+!!!danger Most likely the index is not right, or your case needs a custom query
!!! Success
diff --git a/gem/lib/pagy/keyset.rb b/gem/lib/pagy/keyset.rb
index 05ec5a15a..74f440219 100644
--- a/gem/lib/pagy/keyset.rb
+++ b/gem/lib/pagy/keyset.rb
@@ -43,7 +43,7 @@ def initialize(set, **vars)
return unless @page
latest = JSON.parse(B64.urlsafe_decode(@page)).transform_keys(&:to_sym)
- @latest = @vars[:typecast_latest]&.(latest) || typecast_latest(latest)
+ @latest = typecast_latest(latest)
raise InternalError, 'page and keyset are not consistent' \
unless @latest.keys == @keyset.keys
end
diff --git a/test/pagy/keyset_test.rb b/test/pagy/keyset_test.rb
index 787079e45..78eb5925d 100644
--- a/test/pagy/keyset_test.rb
+++ b/test/pagy/keyset_test.rb
@@ -41,14 +41,6 @@
_(records.size).must_equal 10
_(records.first.id).must_equal 13
end
- it 'uses :typecast_latest' do
- pagy = Pagy::Keyset.new(model.order(:id),
- page: "eyJpZCI6MTB9",
- limit: 10,
- typecast_latest: ->(latest) { latest })
- _ = pagy.records
- _(pagy.latest).must_equal({id: 10})
- end
it 'uses :jsonify_keyset_attributes' do
pagy = Pagy::Keyset.new(model.order(:id),
page: "eyJpZCI6MTB9",