Module: APIHelper::Sortable

Extended by:
ActiveSupport::Concern
Defined in:
lib/api_helper/sortable.rb

Overview

Sortable

A Sortable Resource API gives the flexibility to change how the returned data is sorted to the client. Clients can use the sort URL parameter to control how the returned data is sorted, as this example:

GET /posts?sort=-created_at,title

This means to sort the data by its created time descended and then the title ascended.

Usage

Include this Concern in your Action Controller:

SamplesController < ApplicationController
  include APIHelper::Sortable
end

or in your Grape API class:

class SampleAPI < Grape::API
  helpers APIHelper::Sortable
end

then use the sortable method like this:

resources :posts do
  get do
    sortable default_order: { created_at: :desc }
    @posts = Post.order(sortable_sort)

    # ...
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort_param_desc(example: nil, default: nil) ⇒ Object

Return the ‘sort’ param description



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/api_helper/sortable.rb', line 79

def self.sort_param_desc(example: nil, default: nil)
  if default.present?
    desc = "Specify how the returning data should be sorted, defaults to '#{default}'."
  else
    desc = "Specify how the returning data should be sorted."
  end
  if example.present?
    "#{desc} Example value: '#{example}'"
  else
    desc
  end
end

Instance Method Details

#sortable(default_order: {}) ⇒ Object

Gets the sort parameter with the format ‘resourses?sort=-created_at,name’, verify and converts it into an safe Hash that can be passed into the .order method.

Params:

default_order

Hash the default value to return if the sort parameter is not provided



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/api_helper/sortable.rb', line 51

def sortable(default_order: {})
  # get the parameter
  sort_by = params[:sort] || params[:sort_by]

  if sort_by.is_a?(String)
    # split it
    sort_by_attrs = sort_by.gsub(/[^a-zA-Z0-9\-_,]/, '').split(',')

    # save it
    @sortable_sort = {}
    sort_by_attrs.each do |attrb|
      if attrb.match(/^-/)
        @sortable_sort[attrb.gsub(/^-/, '')] = :desc
      else
        @sortable_sort[attrb] = :asc
      end
    end
  else
    @sortable_sort = default_order
  end
end

#sortable_sortObject

Helper to get the sort data



74
75
76
# File 'lib/api_helper/sortable.rb', line 74

def sortable_sort
  @sortable_sort
end