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
54
55
56
57
58
59
60
61
# File 'lib/rest_framework/filters.rb', line 38

def _get_ordering
  return nil if @controller.class.ordering_query_param.blank?
  ordering_fields = @controller.send(:get_ordering_fields)
  order_string = @controller.params[@controller.class.ordering_query_param]

  unless order_string.blank?
    ordering = {}
    order_string.split(',').each do |field|
      if field[0] == '-'
        column = field[1..-1]
        direction = :desc
      else
        column = field
        direction = :asc
      end
      if column.in?(ordering_fields)
        ordering[column.to_sym] = direction
      end
    end
    return ordering
  end

  return nil
end

#get_filtered_data(data) ⇒ Object

Order data according to the request query parameters.



64
65
66
67
68
69
70
71
72
73
# File 'lib/rest_framework/filters.rb', line 64

def get_filtered_data(data)
  ordering = self._get_ordering
  reorder = !@controller.send(:ordering_no_reorder)

  if ordering && !ordering.empty?
    return data.send(reorder ? :reorder : :order, _get_ordering)
  end

  return data
end