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_filter_paramsObject

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



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

def _get_filter_params
  fields = @controller.class.filterset_fields&.map(&:to_s) || @controller.send(:get_fields)
  return @controller.request.query_parameters.select { |p|
    fields.include?(p)
  }.to_h.symbolize_keys  # convert from HashWithIndifferentAccess to Hash w/keys
end

#get_filtered_data(data) ⇒ Object

Filter data according to the request query parameters.



24
25
26
27
28
29
30
31
# File 'lib/rest_framework/filters.rb', line 24

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

  return data
end