Skip to content

Add resource total count to pagination meta#21

Closed
travismiller wants to merge 1 commit intostas:masterfrom
travismiller:include-total-in-pagination
Closed

Add resource total count to pagination meta#21
travismiller wants to merge 1 commit intostas:masterfrom
travismiller:include-total-in-pagination

Conversation

@travismiller
Copy link

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:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes /
    features)
  • All automated checks pass (CI/CD)

@stas
Copy link
Owner

stas commented Mar 9, 2020

@travismiller thanks for the PR.

To be honest, I'm not 100% convinced we should add it where you're suggesting.
Mainly because jsonapi_pagination_meta generates pagination numbers and not collection size numbers. This way, including total would suggest the total number of pages, not records!!!

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?

@travismiller
Copy link
Author

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

if resources.respond_to?(:unscope)
total = resources.unscope(:limit, :offset).count()
else
# Try to fetch the cached size first
total = resources.instance_variable_get(:@original_size)
total ||= resources.size
end

If the the page_size is say 10, and there are 32 resources in the query result, I was hoping to be able to expose 32 in the meta.

The current meta may include: current, (prev, first), (next, last).

current first prev next last total
1 2 4 32
2 1 1 3 4 32
3 1 2 4 4 32
4 1 3 32

@stas
Copy link
Owner

stas commented Mar 10, 2020

@travismiller you can use the same jsonapi_meta API to achieve this. Consider this:

  def jsonapi_meta(resources)
    pagination = jsonapi_pagination_meta(resources)

    { pagination: pagination, total: resources.size } if pagination.present?
  end

@travismiller
Copy link
Author

Of course! 🤦‍♂️

Thank you @stas for the pointer.

@travismiller
Copy link
Author

@travismiller you can use the same jsonapi_meta API to achieve this. Consider this:

  def jsonapi_meta(resources)
    pagination = jsonapi_pagination_meta(resources)

    { pagination: pagination, total: resources.size } if pagination.present?
  end

@stas upon implementing this suggestion, I see that resources.size is the total number of resources in the current page and not the total across all pages.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants