Module: Kadmin::Concerns::Resources

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/kadmin/concerns/resources.rb

Constant Summary collapse

DEFAULT_FINDER_PAGE_SIZE =

Default finder page size

100

Instance Method Summary collapse

Instance Method Details

#resources_finder(scope, filters = []) ⇒ Object

Does not return anything, but will modify your finder instance.

Parameters:

  • finder (Kadmin::Finder)

    the finder to respond with

  • filters (Array<String>) (defaults to: [])

    an array of filters, of format: { name:, column:, param: }



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/kadmin/concerns/resources.rb', line 14

def resources_finder(scope, filters = [])
  filters = Array.wrap(filters)
  filter_params = filters.map { |hash| hash[:param] }
  permitted = params.permit(:page_size, :page_offset, :format, *filter_params, :sort_column, :sort_asc)

  page_size = permitted.fetch(:page_size, DEFAULT_FINDER_PAGE_SIZE).to_i
  page_offset = permitted.fetch(:page_offset, 0).to_i

  finder = Finder.new(scope)
  finder.paginate(size: page_size, offset: page_offset)

  if permitted[:sort_column].present?
    finder.order(permitted[:sort_column], ActiveRecord::Type::Boolean.new.deserialize(permitted.fetch(:sort_asc, true)))
  end

  filters.each do |hash|
    value = permitted[hash[:param]]
    filter = if hash[:filter].present?
      Finder::Filter.new(hash[:name], hash[:filter])
    else
      resources_deprecated_parse_filter(hash)
    end

    finder.filter(filter, value)
  end

  return finder
end