Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paginate Rabl's collections

I have this template:

# app/views/posts/index.rabl
collection @posts => :posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

Witch returns:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ]
}

And I would like to add to add some pagination params in order to rendering this:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ],
    "total": 42,
    "total_pages": 12
}

Any ideas? Many thanks!

like image 797
Zag zag.. Avatar asked Feb 16 '12 15:02

Zag zag..


1 Answers

Sorry for my noob question, whitch was answered by the README. Here's an example of pagination:

object false

node(:total) {|m| @posts.total_count }
node(:total_pages) {|m| @posts.num_pages }

child(@posts) do
  extends "api/v1/posts/show"
end

Note: I'm using Kaminari for pagination.

like image 178
Zag zag.. Avatar answered Oct 26 '22 15:10

Zag zag..