Class: LcpRuby::Search::CustomFilterInterceptor
- Inherits:
-
Object
- Object
- LcpRuby::Search::CustomFilterInterceptor
- Defined in:
- lib/lcp_ruby/search/custom_filter_interceptor.rb
Class Method Summary collapse
-
.apply(scope, filter_params, model_class, evaluator) ⇒ Object
Detects and calls filter_* class methods on the model class.
-
.own_filter_method?(model_class, method_name) ⇒ Boolean
Returns true if the method is defined on the model’s singleton class (not inherited from ActiveRecord::Base or other ancestors).
Class Method Details
.apply(scope, filter_params, model_class, evaluator) ⇒ Object
Detects and calls filter_* class methods on the model class. Returns [updated_scope, remaining_params] where remaining_params has the intercepted keys removed.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lcp_ruby/search/custom_filter_interceptor.rb', line 7 def self.apply(scope, filter_params, model_class, evaluator) return [ scope, filter_params ] if filter_params.blank? remaining = filter_params.dup filter_params.each do |key, value| method_name = "filter_#{key}" next unless own_filter_method?(model_class, method_name) result = model_class.send(method_name, scope, value, evaluator) if result.is_a?(ActiveRecord::Relation) scope = result remaining.delete(key) else Rails.logger.warn( "[LcpRuby::Search] filter_#{key} on #{model_class.name} did not return " \ "ActiveRecord::Relation (got #{result.class}), skipping" ) end end [ scope, remaining ] end |
.own_filter_method?(model_class, method_name) ⇒ Boolean
Returns true if the method is defined on the model’s singleton class (not inherited from ActiveRecord::Base or other ancestors).
34 35 36 37 |
# File 'lib/lcp_ruby/search/custom_filter_interceptor.rb', line 34 def self.own_filter_method?(model_class, method_name) model_class.respond_to?(method_name) && !ActiveRecord::Base.respond_to?(method_name) end |