Active_model_serializers: How do I change the default pagination params?

Created on 30 Jun 2016  路  11Comments  路  Source: rails-api/active_model_serializers

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

JSON API Question Ready for PR 0.10.x

Most helpful comment

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

ActiveModelSerializers.config.jsonapi_pagination_links_enabled = false

All 11 comments

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

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.

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

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

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

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.

@bf4 Thanks for your help. 馃憤

@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

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

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

ActiveModelSerializers.config.jsonapi_pagination_links_enabled = false
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mifrill picture Mifrill  路  4Comments

greggawatt picture greggawatt  路  4Comments

kapso picture kapso  路  4Comments

steverob picture steverob  路  4Comments

adamcrown picture adamcrown  路  4Comments