Module: AdministrateFilterable::Filterer

Extended by:
ActiveSupport::Concern
Defined in:
lib/administrate_filterable/filterer.rb

Instance Method Summary collapse

Instance Method Details

#filtered_resources(resources) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/administrate_filterable/filterer.rb', line 23

def filtered_resources(resources)
  @filterable_attributes = AdministrateFilterable::FiltererService.filter_attributes(dashboard, new_resource)

  filter_params = params[resource_name]
  return resources if filter_params.blank?

  filter_params.each do |key, value|
    next unless resources.column_names.include?(key.to_s) && value.present?

    # TODO: Add support for relational filter (e.g. filter by `belongs_to` association, etc)
    sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, ["#{key} LIKE ?", "%#{value}%"])
    resources = resources.where(sanitized_query)
  end

  resources
end