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.
67 68 69 |
# File 'lib/rest_framework/filters.rb', line 67 def _get_fields return @controller.class.ordering_fields&.map(&:to_s) || @controller.get_fields end |
#_get_ordering ⇒ Object
Convert ordering string to an ordering configuration.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rest_framework/filters.rb', line 72 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 if !column.in?(fields) && column.split(".").first.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.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rest_framework/filters.rb', line 101 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 |