mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Sometimes the objects are not paginated. In this case we need to avoid trying to render pagination data, as it will throw an error. This guard clause also means we can remove messy conditionals from several controllers.
27 lines
572 B
Ruby
27 lines
572 B
Ruby
# frozen_string_literal: true
|
|
|
|
module PaginationData
|
|
extend ActiveSupport::Concern
|
|
|
|
def pagination_data(objects)
|
|
return unless objects.respond_to? :total_count
|
|
|
|
{
|
|
results: objects.total_count,
|
|
pages: objects.total_pages,
|
|
page: (params[:page] || 1).to_i,
|
|
per_page: (params[:per_page] || default_per_page).to_i
|
|
}
|
|
end
|
|
|
|
def pagination_required?
|
|
params[:page].present? || params[:per_page].present?
|
|
end
|
|
|
|
def default_per_page
|
|
return unless defined? self.class::DEFAULT_PER_PAGE
|
|
|
|
self.class::DEFAULT_PER_PAGE
|
|
end
|
|
end
|