From 9c9459211fa48abc6f982be0e5215d4cfe2e2b9c Mon Sep 17 00:00:00 2001 From: Jean Moreau Date: Fri, 8 Mar 2024 15:11:58 -0500 Subject: [PATCH] Implement basic transaction pagination (#531) * install pagy * add pagy to controller, display default pagy UI * display hardcoded custom UI to confirm styling * implement custom UI with pagy methods * move pagination into partial * use lucide icons * only display pagination if 2 or more pages are available * add mobile pagination placeholder * use link_to and display greyed out buttons when no prev or next needed * sort transactions by date so grouping works appropriately with pagination * add space between mobile view buttons * remove debugging --- Gemfile | 1 + Gemfile.lock | 2 + app/controllers/application_controller.rb | 1 + app/controllers/transactions_controller.rb | 2 +- app/helpers/application_helper.rb | 2 + app/views/transactions/_pagination.html.erb | 57 +++++++++++++++++++++ app/views/transactions/index.html.erb | 5 ++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 app/views/transactions/_pagination.html.erb diff --git a/Gemfile b/Gemfile index 7b6dbf07..0192f367 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem "bcrypt", "~> 3.1.7" gem "inline_svg" gem "tzinfo-data", platforms: %i[ windows jruby ] gem "faraday" +gem "pagy" group :development, :test do gem "debug", platforms: %i[ mri windows ] diff --git a/Gemfile.lock b/Gemfile.lock index ca1b2cb0..411ab898 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -239,6 +239,7 @@ GEM racc (~> 1.4) nokogiri (1.16.2-x86_64-linux) racc (~> 1.4) + pagy (7.0.10) parallel (1.24.0) parser (3.3.0.5) ast (~> 2.4.1) @@ -403,6 +404,7 @@ DEPENDENCIES inline_svg letter_opener lucide-rails! + pagy pg (~> 1.5) propshaft puma (>= 5.0) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9d2e1869..8166a181 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base include Authentication + include Pagy::Backend default_form_builder ApplicationFormBuilder diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index abc05a0b..eef0ca74 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -3,7 +3,7 @@ class TransactionsController < ApplicationController before_action :set_transaction, only: %i[ show edit update destroy ] def index - @transactions = Current.family.transactions + @pagy, @transactions = pagy(Current.family.transactions.order(date: :desc), items: 50) end def show diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3c755882..ed191035 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,6 @@ module ApplicationHelper + include Pagy::Frontend + def title(page_title) content_for(:title) { page_title } end diff --git a/app/views/transactions/_pagination.html.erb b/app/views/transactions/_pagination.html.erb new file mode 100644 index 00000000..2e76362a --- /dev/null +++ b/app/views/transactions/_pagination.html.erb @@ -0,0 +1,57 @@ + +
+ <% if pagy.prev %> + <%= link_to 'Previous', pagy_url_for(pagy, pagy.prev), class: "relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-500 hover:bg-gray-50" %> + <% else %> +
Previous
+ <% end %> + + <% if pagy.next %> + <%= link_to 'Next', pagy_url_for(pagy, pagy.next), class: "relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-500 hover:bg-gray-50" %> + <% else %> +
Next
+ <% end %> +
+ + + + + diff --git a/app/views/transactions/index.html.erb b/app/views/transactions/index.html.erb index f67b0511..2ecaf472 100644 --- a/app/views/transactions/index.html.erb +++ b/app/views/transactions/index.html.erb @@ -70,6 +70,11 @@ <%= render partial: "transactions/transaction_group", locals: { date: date, transactions: grouped_transactions } %> <% end %> + <% if @pagy.pages > 1 %> + + <% end %> <% end %>