Recent versions of ActiveResource allow you to parse HTTP responses (without hacks).
For those unfamiliar, what is ActiveResource?
ActiveResource is a gem extracted from Rails. It is built on a standard JSON or XML format for requesting and submitting resources over HTTP.
The basic documentation is very well explained and can help you get started fairly fast. Unfortunately, once you begin to grow your application (and depend on ARes), you’ll notice that some simple tasks can quickly become rather cumbersome to figure out. Pagination is one of the first bumps I hit.
The solution
The first way to implement pagination, and my preferred choice, is to send pagination data along HTTP headers. This approach sends a clean response and follows API best practices. The downside to it is that it can be complex to implement.
Example with a Rails API and the will_paginate gem
1 2 3 4 5 6 7 8 9 | |
Nothing new here, the paginate method is provided by the will_paginate gem. However, note the set_pagination_headers method, which is defined in the ApiController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
See, limit_value, offset and total_count are provided by will_paginate, and I chose to send these values because I use the Kaminari gem with ActiveResource in my client. Kaminari has an extension for paginatable arrays, which requires precisely these variables to figure out pagination logic. The set up is done once in an after_filter, and we embed this data into our response headers.
Note: The best practice for custom HTTP headers was to prefix them with “X-“, but that approach has been deprecated recently.
That’s all for our API. Now we turn to the client.
ActiveResource 4.0.0.beta1 introduces ActiveResource::Collection, which (according to the documentation in the source code) is a wrapper to handle parsing index responses. A Post class can be set up to handle it with:
1 2 3 4 5 6 | |
The PaginatedCollection class will take the parsed elements. Here we can access the connection object, which holds the response headers we set in our API.
Remember: This class inherits from ActiveResource::Collection and there are certain methods we can override to customise our collection object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
Note: You must install the Kaminari gem for this to work.
Now your ActiveResource collections can handle pagination:
1 2 3 4 5 6 7 | |
And your views too (thanks to Kaminari):
1 2 3 4 5 6 7 8 | |
Other options
Another alternative is to send pagination data inside the body of the response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
You will have to make some modifications to the code above in order for this to work.
Final words
I encourage you to try the first approach. It embraces the principles of flexibility, standardization and loose coupling.
Final notes
- You can use either
will_paginateorKaminari, or both (like in this example), or even another solution. As long as you can create custom paginatable arrays. I chose Kaminari for the client because it was easier and I read it handles arrays pretty well. - Solution built with ideas from this blog post and the active_resource source code
ActiveResourcemight not have the greatestREADME, but you can find everything you need in the comments inside the source code, and in the tests, so make sure to take a look there.