Class: RESTFramework::ModelOrderingFilter
- Inherits:
-
BaseFilter
- Object
- BaseFilter
- RESTFramework::ModelOrderingFilter
- Defined in:
- lib/rest_framework/filters.rb
Overview
A filter backend which handles ordering of the recordset.
Instance Method Summary collapse
-
#_get_ordering ⇒ Object
Convert ordering string to an ordering configuration.
-
#get_filtered_data(data) ⇒ Object
Order data according to the request query parameters.
Methods inherited from BaseFilter
Constructor Details
This class inherits a constructor from RESTFramework::BaseFilter
Instance Method Details
#_get_ordering ⇒ Object
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 62 63 |
# File 'lib/rest_framework/filters.rb', line 38 def _get_ordering return nil if @controller.class.ordering_query_param.blank? # Ensure ordering_fields are strings since the split param will be strings. ordering_fields = @controller.get_ordering_fields&.map(&:to_s) order_string = @controller.params[@controller.class.ordering_query_param] unless order_string.blank? ordering = {}.with_indifferent_access order_string.split(",").each do |field| if field[0] == "-" column = field[1..-1] direction = :desc else column = field direction = :asc end if !ordering_fields || column.in?(ordering_fields) ordering[column] = direction end end return ordering end return nil end |
#get_filtered_data(data) ⇒ Object
Order data according to the request query parameters.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rest_framework/filters.rb', line 66 def get_filtered_data(data) ordering = self._get_ordering reorder = !@controller.class.ordering_no_reorder if ordering && !ordering.empty? return data.send(reorder ? :reorder : :order, _get_ordering) end return data end |