Module: ScopeHound::FilterableController

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/scope_hound/filterable_controller.rb

Overview

Adds capability of filtering to the controllers

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#all_filtered_recordsObject

Returns the value of attribute all_filtered_records.



13
14
15
# File 'app/controllers/concerns/scope_hound/filterable_controller.rb', line 13

def all_filtered_records
  @all_filtered_records
end

#unique_filtersObject

Returns the value of attribute unique_filters.



13
14
15
# File 'app/controllers/concerns/scope_hound/filterable_controller.rb', line 13

def unique_filters
  @unique_filters
end

Instance Method Details

#filter(scope, filters = filter_params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/concerns/scope_hound/filterable_controller.rb', line 15

def filter(scope, filters = filter_params)
  filtered_scope, unique_filters = scope.filter_by(**filters)
  self.unique_filters = unique_filters
  self.all_filtered_records = filtered_scope
  filtered_scope
rescue NoMethodError
  raise "
      Controller #{self.class.name} tried to filter a scope of type #{scope.class.name}.
      Scope class does not extend FilterProxy interface.
    "
end

#filter_paramsObject



27
28
29
# File 'app/controllers/concerns/scope_hound/filterable_controller.rb', line 27

def filter_params
  raise "FilterableModel controller #{self.class.name} does not define filter_params method."
end

#filter_values(attribute, all_values, show_all: false) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/controllers/concerns/scope_hound/filterable_controller.rb', line 31

def filter_values(attribute, all_values, show_all: false)
  if show_all
    all_values
  else
    all_values.select do |_, id|
      unique_filters[attribute]&.include?(id)
    end
  end
end