pagy 9.1.1 → 9.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/apps/calendar.ru +115 -123
- data/apps/demo.ru +273 -259
- data/apps/index.rb +7 -0
- data/apps/keyset_ar.ru +143 -152
- data/apps/keyset_s.ru +135 -154
- data/apps/rails.ru +15 -16
- data/apps/repro.ru +107 -102
- data/bin/pagy +14 -18
- data/config/pagy.rb +1 -1
- data/javascripts/pagy.min.js +2 -2
- data/javascripts/pagy.min.js.map +2 -2
- data/javascripts/pagy.mjs +1 -1
- data/lib/pagy/keyset/active_record.rb +4 -4
- data/lib/pagy/keyset/sequel.rb +4 -4
- data/lib/pagy/keyset.rb +21 -11
- data/lib/pagy.rb +1 -1
- metadata +3 -2
data/apps/keyset_ar.ru
CHANGED
@@ -1,70 +1,169 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
|
3
|
+
# DESCRIPTION
|
4
|
+
# Showcase the keyset ActiveRecord pagination
|
5
|
+
#
|
6
|
+
# DOC
|
7
|
+
# https://ddnexus.github.io/pagy/playground/#5-keyset-apps
|
8
|
+
#
|
9
|
+
# BIN HELP
|
10
|
+
# bundle exec pagy -h
|
11
|
+
#
|
5
12
|
# DEV USAGE
|
6
|
-
# pagy clone
|
7
|
-
# pagy ./
|
8
|
-
|
13
|
+
# bundle exec pagy clone keyset_ar
|
14
|
+
# bundle exec pagy ./keyset_ar.ru
|
15
|
+
#
|
9
16
|
# URL
|
10
17
|
# http://0.0.0.0:8000
|
11
18
|
|
12
|
-
|
13
|
-
# pagy -h
|
14
|
-
|
15
|
-
# DOC
|
16
|
-
# https://ddnexus.github.io/pagy/playground/#5-keyset-app
|
17
|
-
|
18
|
-
VERSION = '8.6.2'
|
19
|
+
VERSION = '9.3.0'
|
19
20
|
|
20
|
-
#
|
21
|
+
# Bundle
|
21
22
|
require 'bundler/inline'
|
22
23
|
require 'bundler'
|
23
24
|
Bundler.configure
|
24
25
|
gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
|
25
26
|
source 'https://rubygems.org'
|
26
|
-
gem '
|
27
|
+
gem 'activerecord'
|
27
28
|
gem 'puma'
|
28
|
-
gem '
|
29
|
-
|
30
|
-
# https://github.com/rails/rails/blame/v7.1.3.4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L14
|
31
|
-
gem 'sqlite3', '~> 1.4.0'
|
29
|
+
gem 'sinatra'
|
30
|
+
gem 'sqlite3'
|
32
31
|
end
|
33
32
|
|
34
|
-
#
|
35
|
-
require '
|
36
|
-
require '
|
33
|
+
# Pagy initializer
|
34
|
+
require 'pagy/extras/limit'
|
35
|
+
require 'pagy/extras/keyset'
|
36
|
+
require 'pagy/extras/pagy'
|
37
|
+
Pagy::DEFAULT[:limit] = 10
|
38
|
+
Pagy::DEFAULT.freeze
|
39
|
+
|
40
|
+
# Sinatra setup
|
41
|
+
require 'sinatra/base'
|
42
|
+
# Sinatra application
|
43
|
+
class PagyKeyset < Sinatra::Base
|
44
|
+
include Pagy::Backend
|
45
|
+
# Root route/action
|
46
|
+
get '/' do
|
47
|
+
Time.zone = 'UTC'
|
37
48
|
|
38
|
-
|
49
|
+
@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
|
50
|
+
@pagy, @pets = pagy_keyset(Pet.order(@order))
|
51
|
+
erb :main
|
52
|
+
end
|
39
53
|
|
40
|
-
|
41
|
-
|
42
|
-
config.root = __dir__
|
43
|
-
config.session_store :cookie_store, key: 'cookie_store_key'
|
44
|
-
Rails.application.credentials.secret_key_base = 'absolute_secret'
|
54
|
+
helpers do
|
55
|
+
include Pagy::Frontend
|
45
56
|
|
46
|
-
|
47
|
-
|
57
|
+
def order_symbol(dir)
|
58
|
+
{ asc: '↗', desc: '↘' }[dir]
|
59
|
+
end
|
60
|
+
end
|
48
61
|
|
49
|
-
|
50
|
-
|
62
|
+
# Views
|
63
|
+
template :layout do
|
64
|
+
<<~ERB
|
65
|
+
<!DOCTYPE html>
|
66
|
+
<html lang="en">
|
67
|
+
<html>
|
68
|
+
<head>
|
69
|
+
<title>Pagy Keyset App</title>
|
70
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
71
|
+
<style type="text/css">
|
72
|
+
@media screen { html, body {
|
73
|
+
font-size: 1rem;
|
74
|
+
line-height: 1.2s;
|
75
|
+
padding: 0;
|
76
|
+
margin: 0;
|
77
|
+
} }
|
78
|
+
body {
|
79
|
+
background: white !important;
|
80
|
+
margin: 0 !important;
|
81
|
+
font-family: sans-serif !important;
|
82
|
+
}
|
83
|
+
.content {
|
84
|
+
padding: 1rem 1.5rem 2rem !important;
|
85
|
+
}
|
86
|
+
|
87
|
+
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
|
88
|
+
</style>
|
89
|
+
</head>
|
90
|
+
<body>
|
91
|
+
<%= yield %>
|
92
|
+
</body>
|
93
|
+
</html>
|
94
|
+
ERB
|
95
|
+
end
|
96
|
+
|
97
|
+
template :main do
|
98
|
+
<<~ERB
|
99
|
+
<div class="content">
|
100
|
+
<h1>Pagy Keyset App</h1>
|
101
|
+
<p>Self-contained, standalone app usable to easily reproduce any keyset related pagy issue with ActiveRecord sets.</p>
|
102
|
+
<p>Please, report the following versions in any new issue.</p>
|
103
|
+
<h2>Versions</h2>
|
104
|
+
<ul>
|
105
|
+
<li>Ruby: <%= RUBY_VERSION %></li>
|
106
|
+
<li>Rack: <%= Rack::RELEASE %></li>
|
107
|
+
<li>Sinatra: <%= Sinatra::VERSION %></li>
|
108
|
+
<li>Pagy: <%= Pagy::VERSION %></li>
|
109
|
+
</ul>
|
110
|
+
|
111
|
+
<h3>Collection</h3>
|
112
|
+
<div id="records" class="collection">
|
113
|
+
<table border="1" cellspacing="0" cellpadding="3">
|
114
|
+
<tr>
|
115
|
+
<th>animal <%= order_symbol(@order[:animal]) %></th>
|
116
|
+
<th>name <%= order_symbol(@order[:name]) %></th>
|
117
|
+
<th>birthdate <%= order_symbol(@order[:birthdate]) %></th>
|
118
|
+
<th>id <%= order_symbol(@order[:id]) %></th>
|
119
|
+
</tr>
|
120
|
+
<% @pets.each do |pet| %>
|
121
|
+
<tr>
|
122
|
+
<td><%= pet.animal %></td>
|
123
|
+
<td><%= pet.name %></td>
|
124
|
+
<td><%= pet.birthdate %></td>
|
125
|
+
<td><%= pet.id %></td>
|
126
|
+
</tr>
|
127
|
+
<% end %>
|
128
|
+
</table>
|
129
|
+
</div>
|
130
|
+
<p>
|
131
|
+
<nav class="pagy" id="next" aria-label="Pagy next">
|
132
|
+
<%= pagy_next_a(@pagy, text: 'Next page >') %>
|
133
|
+
</nav>
|
134
|
+
</div>
|
135
|
+
ERB
|
51
136
|
end
|
52
137
|
end
|
53
138
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
139
|
+
# ActiveRecord setup
|
140
|
+
require 'active_record'
|
141
|
+
|
142
|
+
# Match the microsecods with the strings stored into the time columns of SQLite
|
143
|
+
# ActiveSupport::JSON::Encoding.time_precision = 6
|
144
|
+
|
145
|
+
# Log
|
146
|
+
output = ENV['APP_ENV'].equal?('showcase') ? IO::NULL : $stdout
|
147
|
+
ActiveRecord::Base.logger = Logger.new(output)
|
148
|
+
# SQLite DB files
|
149
|
+
dir = ENV['APP_ENV'].equal?('development') ? '.' : Dir.pwd # app dir in dev or pwd otherwise
|
150
|
+
abort "ERROR: Cannot create DB files: the directory #{dir.inspect} is not writable." \
|
151
|
+
unless File.writable?(dir)
|
152
|
+
# Connection
|
153
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "#{dir}/tmp/pagy-keyset-ar.sqlite3")
|
154
|
+
# Schema
|
155
|
+
ActiveRecord::Schema.define do
|
156
|
+
create_table :pets, force: true do |t|
|
157
|
+
t.string :animal
|
158
|
+
t.string :name
|
159
|
+
t.date :birthdate
|
160
|
+
end
|
58
161
|
end
|
59
162
|
|
60
|
-
#
|
61
|
-
|
62
|
-
require 'pagy/extras/limit'
|
63
|
-
require 'pagy/extras/keyset'
|
64
|
-
Pagy::DEFAULT[:limit] = 10
|
65
|
-
Pagy::DEFAULT.freeze
|
163
|
+
# Models
|
164
|
+
class Pet < ActiveRecord::Base; end
|
66
165
|
|
67
|
-
|
166
|
+
data = <<~DATA
|
68
167
|
Luna | dog | 2018-03-10
|
69
168
|
Coco | cat | 2019-05-15
|
70
169
|
Dodo | dog | 2020-06-25
|
@@ -115,122 +214,14 @@ PETS = <<~PETS
|
|
115
214
|
Sary | bird | 2023-04-29
|
116
215
|
Rocky | bird | 2023-05-14
|
117
216
|
Coco | dog | 2023-05-27
|
118
|
-
|
119
|
-
|
120
|
-
# Activerecord initializer
|
121
|
-
ActiveRecord::Base.logger = Logger.new(OUTPUT)
|
122
|
-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "#{dir}/tmp/pagy-keyset-ar.sqlite3")
|
123
|
-
ActiveRecord::Schema.define do
|
124
|
-
create_table :pets, force: true do |t|
|
125
|
-
t.string :animal
|
126
|
-
t.string :name
|
127
|
-
t.date :birthdate
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# Models
|
132
|
-
class Pet < ActiveRecord::Base
|
133
|
-
end
|
217
|
+
DATA
|
134
218
|
|
135
219
|
# DB seed
|
136
220
|
pets = []
|
137
|
-
|
221
|
+
data.each_line(chomp: true) do |pet|
|
138
222
|
name, animal, birthdate = pet.split('|').map(&:strip)
|
139
223
|
pets << { name:, animal:, birthdate: }
|
140
224
|
end
|
141
225
|
Pet.insert_all(pets)
|
142
226
|
|
143
|
-
# Helpers
|
144
|
-
module PetsHelper
|
145
|
-
include Pagy::Frontend
|
146
|
-
|
147
|
-
def order_symbol(dir)
|
148
|
-
{ asc: '↗', desc: '↘' }[dir]
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# Controllers
|
153
|
-
class PetsController < ActionController::Base # :nodoc:
|
154
|
-
include Rails.application.routes.url_helpers
|
155
|
-
include Pagy::Backend
|
156
|
-
|
157
|
-
def index
|
158
|
-
Time.zone = 'UTC'
|
159
|
-
|
160
|
-
@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
|
161
|
-
@pagy, @pets = pagy_keyset(Pet.order(@order))
|
162
|
-
render inline: TEMPLATE
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
TEMPLATE = <<~ERB
|
167
|
-
<!DOCTYPE html>
|
168
|
-
<html lang="en">
|
169
|
-
<html>
|
170
|
-
<head>
|
171
|
-
<title>Pagy Keyset App</title>
|
172
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
173
|
-
<style type="text/css">
|
174
|
-
@media screen { html, body {
|
175
|
-
font-size: 1rem;
|
176
|
-
line-height: 1.2s;
|
177
|
-
padding: 0;
|
178
|
-
margin: 0;
|
179
|
-
} }
|
180
|
-
body {
|
181
|
-
background: white !important;
|
182
|
-
margin: 0 !important;
|
183
|
-
font-family: sans-serif !important;
|
184
|
-
}
|
185
|
-
.content {
|
186
|
-
padding: 1rem 1.5rem 2rem !important;
|
187
|
-
}
|
188
|
-
|
189
|
-
<%== Pagy.root.join('stylesheets', 'pagy.css').read %>
|
190
|
-
</style>
|
191
|
-
</head>
|
192
|
-
|
193
|
-
<body>
|
194
|
-
|
195
|
-
<div class="content">
|
196
|
-
<h1>Pagy Keyset App</h1>
|
197
|
-
<p>Self-contained, standalone Rails app usable to easily reproduce any keyset related pagy issue with ActiveRecord sets.</p>
|
198
|
-
<p>Please, report the following versions in any new issue.</p>
|
199
|
-
<h2>Versions</h2>
|
200
|
-
<ul>
|
201
|
-
<li>Ruby: <%== RUBY_VERSION %></li>
|
202
|
-
<li>Rack: <%== Rack::RELEASE %></li>
|
203
|
-
<li>Rails: <%== Rails.version %></li>
|
204
|
-
<li>Pagy: <%== Pagy::VERSION %></li>
|
205
|
-
</ul>
|
206
|
-
|
207
|
-
<h3>Collection</h3>
|
208
|
-
<div id="records" class="collection">
|
209
|
-
<table border="1" cellspacing="0" cellpadding="3">
|
210
|
-
<tr>
|
211
|
-
<th>animal <%== order_symbol(@order[:animal]) %></th>
|
212
|
-
<th>name <%== order_symbol(@order[:name]) %></th>
|
213
|
-
<th>birthdate <%== order_symbol(@order[:birthdate]) %></th>
|
214
|
-
<th>id <%== order_symbol(@order[:id]) %></th>
|
215
|
-
</tr>
|
216
|
-
<% @pets.each do |pet| %>
|
217
|
-
<tr>
|
218
|
-
<td><%= pet.animal %></td>
|
219
|
-
<td><%= pet.name %></td>
|
220
|
-
<td><%= pet.birthdate %></td>
|
221
|
-
<td><%= pet.id %></td>
|
222
|
-
</tr>
|
223
|
-
<% end %>
|
224
|
-
</table>
|
225
|
-
</div>
|
226
|
-
<p>
|
227
|
-
<nav class="pagy" id="next" aria-label="Pagy next">
|
228
|
-
<%== pagy_next_a(@pagy, text: 'Next page >') %>
|
229
|
-
</nav>
|
230
|
-
</div>
|
231
|
-
|
232
|
-
</body>
|
233
|
-
</html>
|
234
|
-
ERB
|
235
|
-
|
236
227
|
run PagyKeyset
|
data/apps/keyset_s.ru
CHANGED
@@ -1,71 +1,163 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
|
3
|
+
# DESCRIPTION
|
4
|
+
# Showcase the keyset ActiveRecord pagination
|
5
|
+
#
|
6
|
+
# DOC
|
7
|
+
# https://ddnexus.github.io/pagy/playground/#5-keyset-apps
|
8
|
+
#
|
9
|
+
# BIN HELP
|
10
|
+
# bundle exec pagy -h
|
11
|
+
#
|
5
12
|
# DEV USAGE
|
6
|
-
# pagy clone
|
7
|
-
# pagy ./
|
8
|
-
|
13
|
+
# bundle exec pagy clone keyset_ar
|
14
|
+
# bundle exec pagy ./keyset_ar.ru
|
15
|
+
#
|
9
16
|
# URL
|
10
17
|
# http://0.0.0.0:8000
|
11
18
|
|
12
|
-
|
13
|
-
# pagy -h
|
14
|
-
|
15
|
-
# DOC
|
16
|
-
# https://ddnexus.github.io/pagy/playground/#5-keyset-app
|
17
|
-
|
18
|
-
VERSION = '8.6.2'
|
19
|
+
VERSION = '9.3.0'
|
19
20
|
|
20
|
-
#
|
21
|
+
# Bundle
|
21
22
|
require 'bundler/inline'
|
22
23
|
require 'bundler'
|
23
24
|
Bundler.configure
|
24
25
|
gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
|
25
26
|
source 'https://rubygems.org'
|
26
|
-
gem 'oj'
|
27
27
|
gem 'puma'
|
28
|
-
gem 'rails'
|
29
|
-
# activerecord/sqlite3_adapter.rb probably useless) constraint !!!
|
30
|
-
# https://github.com/rails/rails/blame/v7.1.3.4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L14
|
31
|
-
gem 'sqlite3', '~> 1.4.0'
|
32
28
|
gem 'sequel'
|
29
|
+
gem 'sinatra'
|
30
|
+
gem 'sqlite3'
|
33
31
|
end
|
34
32
|
|
35
|
-
#
|
36
|
-
require '
|
37
|
-
require '
|
33
|
+
# Pagy initializer
|
34
|
+
require 'pagy/extras/limit'
|
35
|
+
require 'pagy/extras/keyset'
|
36
|
+
require 'pagy/extras/pagy'
|
37
|
+
Pagy::DEFAULT[:limit] = 10
|
38
|
+
Pagy::DEFAULT.freeze
|
38
39
|
|
39
|
-
|
40
|
+
# Sinatra setup
|
41
|
+
require 'sinatra/base'
|
42
|
+
require 'logger'
|
43
|
+
# Sinatra application
|
44
|
+
class PagyKeyset < Sinatra::Base
|
45
|
+
include Pagy::Backend
|
46
|
+
# Root route/action
|
47
|
+
get '/' do
|
48
|
+
@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
|
49
|
+
@pagy, @pets = pagy_keyset(Pet.order(:animal, :name, Sequel.desc(:birthdate), :id))
|
50
|
+
erb :main
|
51
|
+
end
|
40
52
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
53
|
+
helpers do
|
54
|
+
include Pagy::Frontend
|
55
|
+
|
56
|
+
def order_symbol(dir)
|
57
|
+
{ asc: '↗', desc: '↘' }[dir]
|
58
|
+
end
|
59
|
+
end
|
46
60
|
|
47
|
-
|
48
|
-
|
61
|
+
# Views
|
62
|
+
template :layout do
|
63
|
+
<<~ERB
|
64
|
+
<!DOCTYPE html>
|
65
|
+
<html lang="en">
|
66
|
+
<html>
|
67
|
+
<head>
|
68
|
+
<title>Pagy Keyset App</title>
|
69
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
70
|
+
<style type="text/css">
|
71
|
+
@media screen { html, body {
|
72
|
+
font-size: 1rem;
|
73
|
+
line-height: 1.2s;
|
74
|
+
padding: 0;
|
75
|
+
margin: 0;
|
76
|
+
} }
|
77
|
+
body {
|
78
|
+
background: white !important;
|
79
|
+
margin: 0 !important;
|
80
|
+
font-family: sans-serif !important;
|
81
|
+
}
|
82
|
+
.content {
|
83
|
+
padding: 1rem 1.5rem 2rem !important;
|
84
|
+
}
|
85
|
+
|
86
|
+
<%= Pagy.root.join('stylesheets', 'pagy.css').read %>
|
87
|
+
</style>
|
88
|
+
</head>
|
89
|
+
<body>
|
90
|
+
<%= yield %>
|
91
|
+
</body>
|
92
|
+
</html>
|
93
|
+
ERB
|
94
|
+
end
|
95
|
+
|
96
|
+
template :main do
|
97
|
+
<<~ERB
|
98
|
+
<div class="content">
|
99
|
+
<h1>Pagy Keyset App</h1>
|
100
|
+
<p>Self-contained, standalone app usable to easily reproduce any keyset related pagy issue with ActiveRecord sets.</p>
|
101
|
+
<p>Please, report the following versions in any new issue.</p>
|
102
|
+
<h2>Versions</h2>
|
103
|
+
<ul>
|
104
|
+
<li>Ruby: <%= RUBY_VERSION %></li>
|
105
|
+
<li>Rack: <%= Rack::RELEASE %></li>
|
106
|
+
<li>Sinatra: <%= Sinatra::VERSION %></li>
|
107
|
+
<li>Pagy: <%= Pagy::VERSION %></li>
|
108
|
+
</ul>
|
49
109
|
|
50
|
-
|
51
|
-
|
110
|
+
<h3>Collection</h3>
|
111
|
+
<div id="records" class="collection">
|
112
|
+
<table border="1" cellspacing="0" cellpadding="3">
|
113
|
+
<tr>
|
114
|
+
<th>animal <%= order_symbol(@order[:animal]) %></th>
|
115
|
+
<th>name <%= order_symbol(@order[:name]) %></th>
|
116
|
+
<th>birthdate <%= order_symbol(@order[:birthdate]) %></th>
|
117
|
+
<th>id <%= order_symbol(@order[:id]) %></th>
|
118
|
+
</tr>
|
119
|
+
<% @pets.each do |pet| %>
|
120
|
+
<tr>
|
121
|
+
<td><%= pet.animal %></td>
|
122
|
+
<td><%= pet.name %></td>
|
123
|
+
<td><%= pet.birthdate %></td>
|
124
|
+
<td><%= pet.id %></td>
|
125
|
+
</tr>
|
126
|
+
<% end %>
|
127
|
+
</table>
|
128
|
+
</div>
|
129
|
+
<p>
|
130
|
+
<nav class="pagy" id="next" aria-label="Pagy next">
|
131
|
+
<%= pagy_next_a(@pagy, text: 'Next page >') %>
|
132
|
+
</nav>
|
133
|
+
</div>
|
134
|
+
ERB
|
52
135
|
end
|
53
136
|
end
|
54
137
|
|
55
|
-
|
138
|
+
# Sequel setup
|
139
|
+
require 'sequel'
|
140
|
+
Sequel.default_timezone = :utc
|
141
|
+
# SQLite DB files
|
142
|
+
dir = ENV['APP_ENV'].equal?('development') ? '.' : Dir.pwd # app dir in dev or pwd otherwise
|
143
|
+
abort "ERROR: Cannot create DB files: the directory #{dir.inspect} is not writable." \
|
56
144
|
unless File.writable?(dir)
|
57
|
-
|
58
|
-
|
145
|
+
# Connection
|
146
|
+
output = ENV['APP_ENV'].equal?('showcase') ? IO::NULL : $stdout
|
147
|
+
DB = Sequel.connect(adapter: 'sqlite', user: 'root', password: 'password', host: 'localhost', port: '3306',
|
148
|
+
database: "#{dir}/tmp/pagy-keyset-s.sqlite3", max_connections: 10, loggers: [Logger.new(output)])
|
149
|
+
# Schema
|
150
|
+
DB.create_table! :pets do
|
151
|
+
primary_key :id
|
152
|
+
String :animal, unique: false, null: false
|
153
|
+
String :name, unique: false, null: false
|
154
|
+
Date :birthdate, unique: false, null: false
|
59
155
|
end
|
60
156
|
|
61
|
-
#
|
62
|
-
|
63
|
-
require 'pagy/extras/limit'
|
64
|
-
require 'pagy/extras/keyset'
|
65
|
-
Pagy::DEFAULT[:limit] = 10
|
66
|
-
Pagy::DEFAULT.freeze
|
157
|
+
# Models
|
158
|
+
class Pet < Sequel::Model; end
|
67
159
|
|
68
|
-
|
160
|
+
data = <<~DATA
|
69
161
|
Luna | dog | 2018-03-10
|
70
162
|
Coco | cat | 2019-05-15
|
71
163
|
Dodo | dog | 2020-06-25
|
@@ -116,123 +208,12 @@ PETS = <<~PETS
|
|
116
208
|
Sary | bird | 2023-04-29
|
117
209
|
Rocky | bird | 2023-05-14
|
118
210
|
Coco | dog | 2023-05-27
|
119
|
-
|
120
|
-
|
121
|
-
Sequel.default_timezone = :utc
|
122
|
-
|
123
|
-
## Sequel initializer
|
124
|
-
DB = Sequel.connect(adapter: 'sqlite', user: 'root', password: 'password', host: 'localhost', port: '3306',
|
125
|
-
database: "#{dir}/tmp/pagy-keyset-s.sqlite3", max_connections: 10, loggers: [Logger.new(OUTPUT)])
|
126
|
-
|
127
|
-
DB.create_table! :pets do
|
128
|
-
primary_key :id
|
129
|
-
String :animal, unique: false, null: false
|
130
|
-
String :name, unique: false, null: false
|
131
|
-
Date :birthdate, unique: false, null: false
|
132
|
-
end
|
211
|
+
DATA
|
133
212
|
|
134
213
|
dataset = DB[:pets]
|
135
|
-
|
136
|
-
PETS.each_line(chomp: true) do |pet|
|
214
|
+
data.each_line(chomp: true) do |pet|
|
137
215
|
name, animal, birthdate = pet.split('|').map(&:strip)
|
138
216
|
dataset.insert(name:, animal:, birthdate:)
|
139
217
|
end
|
140
218
|
|
141
|
-
# Models
|
142
|
-
class Pet < Sequel::Model
|
143
|
-
end
|
144
|
-
|
145
|
-
# Helpers
|
146
|
-
module PetsHelper
|
147
|
-
include Pagy::Frontend
|
148
|
-
|
149
|
-
def order_symbol(dir)
|
150
|
-
{ asc: '↗', desc: '↘' }[dir]
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
# Controllers
|
155
|
-
class PetsController < ActionController::Base # :nodoc:
|
156
|
-
include Rails.application.routes.url_helpers
|
157
|
-
include Pagy::Backend
|
158
|
-
|
159
|
-
def index
|
160
|
-
Time.zone = 'UTC'
|
161
|
-
|
162
|
-
@order = { animal: :asc, name: :asc, birthdate: :desc, id: :asc }
|
163
|
-
@pagy, @pets = pagy_keyset(Pet.order(:animal, :name, Sequel.desc(:birthdate), :id))
|
164
|
-
render inline: TEMPLATE
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
TEMPLATE = <<~ERB
|
169
|
-
<!DOCTYPE html>
|
170
|
-
<html lang="en">
|
171
|
-
<html>
|
172
|
-
<head>
|
173
|
-
<title>Pagy Keyset App</title>
|
174
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
175
|
-
<style type="text/css">
|
176
|
-
@media screen { html, body {
|
177
|
-
font-size: 1rem;
|
178
|
-
line-height: 1.2s;
|
179
|
-
padding: 0;
|
180
|
-
margin: 0;
|
181
|
-
} }
|
182
|
-
body {
|
183
|
-
background: white !important;
|
184
|
-
margin: 0 !important;
|
185
|
-
font-family: sans-serif !important;
|
186
|
-
}
|
187
|
-
.content {
|
188
|
-
padding: 1rem 1.5rem 2rem !important;
|
189
|
-
}
|
190
|
-
|
191
|
-
<%== Pagy.root.join('stylesheets', 'pagy.css').read %>
|
192
|
-
</style>
|
193
|
-
</head>
|
194
|
-
|
195
|
-
<body>
|
196
|
-
|
197
|
-
<div class="content">
|
198
|
-
<h1>Pagy Keyset App</h1>
|
199
|
-
<p>Self-contained, standalone Rails app usable to easily reproduce any keyset related pagy issue with Sequel sets.</p>
|
200
|
-
<p>Please, report the following versions in any new issue.</p>
|
201
|
-
<h2>Versions</h2>
|
202
|
-
<ul>
|
203
|
-
<li>Ruby: <%== RUBY_VERSION %></li>
|
204
|
-
<li>Rack: <%== Rack::RELEASE %></li>
|
205
|
-
<li>Rails: <%== Rails.version %></li>
|
206
|
-
<li>Pagy: <%== Pagy::VERSION %></li>
|
207
|
-
</ul>
|
208
|
-
|
209
|
-
<h3>Collection</h3>
|
210
|
-
<div id="records" class="collection">
|
211
|
-
<table border="1" cellspacing="0" cellpadding="3">
|
212
|
-
<tr>
|
213
|
-
<th>animal <%== order_symbol(@order[:animal]) %></th>
|
214
|
-
<th>name <%== order_symbol(@order[:name]) %></th>
|
215
|
-
<th>birthdate <%== order_symbol(@order[:birthdate]) %></th>
|
216
|
-
<th>id <%== order_symbol(@order[:id]) %></th>
|
217
|
-
</tr>
|
218
|
-
<% @pets.each do |pet| %>
|
219
|
-
<tr>
|
220
|
-
<td><%= pet.animal %></td>
|
221
|
-
<td><%= pet.name %></td>
|
222
|
-
<td><%= pet.birthdate %></td>
|
223
|
-
<td><%= pet.id %></td>
|
224
|
-
</tr>
|
225
|
-
<% end %>
|
226
|
-
</table>
|
227
|
-
</div>
|
228
|
-
<p>
|
229
|
-
<nav class="pagy" id="next" aria-label="Pagy next">
|
230
|
-
<%== pagy_next_a(@pagy, text: 'Next page >') %>
|
231
|
-
</nav>
|
232
|
-
</div>
|
233
|
-
|
234
|
-
</body>
|
235
|
-
</html>
|
236
|
-
ERB
|
237
|
-
|
238
219
|
run PagyKeyset
|