Class: RESTFramework::ModelFilter

Inherits:
BaseFilter show all
Defined in:
lib/rest_framework/filters.rb

Overview

A simple filtering backend that supports filtering a recordset based on fields defined on the controller class.

Instance Method Summary collapse

Methods inherited from BaseFilter

#initialize

Constructor Details

This class inherits a constructor from RESTFramework::BaseFilter

Instance Method Details

#_get_fieldsObject

Get a list of filterset fields for the current action. Fallback to columns because we don’t want to try filtering by any query parameter because that could clash with other query parameters.



16
17
18
# File 'lib/rest_framework/filters.rb', line 16

def _get_fields
  return @controller.class.filterset_fields || @controller.get_fields(fallback: true)
end

#_get_filter_paramsObject

Filter params for keys allowed by the current action’s filterset_fields/fields config.



21
22
23
24
25
26
27
28
# File 'lib/rest_framework/filters.rb', line 21

def _get_filter_params
  # Map filterset fields to strings because query parameter keys are strings.
  if fields = self._get_fields.map(&:to_s)
    return @controller.request.query_parameters.select { |p, _| fields.include?(p) }
  end

  return @controller.request.query_parameters.to_h
end

#get_filtered_data(data) ⇒ Object

Filter data according to the request query parameters.



31
32
33
34
35
36
37
38
# File 'lib/rest_framework/filters.rb', line 31

def get_filtered_data(data)
  filter_params = self._get_filter_params.symbolize_keys
  unless filter_params.blank?
    return data.where(**filter_params)
  end

  return data
end