Skip to content

Commit 3be3519

Browse files
committed
feat(pagination): introduce pagy gem #195
1 parent 886e0e9 commit 3be3519

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ gem "image_processing", "~> 1.14"
5151

5252
gem "acts-as-taggable-on"
5353
gem "aws-sdk-s3", require: false
54+
gem "pagy"
5455
gem "requestjs-rails"
5556

5657
group :development, :test do

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ GEM
283283
nokogiri (1.18.8-x86_64-linux-musl)
284284
racc (~> 1.4)
285285
ostruct (0.6.1)
286+
pagy (9.3.4)
286287
parallel (1.26.3)
287288
parser (3.3.7.2)
288289
ast (~> 2.4.1)
@@ -510,6 +511,7 @@ DEPENDENCIES
510511
jbuilder
511512
kamal
512513
letter_opener
514+
pagy
513515
pg (~> 1.1)
514516
propshaft
515517
puma (>= 5.0)

app/helpers/application_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module ApplicationHelper
2+
include Pagy::Frontend
3+
24
def flash_class(level)
35
case level
46
when "notice" then "alert-light-success"

config/initializers/pagy.rb

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# frozen_string_literal: true
2+
3+
# Pagy initializer file (9.3.3)
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+
8+
# Pagy Variables
9+
# See https://ddnexus.github.io/pagy/docs/api/pagy#variables
10+
# You can set any pagy variable as a Pagy::DEFAULT. They can also be overridden per instance by just passing them to
11+
# Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods
12+
# Here are the few that make more sense as DEFAULTs:
13+
# Pagy::DEFAULT[:limit] = 20 # default
14+
# Pagy::DEFAULT[:size] = 7 # default
15+
# Pagy::DEFAULT[:ends] = true # default
16+
# Pagy::DEFAULT[:page_param] = :page # default
17+
# Pagy::DEFAULT[:count_args] = [] # example for non AR ORMs
18+
# Pagy::DEFAULT[:max_pages] = 3000 # example
19+
20+
21+
# Extras
22+
# See https://ddnexus.github.io/pagy/categories/extra
23+
24+
25+
# Legacy Compatibility Extras
26+
27+
# Size extra: Enable the Array type for the `:size` variable (e.g. `size: [1,4,4,1]`)
28+
# See https://ddnexus.github.io/pagy/docs/extras/size
29+
# require 'pagy/extras/size' # must be required before the other extras
30+
31+
32+
# Backend Extras
33+
34+
# Arel extra: For better performance utilizing grouped ActiveRecord collections:
35+
# See: https://ddnexus.github.io/pagy/docs/extras/arel
36+
# require 'pagy/extras/arel'
37+
38+
# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding
39+
# See https://ddnexus.github.io/pagy/docs/extras/array
40+
# require 'pagy/extras/array'
41+
42+
# Calendar extra: Add pagination filtering by calendar time unit (year, quarter, month, week, day)
43+
# See https://ddnexus.github.io/pagy/docs/extras/calendar
44+
# require 'pagy/extras/calendar'
45+
# Default for each calendar unit class in IRB:
46+
# >> Pagy::Calendar::Year::DEFAULT
47+
# >> Pagy::Calendar::Quarter::DEFAULT
48+
# >> Pagy::Calendar::Month::DEFAULT
49+
# >> Pagy::Calendar::Week::DEFAULT
50+
# >> Pagy::Calendar::Day::DEFAULT
51+
#
52+
# Uncomment the following lines, if you need calendar localization without using the I18n extra
53+
# module LocalizePagyCalendar
54+
# def localize(time, opts)
55+
# ::I18n.l(time, **opts)
56+
# end
57+
# end
58+
# Pagy::Calendar.prepend LocalizePagyCalendar
59+
60+
# Countless extra: Paginate without any count, saving one query per rendering
61+
# See https://ddnexus.github.io/pagy/docs/extras/countless
62+
# require 'pagy/extras/countless'
63+
# Pagy::DEFAULT[:countless_minimal] = false # default (eager loading)
64+
65+
# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
66+
# See https://ddnexus.github.io/pagy/docs/extras/elasticsearch_rails
67+
# Default :pagy_search method: change only if you use also
68+
# the searchkick or meilisearch extra that defines the same
69+
# Pagy::DEFAULT[:elasticsearch_rails_pagy_search] = :pagy_search
70+
# Default original :search method called internally to do the actual search
71+
# Pagy::DEFAULT[:elasticsearch_rails_search] = :search
72+
# require 'pagy/extras/elasticsearch_rails'
73+
74+
# Headers extra: http response headers (and other helpers) useful for API pagination
75+
# See https://ddnexus.github.io/pagy/docs/extras/headers
76+
# require 'pagy/extras/headers'
77+
# Pagy::DEFAULT[:headers] = { page: 'Current-Page',
78+
# limit: 'Page-Items',
79+
# count: 'Total-Count',
80+
# pages: 'Total-Pages' } # default
81+
82+
# Keyset extra: Paginate with the Pagy keyset pagination technique
83+
# See https://ddnexus.github.io/pagy/docs/extras/keyset
84+
# require 'pagy/extras/keyset'
85+
86+
# Meilisearch extra: Paginate `Meilisearch` result objects
87+
# See https://ddnexus.github.io/pagy/docs/extras/meilisearch
88+
# Default :pagy_search method: change only if you use also
89+
# the elasticsearch_rails or searchkick extra that define the same method
90+
# Pagy::DEFAULT[:meilisearch_pagy_search] = :pagy_search
91+
# Default original :search method called internally to do the actual search
92+
# Pagy::DEFAULT[:meilisearch_search] = :ms_search
93+
# require 'pagy/extras/meilisearch'
94+
95+
# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc.
96+
# See https://ddnexus.github.io/pagy/docs/extras/metadata
97+
# you must require the JS Tools internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
98+
# require 'pagy/extras/js_tools'
99+
# require 'pagy/extras/metadata'
100+
# For performance reasons, you should explicitly set ONLY the metadata you use in the frontend
101+
# Pagy::DEFAULT[:metadata] = %i[scaffold_url page prev next last] # example
102+
103+
# Searchkick extra: Paginate `Searchkick::Results` objects
104+
# See https://ddnexus.github.io/pagy/docs/extras/searchkick
105+
# Default :pagy_search method: change only if you use also
106+
# the elasticsearch_rails or meilisearch extra that defines the same
107+
# Pagy::DEFAULT[:searchkick_pagy_search] = :pagy_search
108+
# Default original :search method called internally to do the actual search
109+
# Pagy::DEFAULT[:searchkick_search] = :search
110+
# require 'pagy/extras/searchkick'
111+
# uncomment if you are going to use Searchkick.pagy_search
112+
# Searchkick.extend Pagy::Searchkick
113+
114+
115+
# Frontend Extras
116+
117+
# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
118+
# See https://ddnexus.github.io/pagy/docs/extras/bootstrap
119+
require "pagy/extras/bootstrap"
120+
121+
# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination
122+
# See https://ddnexus.github.io/pagy/docs/extras/bulma
123+
# require 'pagy/extras/bulma'
124+
125+
# Pagy extra: Add the pagy styled versions of the javascript-powered navs
126+
# and a few other components to the Pagy::Frontend module.
127+
# See https://ddnexus.github.io/pagy/docs/extras/pagy
128+
# require 'pagy/extras/pagy'
129+
130+
# Multi size var used by the *_nav_js helpers
131+
# See https://ddnexus.github.io/pagy/docs/extras/pagy#steps
132+
# Pagy::DEFAULT[:steps] = { 0 => 5, 540 => 7, 720 => 9 } # example
133+
134+
135+
# Feature Extras
136+
137+
# Gearbox extra: Automatically change the limit per page depending on the page number
138+
# See https://ddnexus.github.io/pagy/docs/extras/gearbox
139+
# require 'pagy/extras/gearbox'
140+
# set to false only if you want to make :gearbox_extra an opt-in variable
141+
# Pagy::DEFAULT[:gearbox_extra] = false # default true
142+
# Pagy::DEFAULT[:gearbox_limit] = [15, 30, 60, 100] # default
143+
144+
# Limit extra: Allow the client to request a custom limit per page with an optional selector UI
145+
# See https://ddnexus.github.io/pagy/docs/extras/limit
146+
# require 'pagy/extras/limit'
147+
# set to false only if you want to make :limit_extra an opt-in variable
148+
# Pagy::DEFAULT[:limit_extra] = false # default true
149+
# Pagy::DEFAULT[:limit_param] = :limit # default
150+
# Pagy::DEFAULT[:limit_max] = 100 # default
151+
152+
# Overflow extra: Allow for easy handling of overflowing pages
153+
# See https://ddnexus.github.io/pagy/docs/extras/overflow
154+
# require 'pagy/extras/overflow'
155+
# Pagy::DEFAULT[:overflow] = :empty_page # default (other options: :last_page and :exception)
156+
157+
# Trim extra: Remove the page=1 param from links
158+
# See https://ddnexus.github.io/pagy/docs/extras/trim
159+
# require 'pagy/extras/trim'
160+
# set to false only if you want to make :trim_extra an opt-in variable
161+
# Pagy::DEFAULT[:trim_extra] = false # default true
162+
163+
# Standalone extra: Use pagy in non Rack environment/gem
164+
# See https://ddnexus.github.io/pagy/docs/extras/standalone
165+
# require 'pagy/extras/standalone'
166+
# Pagy::DEFAULT[:url] = 'http://www.example.com/subdir' # optional default
167+
168+
# Jsonapi extra: Implements JSON:API specifications
169+
# See https://ddnexus.github.io/pagy/docs/extras/jsonapi
170+
# require 'pagy/extras/jsonapi' # must be required after the other extras
171+
# set to false only if you want to make :jsonapi an opt-in variable
172+
# Pagy::DEFAULT[:jsonapi] = false # default true
173+
174+
# Rails
175+
# Enable the .js file required by the helpers that use javascript
176+
# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_limit_selector_js)
177+
# See https://ddnexus.github.io/pagy/docs/api/javascript
178+
179+
# With the asset pipeline
180+
# Sprockets need to look into the pagy javascripts dir, so add it to the assets paths
181+
# Rails.application.config.assets.paths << Pagy.root.join('javascripts')
182+
183+
# I18n
184+
185+
# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
186+
# See https://ddnexus.github.io/pagy/docs/api/i18n
187+
# Notice: No need to configure anything in this section if your app uses only "en"
188+
# or if you use the i18n extra below
189+
#
190+
# Examples:
191+
# load the "de" built-in locale:
192+
# Pagy::I18n.load(locale: 'de')
193+
#
194+
# load the "de" locale defined in the custom file at :filepath:
195+
# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
196+
#
197+
# load the "de", "en" and "es" built-in locales:
198+
# (the first passed :locale will be used also as the default_locale)
199+
# Pagy::I18n.load({ locale: 'de' },
200+
# { locale: 'en' },
201+
# { locale: 'es' })
202+
#
203+
# load the "en" built-in locale, a custom "es" locale,
204+
# and a totally custom locale complete with a custom :pluralize proc:
205+
# (the first passed :locale will be used also as the default_locale)
206+
# Pagy::I18n.load({ locale: 'en' },
207+
# { locale: 'es', filepath: 'path/to/pagy-es.yml' },
208+
# { locale: 'xyz', # not built-in
209+
# filepath: 'path/to/pagy-xyz.yml',
210+
# pluralize: lambda{ |count| ... } )
211+
212+
213+
# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
214+
# than the default pagy internal i18n (see above)
215+
# See https://ddnexus.github.io/pagy/docs/extras/i18n
216+
require "pagy/extras/i18n"
217+
218+
219+
# When you are done setting your own default freeze it, so it will not get changed accidentally
220+
Pagy::DEFAULT.freeze

config/locales/pagy.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
pagy:
3+
prev: "Previous"
4+
next: "Next"

0 commit comments

Comments
 (0)