class RuboCop::Cop::Betterment::UnscopedFind

Constants

FINDS
METHOD_PATTERN
MSG

Attributes

unauthenticated_models[RW]

Public Class Methods

new(config = nil, options = nil) click to toggle source
Calls superclass method
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 31
def initialize(config = nil, options = nil)
  super(config, options)
  config = @config.for_cop(self)
  @unauthenticated_models = config.fetch("unauthenticated_models", []).map(&:to_sym)
end

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 37
def on_send(node)
  _, _, *arg_nodes = *node
  return unless
    (
        find?(node) ||
        custom_scope_find?(node) ||
        static_method_name(node.method_name)
    ) && !@unauthenticated_models.include?(Utils::Parser.get_root_token(node))

  add_offense(node) if find_param_arg(arg_nodes)
end

Private Instance Methods

find_param_arg(arg_nodes) click to toggle source
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 51
def find_param_arg(arg_nodes)
  return unless arg_nodes

  arg_nodes.find do |arg|
    if arg.hash_type?
      arg.children.each do |pair|
        _key, value = *pair.children
        return arg if Utils::Parser.get_root_token(value) == :params
      end
    end
    Utils::Parser.get_root_token(arg) == :params
  end
end
static_method_name(method_name) click to toggle source

yoinked from Rails/DynamicFindBy

# File lib/rubocop/cop/betterment/unscoped_find.rb, line 66
def static_method_name(method_name)
  match = METHOD_PATTERN.match(method_name)
  return nil unless match

  match[2] ? 'find_by!' : 'find_by'
end