Collection ordering for grape API framework.
Add this line to your application's Gemfile:
gem 'grape-order'
And then execute:
$ bundle
Or install it yourself as:
$ gem install grape-order
class MyApi < Grape::API
# Include Grape::Sort module in your api
include Grape::Order
resource :comments do
desc 'Return a list of comments.'
# Annotate action with `order`.
# This will add an optional param: order
#
# You can optionally set the default :order setting to the fields of your choice.
#
order '-created_at'
get do
comments = Comment.where(...)
# Use `order` helper to order the collection by passed params.
order(comments)
end
end
end
Now you can pass a string order
param to HTTP request to your endpoint structured as follows:
- prepend field name by
-
to get ascending order, like-created_at
- pass multiple fields to order by separating them by comma, like
-created_at, name
- ordering on nested resources is also supported, eg: 'posts.created_at, -comments.created_at'
curl -v http://lvh.me:3000/comments?order=-created_at
The gem structure was based on https://github.com/monterail/grape-kaminari (which can be used for pagination).
Feel free to submit issue with bug report or pull requests on GitHub at https://github.com/PrositAS/grape-order.