-
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
Pagination Links #1041
Pagination Links #1041
Conversation
If you want pagination links in your response, specify it in the `render` | ||
|
||
```ruby | ||
render json: @posts, pagination: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this. Did you try subclassing the ArraySerializer
? e.g. https://github.com/rails-api/active_model_serializers#2-for-an-array-resource and just setting the meta? Have you seen pagination gems such as https://github.com/davidcelis/api-pagination ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! @bf4 about the meta thing, it would work, but it would not follow json-api conventions.
@bacarini I think the README might not be the best place for describing this feature, mostly because it's to a particular adapter (json-api) and also because would be awesome to have some examples.
We are trying to consolidate our new docs, would be great if instead of adding this to the README you could open a new article there with this PR describing the feature, some examples and some of the gems that ppl can use in order to make it work.
Hmm.. well, it should subclass the ArraySerializer for sure, but besides testing that we can set meta data in a collection, I'm not sure it's the job of this library to expose anything more than the meta option for any pagination libraries to use. |
My bad, it should be in a links object, not meta http://jsonapi.org/format/#fetching-pagination
"links": {
"self": "http://example.com/posts",
"first": "http://example.com/posts?page[number]=1",
} |
I'm happy to pair with you to work on the links object. (We'll need tests, too, and might want to start with a PR to the json-api/example page to make sure we're getting it right) |
great... So, I will work on it. Should we edit this page example or create another one? |
add more examples to it.. see e.g. json-api/json-api#828 (error objects) which is a result of me working on error objects here in AMS #1004 |
|
||
include_meta(hash) | ||
include_pagination_links(hash) if options && options[:pagination] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these changes should be in the json_api adapter, no/
Don't forget to add tests! They'll be required before this gets merged, so better now than later, right? |
Hey @bf4, I've changed a few things according to your comments. I will add more test later on. Meanwhile, it would be great some reviews on the new code. tks |
hash = serializable_hash(options) | ||
include_meta(hash) unless self.class == FlattenJson | ||
hash | ||
serializable_hash(options).tap do |hash| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joaomdmoura OT: Might it make more sense for the FlattenJson adapter to implement a no-op include_meta
so the superclass doesn't need to know about the subclass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 I couldn't agree more with you about this approach. I will be happy to work on it, but in a different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did it in #1048 because
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1048 Merged already.
Great work @bacarini. Btw awesome PR on json-api as well @bf4 !
👍 Indeed I see you added a test, would be great to have one covering the |
@joaomdmoura @bf4, I added test to pagination feature and also included it to I've exchanged I've also opened a PR on |
This is awesome! Thanks for the work on this @bacarini 💥 👍 😄 I would like to make a suggestion. Sometimes pagination is tied to a search or filtered query. Looking at the docs, the returned pagination links look like This might be up to individual interpretation but the way the pagination links are returned will require the client to take additional steps before it can use them (i.e. prefixing the api endpoint, adding filters or search query). I'm suggesting that pagination links should be returned in a way that no additional processing is needed by the client. I'm working on a project using JSON API that needed pagination links and this is similar to how I approached it: url = request.original_url.sub(/\?.*$/, "")
params = request.query_parameters.merge(page: { number: page, size: size }).to_param
"#{url}?#{params}" In my case, I'm using a
In any case, this is great. I like that this gives the ability to add a root |
According to a comment on json-api PR, I added complete URIs, not just query strings. It will accept @enriikke, I can see your point, but what do you think to discuss it on json-api PR? |
018b71c
to
9dbf7c3
Compare
@enriikke may bad, I misunderstood your previous comment. I added new code to merge with urls parameters that already exists. |
Awesome @bacarini I'm looking forward to this PR being merged 👍 😃 |
Pagination links will be included in your response automatically as long as the resource is paginated using Kaminari or WillPaginate and if you are using a JSON-API adapter. The others adapters does not have this feature.
@bf4 no worries! 😃 I`ve changed a few things, could you review? |
hash | ||
serializable_hash(options).tap do |hash| | ||
include_meta(hash) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go away when you rebase, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gone!
@joaomdmoura and @bf4 updated docs as suggested. |
assert_equal expected_links, response['links'] | ||
end | ||
|
||
def test_render_only_prev_and_first_pagination_links_with_additional_params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
``` | ||
|
||
AMS relies on either [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). Please install either dependency by adding one of those to your Gemfile. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: AMS relies on either Kaminari or WillPaginate for pagination.
alternative to consider: AMS pagination relies on a paginated collection with the methods current_page
, total_pages
, and size
, such as are supported by both Kaminari or WillPaginate.
(links left out for ease of me typing :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@joaomdmoura looks good to me. The interface is solid and the tests cover the usage well. I'd like to iterate on it a bit, but we can do that in another PR. @bacarini Has put in a lot of work and it will be the basis for more like it, I am sure! 👍 all around! |
I don't think it necessarily needs to be rebased since I think the commits tell a good story. |
After this is merged, I think #1018 will need to be repurposed to handle the non-pagination link scenarios and how they may interact with pagination links. |
@bf4 Hi. When are you going to merge this PR? |
Awesome work @bacarini . I'me merging this. |
@kalys I don't have commit :) Thanks @joaomdmoura for stewarding this and @bacarini for all the work! |
That is great!!! I am glad we could make it through. 😃 |
Great work @bacarini 🍻 |
cheers mate 🍻 😄 |
I would like to discuss on how we should display pagination links on AMS.
I think you can relie on
kaminari
andwill_paginate
gems to do so. Having said that, I created a class to build links.related issue