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_class(node)
click to toggle source
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 37 def on_class(node) Utils::MethodReturnTable.populate_index(node) end
on_send(node)
click to toggle source
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 41 def on_send(node) _, _, *arg_nodes = *node # rubocop:disable InternalAffairs/NodeDestructuring 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 55 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 uses_params?(value) end end uses_params?(arg) end end
static_method_name(method_name)
click to toggle source
yoinked from Rails/DynamicFindBy
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 78 def static_method_name(method_name) match = METHOD_PATTERN.match(method_name) return nil unless match match[2] ? 'find_by!' : 'find_by' end
uses_params?(node)
click to toggle source
# File lib/rubocop/cop/betterment/unscoped_find.rb, line 70 def uses_params?(node) root = Utils::Parser.get_root_token(node) root == :params || Array(Utils::MethodReturnTable.get_method(root)).find do |x| Utils::Parser.get_root_token(x) == :params end end