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
This commit is contained in:
Jean-Baptiste Bellet
2023-01-25 11:08:36 +01:00
parent a97fb0f46b
commit e18454c55a
2 changed files with 15 additions and 4 deletions

View File

@@ -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

View File

@@ -38,3 +38,5 @@
%tr
%td{colspan: "4"}= t(:none)
= f.submit t(:update)
= render partial: 'admin/shared/pagy_links', locals: { pagy: @pagy }