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.
122 123 124 |
# File 'lib/rest_framework/filters.rb', line 122 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.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rest_framework/filters.rb', line 127 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 |