Skip to content

Commit d884403

Browse files
Replace cursor_pagination with pagy_cursor (#158)
* wip: experiment pagy-cursor * feat: update cursor_helpers using pagy_cursor * chore: add changelog entry * fix: lint failure * fix: exclude pagy_cursor from gemspec because it's a conditional dependency * chore: bump version to 2.0.0
1 parent a9faf5b commit d884403

File tree

7 files changed

+251
-7
lines changed

7 files changed

+251
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
### Changelog
22

3-
#### 1.2.2 (Next)
3+
#### 2.0.0 (Next)
44

55
* [#145](https://github.com/slack-ruby/slack-ruby-bot-server/pull/145): Added support for Ruby 3.1 - [@dblock](https://github.com/dblock).
66
* [#146](https://github.com/slack-ruby/slack-ruby-bot-server/pull/146): Added support for MongoDB 5.0 - [@dblock](https://github.com/dblock).
77
* [#147](https://github.com/slack-ruby/slack-ruby-bot-server/pull/147): Added support for PostgreSQL 14 - [@dblock](https://github.com/dblock).
88
* [#155](https://github.com/slack-ruby/slack-ruby-bot-server/pull/155): Enable mongoid to get connection URI from ENV for test - [@crazyoptimist](https://github.com/crazyoptimist).
99
* [#157](https://github.com/slack-ruby/slack-ruby-bot-server/pull/157): Added test env setup guide for linux users - [@crazyoptimist](https://github.com/crazyoptimist).
10+
* [#158](https://github.com/slack-ruby/slack-ruby-bot-server/pull/158): Replace `cursor_pagination` with `pagy_cursor` - [@crazyoptimist](https://github.com/crazyoptimist).
1011
* Your contribution here.
1112

1213
#### 1.2.1 (2022/03/06)

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ when 'mongoid' then
99
when 'activerecord' then
1010
gem 'activerecord', '~> 6.0.0'
1111
gem 'otr-activerecord'
12-
gem 'cursor_pagination', github: 'dblock/cursor_pagination', branch: 'misc' # rubocop:disable Bundler/OrderedGems
12+
gem 'pagy_cursor'
1313
gem 'pg'
1414
when nil
1515
warn "Missing ENV['DATABASE_ADAPTER']."

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ gem 'slack-ruby-bot-server'
6565

6666
#### ActiveRecord
6767

68-
Use ActiveRecord with, for example, PostgreSQL via [pg](https://github.com/ged/ruby-pg). Add the `activerecord`, `pg`, `otr-activerecord` and `cursor_pagination` gems to your Gemfile.
68+
Use ActiveRecord with, for example, PostgreSQL via [pg](https://github.com/ged/ruby-pg). Add the `activerecord`, `pg`, `otr-activerecord` and `pagy_cursor` gems to your Gemfile.
6969

7070
```
7171
gem 'pg'
7272
gem 'activerecord', require: 'active_record'
7373
gem 'slack-ruby-bot-server'
7474
gem 'otr-activerecord'
75-
gem 'cursor_pagination'
75+
gem 'pagy_cursor'
7676
```
7777

7878
Configure the database connection in `config/postgresql.yml`.

lib/slack-ruby-bot-server.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require 'slack-ruby-client'
44
require 'grape-swagger'
5+
require 'pagy'
6+
require 'pagy_cursor/pagy/extras/cursor'
57

68
require_relative 'slack-ruby-bot-server/loggable'
79
require_relative 'slack-ruby-bot-server/service'

lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Api
33
module Helpers
44
module CursorHelpers
55
extend ActiveSupport::Concern
6+
include Pagy::Cursor::Backend
67

78
# apply cursor-based pagination to a collection
89
# returns a hash:
@@ -33,12 +34,16 @@ def paginate_by_cursor(coll, options)
3334
results[:total_count] = coll.count(:all) if params[:total_count]
3435
coll = coll.offset(params[:offset].to_i) if params.key?(:offset)
3536
sort_options = {}
37+
cursor_direction = :after
3638
sort_order(options).each do |order|
3739
sort_options[order[:column]] = { reverse: true } if order[:direction] == :desc
40+
cursor_direction = :before if order[:column] == coll.primary_key && order[:direction] == :desc
3841
end
39-
coll = coll.cursor(params[:cursor], columns: sort_options).per(size)
42+
cursor_vars = { items: size }
43+
cursor_vars[cursor_direction] = params[:cursor].to_i if params.key?(:cursor)
44+
pagy_cursor, coll = pagy_cursor(coll, cursor_vars, { order: sort_options })
4045
results[:results] = coll.to_a
41-
results[:next] = coll.next_cursor.to_s unless coll.last_page?
46+
results[:next] = coll.last[:id].to_s if pagy_cursor.has_more?
4247
results
4348
end
4449
end
+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# frozen_string_literal: true
2+
3+
# Pagy initializer file (6.0.0)
4+
# Customize only what you really need and notice that the core Pagy works also without any of the following lines.
5+
# Should you just cherry pick part of this file, please maintain the require-order of the extras
6+
7+
# Pagy DEFAULT Variables
8+
# See https://ddnexus.github.io/pagy/docs/api/pagy#variables
9+
# All the Pagy::DEFAULT are set for all the Pagy instances but can be overridden per instance by just passing them to
10+
# Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods
11+
12+
# Instance variables
13+
# See https://ddnexus.github.io/pagy/docs/api/pagy#instance-variables
14+
# Pagy::DEFAULT[:page] = 1 # default
15+
# Pagy::DEFAULT[:items] = 20 # default
16+
# Pagy::DEFAULT[:outset] = 0 # default
17+
18+
# Other Variables
19+
# See https://ddnexus.github.io/pagy/docs/api/pagy#other-variables
20+
# Pagy::DEFAULT[:size] = [1,4,4,1] # default
21+
# Pagy::DEFAULT[:page_param] = :page # default
22+
# The :params can be also set as a lambda e.g ->(params){ params.exclude('useless').merge!('custom' => 'useful') }
23+
# Pagy::DEFAULT[:params] = {} # default
24+
# Pagy::DEFAULT[:fragment] = '#fragment' # example
25+
# Pagy::DEFAULT[:link_extra] = 'data-remote="true"' # example
26+
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
27+
# Pagy::DEFAULT[:cycle] = true # example
28+
# Pagy::DEFAULT[:request_path] = "/foo" # example
29+
30+
# Extras
31+
# See https://ddnexus.github.io/pagy/docs/extras
32+
33+
# Backend Extras
34+
35+
# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding
36+
# See https://ddnexus.github.io/pagy/docs/extras/array
37+
# require 'pagy/extras/array'
38+
39+
# Calendar extra: Add pagination filtering by calendar time unit (year, quarter, month, week, day)
40+
# See https://ddnexus.github.io/pagy/docs/extras/calendar
41+
# require 'pagy/extras/calendar'
42+
# Default for each unit
43+
# Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination
44+
# Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format
45+
#
46+
# Pagy::Calendar::Quarter::DEFAULT[:order] = :asc # Time direction of pagination
47+
# Pagy::Calendar::Quarter::DEFAULT[:format] = '%Y-Q%q' # strftime format
48+
#
49+
# Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
50+
# Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
51+
#
52+
# Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination
53+
# Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format
54+
#
55+
# Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
56+
# Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
57+
#
58+
# Uncomment the following lines, if you need calendar localization without using the I18n extra
59+
# module LocalizePagyCalendar
60+
# def localize(time, opts)
61+
# ::I18n.l(time, **opts)
62+
# end
63+
# end
64+
# Pagy::Calendar.prepend LocalizePagyCalendar
65+
66+
# Countless extra: Paginate without any count, saving one query per rendering
67+
# See https://ddnexus.github.io/pagy/docs/extras/countless
68+
# require 'pagy/extras/countless'
69+
# Pagy::DEFAULT[:countless_minimal] = false # default (eager loading)
70+
71+
# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
72+
# See https://ddnexus.github.io/pagy/docs/extras/elasticsearch_rails
73+
# Default :pagy_search method: change only if you use also
74+
# the searchkick or meilisearch extra that defines the same
75+
# Pagy::DEFAULT[:elasticsearch_rails_pagy_search] = :pagy_search
76+
# Default original :search method called internally to do the actual search
77+
# Pagy::DEFAULT[:elasticsearch_rails_search] = :search
78+
# require 'pagy/extras/elasticsearch_rails'
79+
80+
# Headers extra: http response headers (and other helpers) useful for API pagination
81+
# See http://ddnexus.github.io/pagy/extras/headers
82+
# require 'pagy/extras/headers'
83+
# Pagy::DEFAULT[:headers] = { page: 'Current-Page',
84+
# items: 'Page-Items',
85+
# count: 'Total-Count',
86+
# pages: 'Total-Pages' } # default
87+
88+
# Meilisearch extra: Paginate `Meilisearch` result objects
89+
# See https://ddnexus.github.io/pagy/docs/extras/meilisearch
90+
# Default :pagy_search method: change only if you use also
91+
# the elasticsearch_rails or searchkick extra that define the same method
92+
# Pagy::DEFAULT[:meilisearch_pagy_search] = :pagy_search
93+
# Default original :search method called internally to do the actual search
94+
# Pagy::DEFAULT[:meilisearch_search] = :ms_search
95+
# require 'pagy/extras/meilisearch'
96+
97+
# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc.
98+
# See https://ddnexus.github.io/pagy/docs/extras/metadata
99+
# you must require the frontend helpers internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
100+
# require 'pagy/extras/frontend_helpers'
101+
# require 'pagy/extras/metadata'
102+
# For performance reasons, you should explicitly set ONLY the metadata you use in the frontend
103+
# Pagy::DEFAULT[:metadata] = %i[scaffold_url page prev next last] # example
104+
105+
# Searchkick extra: Paginate `Searchkick::Results` objects
106+
# See https://ddnexus.github.io/pagy/docs/extras/searchkick
107+
# Default :pagy_search method: change only if you use also
108+
# the elasticsearch_rails or meilisearch extra that defines the same
109+
# DEFAULT[:searchkick_pagy_search] = :pagy_search
110+
# Default original :search method called internally to do the actual search
111+
# Pagy::DEFAULT[:searchkick_search] = :search
112+
# require 'pagy/extras/searchkick'
113+
# uncomment if you are going to use Searchkick.pagy_search
114+
# Searchkick.extend Pagy::Searchkick
115+
116+
# Frontend Extras
117+
118+
# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
119+
# See https://ddnexus.github.io/pagy/docs/extras/bootstrap
120+
# require 'pagy/extras/bootstrap'
121+
122+
# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination
123+
# See https://ddnexus.github.io/pagy/docs/extras/bulma
124+
# require 'pagy/extras/bulma'
125+
126+
# Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination
127+
# See https://ddnexus.github.io/pagy/docs/extras/foundation
128+
# require 'pagy/extras/foundation'
129+
130+
# Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination
131+
# See https://ddnexus.github.io/pagy/docs/extras/materialize
132+
# require 'pagy/extras/materialize'
133+
134+
# Navs extra: Add nav_js and combo_nav_js javascript helpers
135+
# Notice: the other frontend extras add their own framework-styled versions,
136+
# so require this extra only if you need the unstyled version
137+
# See https://ddnexus.github.io/pagy/docs/extras/navs
138+
# require 'pagy/extras/navs'
139+
140+
# Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination
141+
# See https://ddnexus.github.io/pagy/docs/extras/semantic
142+
# require 'pagy/extras/semantic'
143+
144+
# UIkit extra: Add nav helper and templates for UIkit pagination
145+
# See https://ddnexus.github.io/pagy/docs/extras/uikit
146+
# require 'pagy/extras/uikit'
147+
148+
# Multi size var used by the *_nav_js helpers
149+
# See https://ddnexus.github.io/pagy/docs/extras/navs#steps
150+
# Pagy::DEFAULT[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example
151+
152+
# Feature Extras
153+
154+
# Gearbox extra: Automatically change the number of items per page depending on the page number
155+
# See https://ddnexus.github.io/pagy/docs/extras/gearbox
156+
# require 'pagy/extras/gearbox'
157+
# set to false only if you want to make :gearbox_extra an opt-in variable
158+
# Pagy::DEFAULT[:gearbox_extra] = false # default true
159+
# Pagy::DEFAULT[:gearbox_items] = [15, 30, 60, 100] # default
160+
161+
# Items extra: Allow the client to request a custom number of items per page with an optional selector UI
162+
# See https://ddnexus.github.io/pagy/docs/extras/items
163+
# require 'pagy/extras/items'
164+
# set to false only if you want to make :items_extra an opt-in variable
165+
# Pagy::DEFAULT[:items_extra] = false # default true
166+
# Pagy::DEFAULT[:items_param] = :items # default
167+
# Pagy::DEFAULT[:max_items] = 100 # default
168+
169+
# Overflow extra: Allow for easy handling of overflowing pages
170+
# See https://ddnexus.github.io/pagy/docs/extras/overflow
171+
# require 'pagy/extras/overflow'
172+
# Pagy::DEFAULT[:overflow] = :empty_page # default (other options: :last_page and :exception)
173+
174+
# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination
175+
# See https://ddnexus.github.io/pagy/docs/extras/support
176+
# require 'pagy/extras/support'
177+
178+
# Trim extra: Remove the page=1 param from links
179+
# See https://ddnexus.github.io/pagy/docs/extras/trim
180+
# require 'pagy/extras/trim'
181+
# set to false only if you want to make :trim_extra an opt-in variable
182+
# Pagy::DEFAULT[:trim_extra] = false # default true
183+
184+
# Standalone extra: Use pagy in non Rack environment/gem
185+
# See https://ddnexus.github.io/pagy/docs/extras/standalone
186+
# require 'pagy/extras/standalone'
187+
# Pagy::DEFAULT[:url] = 'http://www.example.com/subdir' # optional default
188+
189+
# Rails
190+
# Enable the .js file required by the helpers that use javascript
191+
# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js)
192+
# See https://ddnexus.github.io/pagy/docs/extras#javascript
193+
194+
# With the asset pipeline
195+
# Sprockets need to look into the pagy javascripts dir, so add it to the assets paths
196+
# Rails.application.config.assets.paths << Pagy.root.join('javascripts')
197+
198+
# I18n
199+
200+
# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
201+
# See https://ddnexus.github.io/pagy/docs/api/frontend#i18n
202+
# Notice: No need to configure anything in this section if your app uses only "en"
203+
# or if you use the i18n extra below
204+
#
205+
# Examples:
206+
# load the "de" built-in locale:
207+
# Pagy::I18n.load(locale: 'de')
208+
#
209+
# load the "de" locale defined in the custom file at :filepath:
210+
# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
211+
#
212+
# load the "de", "en" and "es" built-in locales:
213+
# (the first passed :locale will be used also as the default_locale)
214+
# Pagy::I18n.load({ locale: 'de' },
215+
# { locale: 'en' },
216+
# { locale: 'es' })
217+
#
218+
# load the "en" built-in locale, a custom "es" locale,
219+
# and a totally custom locale complete with a custom :pluralize proc:
220+
# (the first passed :locale will be used also as the default_locale)
221+
# Pagy::I18n.load({ locale: 'en' },
222+
# { locale: 'es', filepath: 'path/to/pagy-es.yml' },
223+
# { locale: 'xyz', # not built-in
224+
# filepath: 'path/to/pagy-xyz.yml',
225+
# pluralize: lambda{ |count| ... } )
226+
227+
# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
228+
# than the default pagy internal i18n (see above)
229+
# See https://ddnexus.github.io/pagy/docs/extras/i18n
230+
# require 'pagy/extras/i18n'
231+
232+
# Default i18n key
233+
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
234+
235+
# When you are done setting your own default freeze it, so it will not get changed accidentally
236+
Pagy::DEFAULT.freeze

lib/slack-ruby-bot-server/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module SlackRubyBotServer
2-
VERSION = '1.2.2'.freeze
2+
VERSION = '2.0.0'.freeze
33
end

0 commit comments

Comments
 (0)