-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
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 As quoted the JSON API standard doesn't have any preference. So I would like to move away from the |
Maybe take a look at #1549 (comment) and #1596 (comment) ? |
Both of those relates to disabling pagination. I'm not interested in that. I'm interested in hot to configuring the |
@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 |
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
The text was updated successfully, but these errors were encountered: