Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I change the default pagination params? #1823

Closed
ekampp opened this issue Jun 30, 2016 · 11 comments
Closed

How do I change the default pagination params? #1823

ekampp opened this issue Jun 30, 2016 · 11 comments

Comments

@ekampp
Copy link

ekampp commented Jun 30, 2016

Expected behavior vs actual behavior

JSON API states that "[...] For example, a page-based strategy might use query parameters such as page[number] and page[size] [...]"

So I would expect some way of configuring this. I cannot find that place. Am I missing something, or is this just not a config option?

Steps to reproduce

N/A

Environment

AMS 0.10.1
Rails 5.1 (master branch)

Backtrace

N/A

Additonal helpful information

Nil

@bf4
Copy link
Member

bf4 commented Jul 5, 2016

There's a couple of ways, but basically you probably want to look at https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md#how-to-add-pagination-links and report back here if that resolves your question or any followup questions

@ekampp
Copy link
Author

ekampp commented Jul 5, 2016

I have looked there. It doesn't supply any immediate option for changing the resulting JSON from page[number] to e.g. page, and page[size] to per_page.

As quoted the JSON API standard doesn't have any preference. So I would like to move away from the page hash and into a more simple, flat pagination parameter structure in my project. I would have expected this to be both possible and simple. This doesn't seem to be the case.

@bf4
Copy link
Member

bf4 commented Jul 5, 2016

Maybe take a look at #1549 (comment) and #1596 (comment) ?

@ekampp
Copy link
Author

ekampp commented Jul 6, 2016

Both of those relates to disabling pagination. I'm not interested in that. I'm interested in hot to configuring the page[number] to just page, and the page[size] to per_page, but keep pagination in place

@bf4
Copy link
Member

bf4 commented Jul 6, 2016

@ekampp yeah, you need to disable the convention and then write it yourself. I'm not sure what you were expecting.

@bf4
Copy link
Member

bf4 commented Jul 6, 2016

@ekampp If you want to modify the PaginationLinks builder in a PR so that it can be customized, you are welcome to, but we already let you build what you want.

@ekampp
Copy link
Author

ekampp commented Jul 6, 2016

Since the standard specifically states it's agnostic I did expect a configurable option for the names. But I'll figure it some other way, then.

@ekampp
Copy link
Author

ekampp commented Jul 6, 2016

@bf4 Thanks for your help. 👍

@bf4
Copy link
Member

bf4 commented Jul 6, 2016

@ekampp Yeah, we generally implement the recommendations when possible, but not require them. In some cases, such as this, we weren't as careful in making it easy to opt out of the recommended pagination

@saneshark
Copy link

saneshark commented Jul 18, 2016

Just added this to my initializer and it seemed to solve the problem.

# This module monkey patches the pagination links defined by the json-api adapter_options
# to more closely match the specifications
module CustomPaginationLinks
  FIRST_PAGE = 1

  def as_json
    per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
    pagination_links = pages_from.each_with_object({}) do |(key, value), hash|
      # Use the non nested syntax for pagination params
      params = query_parameters.merge(page: value, per_page: per_page).to_query
      # Changed this to set the value to nil when no value is specified by pages_from
      hash[key] = value.present? ? "#{url(adapter_options)}?#{params}" : nil
    end
    # Always include self, regardless of pagination links existing or not.
    { self: "#{url(adapter_options)}?#{query_parameters.to_query}" }.merge(pagination_links)
  end

  private

  # Changed these to allow nil values, this way the keys are always present, but possibly null
  def pages_from
    #return {} if collection.total_pages <= FIRST_PAGE
    {}.tap do |pages|
      pages[:first] = FIRST_PAGE
      pages[:prev] = first_page? ? nil : collection.current_page - FIRST_PAGE
      pages[:next] = last_page? ? nil : collection.current_page + FIRST_PAGE
      pages[:last] = collection.total_pages
    end
  end

  def first_page?
    collection.current_page == FIRST_PAGE
  end

  def last_page?
    collection.current_page == collection.total_pages
  end
end

ActiveModelSerializers::Adapter::JsonApi::PaginationLinks.prepend CustomPaginationLinks
ActiveModelSerializers.config.adapter = :json_api

@richmolj
Copy link
Contributor

richmolj commented Sep 6, 2016

@ekampp @saneshark on master, you can now do:

ActiveModelSerializers.config.jsonapi_pagination_links_enabled = false

@richmolj richmolj closed this as completed Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants