Class: ScopeHound::FilterProxy
- Inherits:
-
Object
- Object
- ScopeHound::FilterProxy
- Extended by:
- FilterScopable
- Defined in:
- app/models/concerns/scope_hound/filter_proxy.rb
Overview
Supports the filter by methods. And runs the filter methods to get the result query
Class Method Summary collapse
- .calculate_unique_filter_values(scope) ⇒ Object
- .filter_by(**filters) ⇒ Object
- .filter_scopes_module ⇒ Object
-
.query_scope ⇒ Object
Model Class whose scope will be extended with our filter scopes module.
Methods included from FilterScopable
filter_scope, filter_scope_path_for, filter_scopes_paths
Class Method Details
.calculate_unique_filter_values(scope) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/concerns/scope_hound/filter_proxy.rb', line 33 def calculate_unique_filter_values(scope) result = {} filter_scopes_module.filter_scopes_paths.each_with_object({}) do |(filter_scope, path)| result[filter_scope] = if path.is_a? Array path.flat_map { |p| scope.pluck(p) }.uniq else scope.pluck(path).uniq end end result end |
.filter_by(**filters) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/concerns/scope_hound/filter_proxy.rb', line 18 def filter_by(**filters) # extend model class scope with filter methods extended_scope = query_scope.extending(filter_scopes_module) filters.each do |filter_scope, filter_value| if filter_value.present? && extended_scope.respond_to?(filter_scope) extended_scope = extended_scope.send(filter_scope, filter_value) end end unique_filter_values = calculate_unique_filter_values(extended_scope) [extended_scope, unique_filter_values] end |
.filter_scopes_module ⇒ Object
14 15 16 |
# File 'app/models/concerns/scope_hound/filter_proxy.rb', line 14 def filter_scopes_module raise "Class #{name} does not define filter_scopes_module class method." end |
.query_scope ⇒ Object
Model Class whose scope will be extended with our filter scopes module
10 11 12 |
# File 'app/models/concerns/scope_hound/filter_proxy.rb', line 10 def query_scope raise "Class #{name} does not define query_scope class method." end |