mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Simplify cases where pagination is conditional and pagination data may or may not be used
This commit is contained in:
@@ -15,12 +15,11 @@ module Admin
|
||||
ransack(params[:q]).result.
|
||||
reorder('spree_line_items.order_id ASC, spree_line_items.id ASC')
|
||||
|
||||
pagy = nil
|
||||
pagy, @line_items = pagy(@line_items) if pagination_required?
|
||||
@pagy, @line_items = pagy(@line_items) if pagination_required?
|
||||
|
||||
render json: {
|
||||
line_items: serialized_line_items,
|
||||
pagination: pagination_data(pagy)
|
||||
pagination: pagination_data
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -73,8 +73,9 @@ module Api
|
||||
end
|
||||
|
||||
def render_paginated_products
|
||||
pagy, results = nil, products
|
||||
pagy, results = pagy(results, items: params[:per_page] || DEFAULT_PER_PAGE) if pagination_required?
|
||||
results = products
|
||||
|
||||
@pagy, results = pagy(results, items: params[:per_page] || DEFAULT_PER_PAGE) if pagination_required?
|
||||
|
||||
serialized_products = ActiveModel::ArraySerializer.new(
|
||||
results,
|
||||
@@ -84,7 +85,7 @@ module Api
|
||||
|
||||
render json: {
|
||||
products: serialized_products,
|
||||
pagination: pagination_data(pagy)
|
||||
pagination: pagination_data
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ module Api
|
||||
|
||||
orders = SearchOrders.new(params, current_api_user).orders
|
||||
|
||||
pagy, paged_orders = pagy(orders, items: params[:per_page] || default_per_page)
|
||||
@pagy, paged_orders = pagy(orders, items: params[:per_page] || default_per_page)
|
||||
|
||||
render json: {
|
||||
orders: serialized_orders(paged_orders),
|
||||
pagination: pagination_data(pagy)
|
||||
pagination: pagination_data
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ module Api
|
||||
end
|
||||
|
||||
def render_paged_products(products, product_serializer = ::Api::Admin::ProductSerializer)
|
||||
pagy, products = pagy(products, items: params[:per_page] || DEFAULT_PER_PAGE)
|
||||
@pagy, products = pagy(products, items: params[:per_page] || DEFAULT_PER_PAGE)
|
||||
|
||||
serialized_products = ActiveModel::ArraySerializer.new(
|
||||
products,
|
||||
@@ -141,7 +141,7 @@ module Api
|
||||
|
||||
render json: {
|
||||
products: serialized_products,
|
||||
pagination: pagination_data(pagy)
|
||||
pagination: pagination_data
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
module PaginationData
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def pagination_data(pagination)
|
||||
return unless pagination.respond_to? :pages
|
||||
def pagination_data
|
||||
return unless defined? @pagy
|
||||
|
||||
{
|
||||
results: pagination.count,
|
||||
pages: pagination.pages,
|
||||
results: @pagy.count,
|
||||
pages: @pagy.pages,
|
||||
page: (params[:page] || 1).to_i,
|
||||
per_page: (params[:per_page] || default_per_page).to_i
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user