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.
Here's Grape:
module API
module V1
class Users < 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
And AMS:
class UserSerializer < ActiveModel::Serializer
cache key: 'user', expires_in: 3.hours
attributes :id, :firstname, :lastname, :email, :username
end
Ideal Response in JSON:
{
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
}
}
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 per_page
and page
), and extra data on the entire collection.