Grape::Order [Gem Version]Code ClimateBuild Status

Collection ordering for grape API framework.

Installation

Add this line to your application's Gemfile:

gem 'grape-order'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape-order

Usage

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

Contributing

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.