Sha256: b80615f3ae68ff9fcefc118c4c5593c47d166fbdc1bc8efd625911d50bfca11d

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# A filter backend which handles ordering of the recordset.
class RESTFramework::Filters::OrderingFilter < RESTFramework::Filters::BaseFilter
  # Get a list of ordering fields for the current action.
  def _get_fields
    return @controller.class.ordering_fields&.map(&:to_s) || @controller.get_fields
  end

  # Convert ordering string to an ordering configuration.
  def _get_ordering
    return nil unless param = @controller.class.ordering_query_param.presence

    # Ensure ordering_fields are strings since the split param will be strings.
    fields = self._get_fields
    order_string = @controller.params[param]

    if order_string.present?
      ordering = {}.with_indifferent_access

      order_string = order_string.join(",") if order_string.is_a?(Array)
      order_string.split(",").map(&:strip).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

  # Order data according to the request query parameters.
  def filter_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
end

# Alias for convenience.
RESTFramework::OrderingFilter = RESTFramework::Filters::OrderingFilter

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest_framework-1.0.0.beta2 lib/rest_framework/filters/ordering_filter.rb
rest_framework-1.0.0.beta1 lib/rest_framework/filters/ordering_filter.rb
rest_framework-0.11.0 lib/rest_framework/filters/ordering_filter.rb