Module: JSONAPI::Utils::Support::Filter::Default
- Included in:
- Response::Support
- Defined in:
- lib/jsonapi/utils/support/filter/default.rb
Instance Method Summary collapse
-
#apply_filter(records, options = {}) ⇒ ActiveRecord::Relation, Array
Apply default equality filters.
-
#apply_filter?(records, options = {}) ⇒ Boolean
Check whether default filters should be applied.
-
#filter_params ⇒ Hash, NilClass
Build a Hash with the default filters.
Instance Method Details
#apply_filter(records, options = {}) ⇒ ActiveRecord::Relation, Array
Apply default equality filters.
e.g.: User.where(name: 'Foobar')
15 16 17 18 19 20 21 |
# File 'lib/jsonapi/utils/support/filter/default.rb', line 15 def apply_filter(records, = {}) if apply_filter?(records, ) records.where(filter_params) else records end end |
#apply_filter?(records, options = {}) ⇒ Boolean
Check whether default filters should be applied.
34 35 36 37 |
# File 'lib/jsonapi/utils/support/filter/default.rb', line 34 def apply_filter?(records, = {}) params[:filter].present? && records.respond_to?(:where) && ([:filter].nil? || [:filter]) end |
#filter_params ⇒ Hash, NilClass
Build a Hash with the default filters.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jsonapi/utils/support/filter/default.rb', line 44 def filter_params @_filter_params ||= case params[:filter] when Hash, ActionController::Parameters default_filters.each_with_object({}) do |field, hash| unformatted_field = @request.unformat_key(field) hash[unformatted_field] = params[:filter][field] end end end |