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_fields ⇒ Object
Get a list of ordering fields for the current action.
-
#_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_fields ⇒ Object
Get a list of ordering fields for the current action. Do not fallback to columns in case the user wants to order by a virtual column.
71 72 73 74 75 |
# File 'lib/rest_framework/filters.rb', line 71 def _get_fields return @_get_fields ||= ( @controller.class.ordering_fields || @controller.get_fields )&.map(&:to_s) end |
#_get_ordering ⇒ Object
Convert ordering string to an ordering configuration.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rest_framework/filters.rb', line 78 def _get_ordering return nil if @controller.class.ordering_query_param.blank? # Ensure ordering_fields are strings since the split param will be strings. fields = self._get_fields order_string = @controller.params[@controller.class.ordering_query_param] if order_string.present? 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 next unless !fields || column.in?(fields) ordering[column] = direction end return ordering end return nil end |
#get_filtered_data(data) ⇒ Object
Order data according to the request query parameters.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rest_framework/filters.rb', line 106 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, ordering) end return data end |