Class: RESTFramework::ModelOrderingFilter

Inherits:
BaseFilter
  • Object
show all
Defined in:
lib/rest_framework/filters.rb

Overview

A filter backend which handles ordering of the recordset.

Instance Method Summary collapse

Methods inherited from BaseFilter

#initialize

Constructor Details

This class inherits a constructor from RESTFramework::BaseFilter

Instance Method Details

#_get_orderingObject

Convert ordering string to an ordering configuration.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rest_framework/filters.rb', line 38

def _get_ordering
  return nil unless @controller.class.ordering_query_param

  order_string = @controller.params[@controller.class.ordering_query_param]
  unless order_string.blank?
    return order_string.split(',').map { |field|
      if field[0] == '-'
        [field[1..-1].to_sym, :desc]
      else
        [field.to_sym, :asc]
      end
    }.to_h
  end

  return nil
end

#get_filtered_data(data) ⇒ Object

Order data according to the request query parameters.



56
57
58
59
60
61
62
# File 'lib/rest_framework/filters.rb', line 56

def get_filtered_data(data)
  ordering = self._get_ordering
  if ordering && !ordering.empty?
    return data.order(_get_ordering)
  end
  return data
end