# frozen_string_literal: true
# Starting point to reproduce keyset related pagy issues
# DEV USAGE
# pagy clone rails
# pagy ./keyset.ru
# URL
# http://0.0.0.0:8000
# HELP
# pagy -h
# DOC
# https://ddnexus.github.io/pagy/playground/#5-keyset-app
VERSION = '8.6.2'
# Gemfile
require 'bundler/inline'
require 'bundler'
Bundler.configure
gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
source 'https://rubygems.org'
gem 'oj'
gem 'puma'
gem 'rails'
# activerecord/sqlite3_adapter.rb probably useless) constraint !!!
# https://github.com/rails/rails/blame/v7.1.3.4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L14
gem 'sqlite3', '~> 1.4.0'
gem 'sequel'
end
# require 'rails/all' # too much stuff
require 'action_controller/railtie'
require 'sequel'
OUTPUT = Rails.env.showcase? ? IO::NULL : $stdout
# Rails config
class PagyKeyset < Rails::Application # :nodoc:
config.root = __dir__
config.session_store :cookie_store, key: 'cookie_store_key'
Rails.application.credentials.secret_key_base = 'absolute_secret'
config.logger = Logger.new(OUTPUT)
Rails.logger = config.logger
routes.draw do
root to: 'pets#index'
end
end
dir = Rails.env.development? ? '.' : Dir.pwd # app dir in dev or pwd otherwise
unless File.writable?(dir)
warn "ERROR: directory #{dir.inspect} is not writable (the pagy-rails-app needs to create DB files)"
exit 1
end
# Pagy initializer
require 'pagy/extras/pagy'
require 'pagy/extras/limit'
require 'pagy/extras/keyset'
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT.freeze
PETS = <<~PETS
Luna | dog | 2018-03-10
Coco | cat | 2019-05-15
Dodo | dog | 2020-06-25
Wiki | bird | 2018-03-12
Baby | rabbit | 2020-01-13
Neki | horse | 2021-07-20
Tino | donkey | 2019-06-18
Plot | cat | 2022-09-21
Riki | cat | 2018-09-14
Susi | horse | 2018-10-26
Coco | pig | 2020-08-29
Momo | bird | 2023-08-25
Lili | cat | 2021-07-22
Beli | pig | 2020-07-26
Rocky | bird | 2022-08-19
Vyvy | dog | 2018-05-16
Susi | horse | 2024-01-25
Ella | cat | 2020-02-20
Rocky | dog | 2019-09-19
Juni | rabbit | 2020-08-24
Coco | bird | 2021-03-17
Susi | dog | 2021-07-28
Luna | horse | 2023-05-14
Gigi | pig | 2022-05-19
Coco | cat | 2020-02-20
Nino | donkey | 2019-06-17
Luna | cat | 2022-02-09
Popi | dog | 2020-09-26
Lili | pig | 2022-06-18
Mina | horse | 2021-04-21
Susi | rabbit | 2023-05-18
Toni | donkey | 2018-06-22
Rocky | horse | 2019-09-28
Lili | cat | 2019-03-18
Roby | cat | 2022-06-19
Anto | horse | 2022-08-18
Susi | pig | 2021-04-21
Boly | bird | 2020-03-29
Sky | cat | 2023-07-19
Lili | dog | 2020-01-28
Fami | snake | 2023-04-27
Lopi | pig | 2019-06-19
Rocky | snake | 2022-03-13
Denis | dog | 2022-06-19
Maca | cat | 2022-06-19
Luna | dog | 2022-08-15
Jeme | horse | 2019-08-08
Sary | bird | 2023-04-29
Rocky | bird | 2023-05-14
Coco | dog | 2023-05-27
PETS
Sequel.default_timezone = :utc
## Sequel initializer
DB = Sequel.connect(adapter: 'sqlite', user: 'root', password: 'password', host: 'localhost', port: '3306',
database: "#{dir}/tmp/pagy-keyset-s.sqlite3", max_connections: 10, loggers: [Logger.new(OUTPUT)])
DB.create_table! :pets do
primary_key :id
String :animal, unique: false, null: false
String :name, unique: false, null: false
Date :birthdate, unique: false, null: false
end
dataset = DB[:pets]
PETS.each_line(chomp: true) do |pet|
name, animal, birthdate = pet.split('|').map(&:strip)
dataset.insert(name:, animal:, birthdate:)
end
# Models
class Pet < Sequel::Model
end
# Helpers
module PetsHelper
include Pagy::Frontend
def order_symbol(dir)
{ asc: '↗', desc: '↘' }[dir]
end
end
# Controllers
class PetsController < ActionController::Base # :nodoc:
include Rails.application.routes.url_helpers
include Pagy::Backend
def index
Time.zone = 'UTC'
@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
@pagy, @pets = pagy_keyset(Pet.order(:animal, :name, Sequel.desc(:birthdate), :id))
render inline: TEMPLATE
end
end
TEMPLATE = <<~ERB
Pagy Keyset App
Pagy Keyset App
Self-contained, standalone Rails app usable to easily reproduce any keyset related pagy issue with Sequel sets.
Please, report the following versions in any new issue.
Versions
- Ruby: <%== RUBY_VERSION %>
- Rack: <%== Rack::RELEASE %>
- Rails: <%== Rails.version %>
- Pagy: <%== Pagy::VERSION %>
Collection
animal <%== order_symbol(@order[:animal]) %> |
name <%== order_symbol(@order[:name]) %> |
birthdate <%== order_symbol(@order[:birthdate]) %> |
id <%== order_symbol(@order[:id]) %> |
<% @pets.each do |pet| %>
<%= pet.animal %> |
<%= pet.name %> |
<%= pet.birthdate %> |
<%= pet.id %> |
<% end %>
ERB
run PagyKeyset