Module: JSONAPI::Utils::Support::Filter

Included in:
Response::Support
Defined in:
lib/jsonapi/utils/support/filter.rb

Instance Method Summary collapse

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, options = {})
  if apply_filter?(records, options)
    records.where(filter_params)
  else
    records
  end
end

#apply_filter?(records, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/jsonapi/utils/support/filter.rb', line 13

def apply_filter?(records, options = {})
  params[:filter].present? && records.respond_to?(:where) &&
    (options[:filter].nil? || options[:filter])
end

#filter_paramsObject



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