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.



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

def _get_filter_params
  fields = @controller.get_filterset_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.



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

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

  return data
end