Add resource total count to pagination meta#21
Add resource total count to pagination meta#21travismiller wants to merge 1 commit intostas:masterfrom
Conversation
|
@travismiller thanks for the PR. To be honest, I'm not 100% convinced we should add it where you're suggesting. There are a couple of ways you already can achieve this just by using the API provided by the library... Could you share a bit more context what are you trying to achieve please? |
|
@stas thanks for responding! I very well may have misunderstood how this works. total may not have been the best identifier either. My goal was to be able to determine the total count of resources in the query. How might we do this appropriately? jsonapi.rb/lib/jsonapi/pagination.rb Lines 65 to 71 in 6085c8c If the the page_size is say The current meta may include:
|
|
@travismiller you can use the same def jsonapi_meta(resources)
pagination = jsonapi_pagination_meta(resources)
{ pagination: pagination, total: resources.size } if pagination.present?
end |
|
Of course! 🤦♂️ Thank you @stas for the pointer. |
@stas upon implementing this suggestion, I see that Our current working implementation looks like this, where we’ve re-implemented the total calculation from inside the library. def jsonapi_meta(resources)
meta = {}
pagination = jsonapi_pagination_meta(resources)
if pagination.present?
meta[:pagination] = pagination
meta[:page_size] = jsonapi_pagination_params.second
meta[:resource_count] = resource_counter(resources)
end
meta
end
def resource_counter(resources)
# https://github.com/stas/jsonapi.rb/blob/v1.5.6/lib/jsonapi/pagination.rb#L65-L71
if resources.respond_to?(:unscope)
resources.unscope(:limit, :offset).count
else
resources.instance_variable_get(:@original_size) || resources.size
end
end |
What is the current behavior?
Currently it’s not possible to determine the total number of resources within paginated results.
What is the new behavior?
Since the total is already calculated, it’s a small change to go ahead and include it.
Checklist
Please make sure the following requirements are complete:
features)