Module: RestApiGenerator::Orderable

Extended by:
ActiveSupport::Concern
Included in:
ChildResourceController, ResourceController
Defined in:
lib/rest_api_generator/orderable.rb

Constant Summary collapse

SORT_ORDER =
{ " " => :asc, "+" => :asc, "-" => :desc }

Instance Method Summary collapse

Instance Method Details

#ordering_params(order_params) ⇒ Object

Returns params for order in active record order syntax GET /api/v1/transactions?sort=-amount

ordering_params(params) # => { amount: :desc }



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rest_api_generator/orderable.rb', line 16

def ordering_params(order_params)
  ordering = {}
  if order_params
    sorted_params = order_params.split(",")
    sorted_params.each do |attr|
      sort_sign = /\A[ +-]/.match?(attr) ? attr.slice!(0) : "+"
      if resource_class.attribute_names.include?(attr)
        ordering[attr] = SORT_ORDER[sort_sign]
      end
    end
  end
  ordering
end