I'm using an api which i can request a page and it returns the results for that page and the total number of pages that query returns.
How can I use kaminari or will_paginate so that they are aware not to insert a query for pagination (because it's through an api) but still display the page numbers and use the total pages returned by the api.
I know you can paginate a collection, but I don't want to use that because the raw query (without pagination takes a long time)
So instead of paginating an array of all the results I want to be able to paginate an object which will fetch additional page when the paginator requests this?
First, I'd like to restate what I think you're asking to make sure I understand-- please correct me if I don't have it right.
You are using an API that is not under your control, and you send it a request for a particular page of results. For example, you send a request to
http://some-service.com?page=3
and it sends back 10 results and also that the total number of pages available is 42.Then to the users of your application, you want to show the results and that they can ask for the previous and next pages, and also tell them how many total pages are available. When a user of your application clicks the
next
link, you want to do a request forhttp://some-service.com?page=4
, etc.I don't think you need kaminari or will-paginate for this-- it sounds like you can just pass through the parameters that your users send to your application to the API, and then you pass the information returned from the API through to your users.
For example, your users would visit a page like
http://your-application.com/results
. In your controller you would getparams[:page]
, which you would need to default to 1 when it wasn't present in the URL.Then you could do a request to
http://some-service.com/page=1
and save the total number of pages in a variable liketotal_pages_available
.In your view, you can check to see if params[:page] is greater than 1, in which case you should show a "previous" link that links to
http://your-application.com/results?page=#{params[:page] - 1}
. You can also check if params[:page] is less thantotal_pages_available
, in which case you should show a "next" link that links tohttp://your-application.com/results?page=#{params[:page] + 1}
.