title: Rails Grape API pagination with AMS and custom JSON
body: <p>Doing in Rails what may require a more complex/creative solution, but basically using Grape, active_model_serializers (AMS), and grape-kaminari to return an index route (list) of User objects, and I'd like to be able to use the advantages of AMS with caching and easily serializing objects, but customize the JSON response.</p>
  
  <p>Here's Grape:</p>
  
  <pre><code>module API
    module V1
      class Users &lt; Grape::API
        include API::V1::Defaults
        include Grape::Kaminari
  
        resource :users do
           paginate per_page: 18
           get do
             users = User.all
             paginate users
           end
        end
      end
    end
  end
  </code></pre>
  
  <p>And AMS:</p>
  
  <pre><code>class UserSerializer &lt; ActiveModel::Serializer
    cache key: 'user', expires_in: 3.hours
    attributes :id, :firstname, :lastname, :email, :username
  end
  </code></pre>
  
  <p>Ideal Response in JSON:</p>
  
  <pre><code>{
    users: [
      {
        id: 1,
        firstname: "Daniel",
        lastname: "Archer,
        email: "daniel@archer.co",
        username: "dja"
       },
      {
        id: 2,
        firstname: "Fakey",
        lastname: "McFakerson,
        email: "fake@fake.io",
        username: "fake"
       }
    ],
    extras: {
      user_count: 300,
      new_users_last_5_days: 10,
      new_users_last_30_days: 25
    }
  }
  </code></pre>
  
  <p>What I'd like to happen (and having trouble with) is to paginate the amount of users in the JSON response, but include some extras that are related to the entire collection of Users. For example, I may have 300 Users, but I only want to return the first 18 users (or however many based on params <code>per_page</code> and <code>page</code>), and extra data on the entire collection.</p>