From 7cff849d790c42eb5b71e0188692a92d41f75cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 1 Mar 2023 17:07:13 +0800 Subject: [PATCH 1/4] Add more users when seeding db --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 98ae840..3671ad2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,7 +10,7 @@ Sidekiq::Testing.inline! do ldap.add_attribute "cn=admin,ou=kosmos.org,cn=users,dc=kosmos,dc=org", :admin, "true" - 5.times do |n| + 35.times do |n| username = Faker::Name.unique.first_name.downcase email = Faker::Internet.unique.email From 3aad27c7bd12ef7b7b0a26bbd16b3e83b38815b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 1 Mar 2023 17:08:24 +0800 Subject: [PATCH 2/4] Add Pagy gem, config, styles --- Gemfile | 1 + Gemfile.lock | 2 + .../stylesheets/application.tailwind.css | 1 + .../stylesheets/components/pagination.css | 45 ++++ app/controllers/admin/base_controller.rb | 2 +- app/helpers/application_helper.rb | 4 +- config/initializers/pagy.rb | 250 ++++++++++++++++++ 7 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 app/assets/stylesheets/components/pagination.css create mode 100644 config/initializers/pagy.rb diff --git a/Gemfile b/Gemfile index 169f39e..97c230e 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,7 @@ gem 'net-ldap' # Utilities gem "rqrcode", "~> 2.0" gem 'rails-settings-cached', '~> 2.8.3' +gem 'pagy', '~> 6.0', '>= 6.0.2' # HTTP requests gem 'faraday' diff --git a/Gemfile.lock b/Gemfile.lock index 5a658b5..3e28d0f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -178,6 +178,7 @@ GEM nokogiri (1.13.9-x86_64-linux) racc (~> 1.4) orm_adapter (0.5.0) + pagy (6.0.2) pg (1.2.3) public_suffix (5.0.0) puma (4.3.12) @@ -327,6 +328,7 @@ DEPENDENCIES listen (~> 3.2) lockbox net-ldap + pagy (~> 6.0, >= 6.0.2) pg (~> 1.2.3) puma (~> 4.1) rails (~> 7.0.2) diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css index 7a9da2e..5ee4651 100644 --- a/app/assets/stylesheets/application.tailwind.css +++ b/app/assets/stylesheets/application.tailwind.css @@ -7,4 +7,5 @@ @import "components/forms"; @import "components/links"; @import "components/notifications"; +@import "components/pagination"; @import "components/tables"; diff --git a/app/assets/stylesheets/components/pagination.css b/app/assets/stylesheets/components/pagination.css new file mode 100644 index 0000000..f6ab6c6 --- /dev/null +++ b/app/assets/stylesheets/components/pagination.css @@ -0,0 +1,45 @@ +@layer components { + .pagy-nav.pagination { + @apply isolate inline-flex -space-x-px rounded-md shadow-sm; + } + + .pagy-nav .page:not(.prev):not(.next) { + @apply hidden sm:inline-block; + } + + .pagy-nav .page.next a { + @apply relative inline-flex items-center rounded-r-md border + border-gray-300 bg-white px-3 py-2 text-sm font-medium + text-gray-500 hover:bg-gray-100 focus:z-20; + } + + .pagy-nav .page.prev a { + @apply relative inline-flex items-center rounded-l-md border + border-gray-300 bg-white px-3 py-2 text-sm font-medium + text-gray-500 hover:bg-gray-100 focus:z-20; + } + + .pagy-nav .page.next.disabled { + @apply relative inline-flex items-center rounded-r-md border + border-gray-300 bg-gray-100 px-3 py-2 text-sm font-medium + text-gray-400 focus:z-20; + } + + .pagy-nav .page.prev.disabled { + @apply relative inline-flex items-center rounded-l-md border + border-gray-300 bg-gray-100 px-3 py-2 text-sm font-medium + text-gray-400 focus:z-20; + } + + .pagy-nav .page a, .page.gap { + @apply bg-white border-gray-300 text-gray-500 hover:bg-gray-100 relative + inline-flex items-center border px-4 py-2 text-sm font-medium + focus:z-20; + } + + .pagy-nav .page.active { + @apply z-10 border-indigo-500 bg-indigo-50 text-indigo-600 relative + inline-flex items-center border px-4 py-2 text-sm font-medium + focus:z-20; + } +} diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 957328e..d504235 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,4 +1,5 @@ class Admin::BaseController < ApplicationController + include Pagy::Backend before_action :authenticate_user! before_action :authorize_admin @@ -7,5 +8,4 @@ class Admin::BaseController < ApplicationController def set_context @context = :admin end - end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 037be7c..b4b4f69 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,6 @@ module ApplicationHelper + include Pagy::Frontend + def sats_to_btc(sats) sats.to_f / 100000000 end @@ -16,6 +18,4 @@ module ApplicationHelper def badge(text, color) tag.span text, class: "inline-flex items-center rounded-full bg-#{color}-100 px-2.5 py-0.5 text-xs font-medium text-#{color}-800" end - end - diff --git a/config/initializers/pagy.rb b/config/initializers/pagy.rb new file mode 100644 index 0000000..16b8843 --- /dev/null +++ b/config/initializers/pagy.rb @@ -0,0 +1,250 @@ +# frozen_string_literal: true + +# Pagy initializer file (6.0.2) +# Customize only what you really need and notice that the core Pagy works also without any of the following lines. +# Should you just cherry pick part of this file, please maintain the require-order of the extras + + +# Pagy DEFAULT Variables +# See https://ddnexus.github.io/pagy/docs/api/pagy#variables +# All the Pagy::DEFAULT are set for all the Pagy instances but can be overridden per instance by just passing them to +# Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods + + +# Instance variables +# See https://ddnexus.github.io/pagy/docs/api/pagy#instance-variables +# Pagy::DEFAULT[:page] = 1 # default +# Pagy::DEFAULT[:items] = 20 # default +# Pagy::DEFAULT[:outset] = 0 # default + + +# Other Variables +# See https://ddnexus.github.io/pagy/docs/api/pagy#other-variables +# Pagy::DEFAULT[:size] = [1,4,4,1] # default +# Pagy::DEFAULT[:page_param] = :page # default +# The :params can be also set as a lambda e.g ->(params){ params.exclude('useless').merge!('custom' => 'useful') } +# Pagy::DEFAULT[:params] = {} # default +# Pagy::DEFAULT[:fragment] = '#fragment' # example +# Pagy::DEFAULT[:link_extra] = 'data-remote="true"' # example +# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default +# Pagy::DEFAULT[:cycle] = true # example +# Pagy::DEFAULT[:request_path] = "/foo" # example + + +# Extras +# See https://ddnexus.github.io/pagy/categories/extra + + +# Backend Extras + +# Arel extra: For better performance utilizing grouped ActiveRecord collections: +# See: https://ddnexus.github.io/pagy/docs/extras/arel +# require 'pagy/extras/arel' + +# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding +# See https://ddnexus.github.io/pagy/docs/extras/array +# require 'pagy/extras/array' + +# Calendar extra: Add pagination filtering by calendar time unit (year, quarter, month, week, day) +# See https://ddnexus.github.io/pagy/docs/extras/calendar +# require 'pagy/extras/calendar' +# Default for each unit +# Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination +# Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format +# +# Pagy::Calendar::Quarter::DEFAULT[:order] = :asc # Time direction of pagination +# Pagy::Calendar::Quarter::DEFAULT[:format] = '%Y-Q%q' # strftime format +# +# Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination +# Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format +# +# Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination +# Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format +# +# Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination +# Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format +# +# Uncomment the following lines, if you need calendar localization without using the I18n extra +# module LocalizePagyCalendar +# def localize(time, opts) +# ::I18n.l(time, **opts) +# end +# end +# Pagy::Calendar.prepend LocalizePagyCalendar + +# Countless extra: Paginate without any count, saving one query per rendering +# See https://ddnexus.github.io/pagy/docs/extras/countless +# require 'pagy/extras/countless' +# Pagy::DEFAULT[:countless_minimal] = false # default (eager loading) + +# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects +# See https://ddnexus.github.io/pagy/docs/extras/elasticsearch_rails +# Default :pagy_search method: change only if you use also +# the searchkick or meilisearch extra that defines the same +# Pagy::DEFAULT[:elasticsearch_rails_pagy_search] = :pagy_search +# Default original :search method called internally to do the actual search +# Pagy::DEFAULT[:elasticsearch_rails_search] = :search +# require 'pagy/extras/elasticsearch_rails' + +# Headers extra: http response headers (and other helpers) useful for API pagination +# See http://ddnexus.github.io/pagy/extras/headers +# require 'pagy/extras/headers' +# Pagy::DEFAULT[:headers] = { page: 'Current-Page', +# items: 'Page-Items', +# count: 'Total-Count', +# pages: 'Total-Pages' } # default + +# Meilisearch extra: Paginate `Meilisearch` result objects +# See https://ddnexus.github.io/pagy/docs/extras/meilisearch +# Default :pagy_search method: change only if you use also +# the elasticsearch_rails or searchkick extra that define the same method +# Pagy::DEFAULT[:meilisearch_pagy_search] = :pagy_search +# Default original :search method called internally to do the actual search +# Pagy::DEFAULT[:meilisearch_search] = :ms_search +# require 'pagy/extras/meilisearch' + +# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc. +# See https://ddnexus.github.io/pagy/docs/extras/metadata +# you must require the frontend helpers internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels +# require 'pagy/extras/frontend_helpers' +# require 'pagy/extras/metadata' +# For performance reasons, you should explicitly set ONLY the metadata you use in the frontend +# Pagy::DEFAULT[:metadata] = %i[scaffold_url page prev next last] # example + +# Searchkick extra: Paginate `Searchkick::Results` objects +# See https://ddnexus.github.io/pagy/docs/extras/searchkick +# Default :pagy_search method: change only if you use also +# the elasticsearch_rails or meilisearch extra that defines the same +# DEFAULT[:searchkick_pagy_search] = :pagy_search +# Default original :search method called internally to do the actual search +# Pagy::DEFAULT[:searchkick_search] = :search +# require 'pagy/extras/searchkick' +# uncomment if you are going to use Searchkick.pagy_search +# Searchkick.extend Pagy::Searchkick + + +# Frontend Extras + +# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination +# See https://ddnexus.github.io/pagy/docs/extras/bootstrap +# require 'pagy/extras/bootstrap' + +# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination +# See https://ddnexus.github.io/pagy/docs/extras/bulma +# require 'pagy/extras/bulma' + +# Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination +# See https://ddnexus.github.io/pagy/docs/extras/foundation +# require 'pagy/extras/foundation' + +# Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination +# See https://ddnexus.github.io/pagy/docs/extras/materialize +# require 'pagy/extras/materialize' + +# Navs extra: Add nav_js and combo_nav_js javascript helpers +# Notice: the other frontend extras add their own framework-styled versions, +# so require this extra only if you need the unstyled version +# See https://ddnexus.github.io/pagy/docs/extras/navs +# require 'pagy/extras/navs' + +# Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination +# See https://ddnexus.github.io/pagy/docs/extras/semantic +# require 'pagy/extras/semantic' + +# UIkit extra: Add nav helper and templates for UIkit pagination +# See https://ddnexus.github.io/pagy/docs/extras/uikit +# require 'pagy/extras/uikit' + +# Multi size var used by the *_nav_js helpers +# See https://ddnexus.github.io/pagy/docs/extras/navs#steps +# Pagy::DEFAULT[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example + + +# Feature Extras + +# Gearbox extra: Automatically change the number of items per page depending on the page number +# See https://ddnexus.github.io/pagy/docs/extras/gearbox +# require 'pagy/extras/gearbox' +# set to false only if you want to make :gearbox_extra an opt-in variable +# Pagy::DEFAULT[:gearbox_extra] = false # default true +# Pagy::DEFAULT[:gearbox_items] = [15, 30, 60, 100] # default + +# Items extra: Allow the client to request a custom number of items per page with an optional selector UI +# See https://ddnexus.github.io/pagy/docs/extras/items +# require 'pagy/extras/items' +# set to false only if you want to make :items_extra an opt-in variable +# Pagy::DEFAULT[:items_extra] = false # default true +# Pagy::DEFAULT[:items_param] = :items # default +# Pagy::DEFAULT[:max_items] = 100 # default + +# Overflow extra: Allow for easy handling of overflowing pages +# See https://ddnexus.github.io/pagy/docs/extras/overflow +# require 'pagy/extras/overflow' +# Pagy::DEFAULT[:overflow] = :empty_page # default (other options: :last_page and :exception) + +# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination +# See https://ddnexus.github.io/pagy/docs/extras/support +# require 'pagy/extras/support' + +# Trim extra: Remove the page=1 param from links +# See https://ddnexus.github.io/pagy/docs/extras/trim +# require 'pagy/extras/trim' +# set to false only if you want to make :trim_extra an opt-in variable +# Pagy::DEFAULT[:trim_extra] = false # default true + +# Standalone extra: Use pagy in non Rack environment/gem +# See https://ddnexus.github.io/pagy/docs/extras/standalone +# require 'pagy/extras/standalone' +# Pagy::DEFAULT[:url] = 'http://www.example.com/subdir' # optional default + + +# Rails +# Enable the .js file required by the helpers that use javascript +# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js) +# See https://ddnexus.github.io/pagy/docs/api/javascript + +# With the asset pipeline +# Sprockets need to look into the pagy javascripts dir, so add it to the assets paths +# Rails.application.config.assets.paths << Pagy.root.join('javascripts') + +# I18n + +# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem +# See https://ddnexus.github.io/pagy/docs/api/i18n +# Notice: No need to configure anything in this section if your app uses only "en" +# or if you use the i18n extra below +# +# Examples: +# load the "de" built-in locale: +# Pagy::I18n.load(locale: 'de') +# +# load the "de" locale defined in the custom file at :filepath: +# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml') +# +# load the "de", "en" and "es" built-in locales: +# (the first passed :locale will be used also as the default_locale) +# Pagy::I18n.load({ locale: 'de' }, +# { locale: 'en' }, +# { locale: 'es' }) +# +# load the "en" built-in locale, a custom "es" locale, +# and a totally custom locale complete with a custom :pluralize proc: +# (the first passed :locale will be used also as the default_locale) +# Pagy::I18n.load({ locale: 'en' }, +# { locale: 'es', filepath: 'path/to/pagy-es.yml' }, +# { locale: 'xyz', # not built-in +# filepath: 'path/to/pagy-xyz.yml', +# pluralize: lambda{ |count| ... } ) + + +# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory +# than the default pagy internal i18n (see above) +# See https://ddnexus.github.io/pagy/docs/extras/i18n +# require 'pagy/extras/i18n' + +# Default i18n key +# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default + + +# When you are done setting your own default freeze it, so it will not get changed accidentally +Pagy::DEFAULT.freeze From cbbb4c6e4751269fdad286bf355369bcaec331e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 1 Mar 2023 17:08:36 +0800 Subject: [PATCH 3/4] Add pagination to admin pages --- app/controllers/admin/donations_controller.rb | 3 ++- app/controllers/admin/invitations_controller.rb | 3 ++- app/controllers/admin/users_controller.rb | 2 +- app/views/admin/donations/index.html.erb | 3 ++- app/views/admin/invitations/index.html.erb | 3 ++- app/views/admin/users/index.html.erb | 3 ++- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/donations_controller.rb b/app/controllers/admin/donations_controller.rb index ffbae14..c29fb80 100644 --- a/app/controllers/admin/donations_controller.rb +++ b/app/controllers/admin/donations_controller.rb @@ -5,7 +5,8 @@ class Admin::DonationsController < Admin::BaseController # GET /donations # GET /donations.json def index - @donations = Donation.all.order('created_at desc') + @pagy, @donations = pagy(Donation.all.order('created_at desc')) + @stats = { overall_sats: @donations.all.sum("amount_sats"), donor_count: Donation.distinct.count(:user_id) diff --git a/app/controllers/admin/invitations_controller.rb b/app/controllers/admin/invitations_controller.rb index ca9b407..97a33b3 100644 --- a/app/controllers/admin/invitations_controller.rb +++ b/app/controllers/admin/invitations_controller.rb @@ -1,7 +1,8 @@ class Admin::InvitationsController < Admin::BaseController def index @current_section = :invitations - @invitations_used = Invitation.used.order('used_at desc') + @pagy, @invitations_used = pagy(Invitation.used.order('used_at desc')) + @stats = { available: Invitation.unused.count, accepted: @invitations_used.length, diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b23f468..2081e7a 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -6,7 +6,7 @@ class Admin::UsersController < Admin::BaseController ldap = LdapService.new @ou = params[:ou] || "kosmos.org" @orgs = ldap.fetch_organizations - @users = User.where(ou: @ou).order(cn: :asc).to_a + @pagy, @users = pagy(User.where(ou: @ou).order(cn: :asc)) @stats = { users_confirmed: User.where(ou: @ou).confirmed.count, diff --git a/app/views/admin/donations/index.html.erb b/app/views/admin/donations/index.html.erb index 1074843..f13e94b 100644 --- a/app/views/admin/donations/index.html.erb +++ b/app/views/admin/donations/index.html.erb @@ -21,7 +21,7 @@
<% if @donations.any? %>

Recent Donations

- +
@@ -52,6 +52,7 @@ <% end %>
User
+ <%== pagy_nav @pagy %> <% else %>

No donations yet. diff --git a/app/views/admin/invitations/index.html.erb b/app/views/admin/invitations/index.html.erb index 51905c0..19e51be 100644 --- a/app/views/admin/invitations/index.html.erb +++ b/app/views/admin/invitations/index.html.erb @@ -24,7 +24,7 @@ <% if @invitations_used.any? %>

Recently Accepted

- +
@@ -44,6 +44,7 @@ <% end %>
Token
+ <%== pagy_nav @pagy %>
<% end %> <% end %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 9e60ac7..e0eb90d 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -30,7 +30,7 @@ <% end %>
- +
@@ -49,5 +49,6 @@ <% end %>
UID
+ <%== pagy_nav @pagy %>
<% end %> From 251a170f2b02032d280beaff6af962bd74267d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 1 Mar 2023 17:14:44 +0800 Subject: [PATCH 4/4] Add documentation link for Pagy --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fe0bfb..b2e7205 100644 --- a/README.md +++ b/README.md @@ -81,12 +81,15 @@ with a fresh installation, delete both that directory as well as the container. ## Documentation +### Rails + * [Ruby on Rails](https://guides.rubyonrails.org/) -* [Sass](https://sass-lang.com/documentation) +* [Pagination](https://ddnexus.github.io/pagy/) ### Front-end * [Tailwind CSS](https://tailwindcss.com/) +* [Sass](https://sass-lang.com/documentation) ### Testing