From e18454c55aa9abd1aaa35abd5e6102c7ff4c81d7 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 25 Jan 2023 11:08:36 +0100 Subject: [PATCH] Add pagination on enterprises page for super admin Enterprises are stored in `@enterprise_set` variables, and we iterate over to show the list of enterprises to super admin. Previously, we used to use `Sets::EnterpriseSet.new(collection)` instead of creating set based on `@collection`: this leads to call the `collection` method twice, which was probably very time consuming. This commit fix also that. + use paginated enterprises loading on bulk update but without testing if the current user is an admin --- app/controllers/admin/enterprises_controller.rb | 17 +++++++++++++---- .../admin/enterprises/_admin_index.html.haml | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 42d4385695..a48657aaf9 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -8,12 +8,13 @@ module Admin class EnterprisesController < Admin::ResourceController include GeocodeEnterpriseAddress include CablecarResponses + include Pagy::Backend # These need to run before #load_resource so that @object is initialised with sanitised values prepend_before_action :override_owner, only: :create prepend_before_action :override_sells, only: :create - before_action :load_enterprise_set, only: :index + before_action :load_enterprise_set_on_index, only: :index before_action :load_countries, except: [:index, :register, :check_permalink] before_action :load_methods_and_fees, only: [:edit, :update] before_action :load_groups, only: [:new, :edit, :update, :create] @@ -100,7 +101,8 @@ module Admin end def bulk_update - @enterprise_set = Sets::EnterpriseSet.new(collection, bulk_params) + load_enterprise_set_with_params(bulk_params) + if @enterprise_set.save flash[:success] = I18n.t(:enterprise_bulk_update_success_notice) @@ -148,8 +150,15 @@ module Admin private - def load_enterprise_set - @enterprise_set = Sets::EnterpriseSet.new(collection) if spree_current_user.admin? + def load_enterprise_set_on_index + return unless spree_current_user.admin? + + load_enterprise_set_with_params + end + + def load_enterprise_set_with_params(params = {}) + @pagy, @paginated_collection = pagy(@collection) + @enterprise_set = Sets::EnterpriseSet.new(@paginated_collection, params) end def load_countries diff --git a/app/views/admin/enterprises/_admin_index.html.haml b/app/views/admin/enterprises/_admin_index.html.haml index f065a8c507..ad37a1200f 100644 --- a/app/views/admin/enterprises/_admin_index.html.haml +++ b/app/views/admin/enterprises/_admin_index.html.haml @@ -38,3 +38,5 @@ %tr %td{colspan: "4"}= t(:none) = f.submit t(:update) + + = render partial: 'admin/shared/pagy_links', locals: { pagy: @pagy }