Module: JSONAPI::Utils::Support::Filter
- Included in:
- Response::Support
- Defined in:
- lib/jsonapi/utils/support/filter.rb
Instance Method Summary collapse
- #apply_filter(records, options = {}) ⇒ Object
- #apply_filter?(records, options = {}) ⇒ Boolean
- #filter_params ⇒ Object
Instance Method Details
#apply_filter(records, options = {}) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/jsonapi/utils/support/filter.rb', line 5 def apply_filter(records, = {}) if apply_filter?(records, ) records.where(filter_params) else records end end |
#apply_filter?(records, options = {}) ⇒ Boolean
13 14 15 16 |
# File 'lib/jsonapi/utils/support/filter.rb', line 13 def apply_filter?(records, = {}) params[:filter].present? && records.respond_to?(:where) && ([:filter].nil? || [:filter]) end |
#filter_params ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi/utils/support/filter.rb', line 18 def filter_params @_filter_params ||= if params[:filter].is_a?(Hash) params[:filter].keys.each_with_object({}) do |resource, hash| hash[resource] = params[:filter][resource] end end end |