From 3213c30a37a5080d9fddb17d777ee579266eb3eb Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 28 Jun 2021 21:08:20 +0100 Subject: [PATCH] Update pagination in Api::ExchangeProductsController --- .../api/v0/exchange_products_controller.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v0/exchange_products_controller.rb b/app/controllers/api/v0/exchange_products_controller.rb index fde6abf174..ed2fe33986 100644 --- a/app/controllers/api/v0/exchange_products_controller.rb +++ b/app/controllers/api/v0/exchange_products_controller.rb @@ -27,7 +27,7 @@ module Api render_variant_count && return if params[:action_name] == "variant_count" - render_paginated_products paginated_products + render_paginated_products end private @@ -51,14 +51,6 @@ module Api new(@order_cycle, spree_current_user) end - def paginated_products - return products unless pagination_required? - - products. - page(params[:page]). - per(params[:per_page] || DEFAULT_PER_PAGE) - end - def load_data_from_exchange exchange = Exchange.find_by(id: exchange_params[:exchange_id]) @@ -80,16 +72,19 @@ module Api end end - def render_paginated_products(paginated_products) + def render_paginated_products + pagy, results = nil, products + pagy, results = pagy(results, items: params[:per_page] || DEFAULT_PER_PAGE) if pagination_required? + serialized_products = ActiveModel::ArraySerializer.new( - paginated_products, + results, each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer, order_cycle: @order_cycle ) render json: { products: serialized_products, - pagination: pagination_data(paginated_products) + pagination: pagination_data(pagy) } end