Module: Toller::ScopeResolver

Defined in:
lib/toller/scope_resolver.rb

Overview

Shared lookup used by the type: :scope filter/sort handlers to confirm a resolved scope_name/field is actually declared on the model (a real scope, or a plain def self.foo), rather than a method inherited from ActiveRecord itself. Without this, a typo'd scope_name: that happens to collide with a built-in ActiveRecord class method (delete_all, where, sum, etc.) would silently public_send it with the raw request value instead of being treated as an unresolvable scope.

Class Method Summary collapse

Class Method Details

.own_class_method?(klass, name) ⇒ Boolean

Returns whether name is defined by klass or one of its own ancestors, rather than inherited from ActiveRecord::Base itself.

Parameters:

  • klass (Class)

    the ActiveRecord model class to check

  • name (Symbol, String)

    the method name to look for

Returns:

  • (Boolean)

    whether name is defined by klass or one of its own ancestors, rather than inherited from ActiveRecord::Base itself



19
20
21
22
23
# File 'lib/toller/scope_resolver.rb', line 19

def own_class_method?(klass, name)
  klass.singleton_class.ancestors
       .take_while { |mod| mod != ActiveRecord::Base.singleton_class }
       .any? { |mod| mod.public_method_defined?(name.to_sym, false) }
end