Class: RESTFramework::ModelSearchFilter
- Inherits:
-
BaseFilter
- Object
- BaseFilter
- RESTFramework::ModelSearchFilter
- Defined in:
- lib/rest_framework/filters.rb
Overview
Multi-field text searching on models.
Instance Method Summary collapse
-
#_get_fields ⇒ Object
Get a list of search fields for the current action.
-
#get_filtered_data(data) ⇒ Object
Filter 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 search fields for the current action. Fallback to columns because we need an explicit list of columns to search on, so ‘nil` is useless in this context.
94 95 96 |
# File 'lib/rest_framework/filters.rb', line 94 def _get_fields return @controller.class.search_fields || @controller.get_fields(fallback: true) end |
#get_filtered_data(data) ⇒ Object
Filter data according to the request query parameters.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rest_framework/filters.rb', line 99 def get_filtered_data(data) fields = self._get_fields search = @controller.request.query_parameters[@controller.class.search_query_param] # Ensure we use array conditions to prevent SQL injection. if search.present? && !fields.empty? return data.where( fields.map { |f| "CAST(#{f} AS VARCHAR) #{@controller.class.search_ilike ? "ILIKE" : "LIKE"} ?" }.join(" OR "), *(["%#{search}%"] * fields.length), ) end return data end |