Class: RuboCop::Cop::Rails::DynamicFindBy
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::DynamicFindBy
- Extended by:
- AutoCorrector
- Includes:
- ActiveRecordHelper
- Defined in:
- lib/rubocop/cop/rails/dynamic_find_by.rb
Overview
Checks dynamic ‘find_by_*` methods. Use `find_by` instead of dynamic method. See. rails.rubystyle.guide#find_by
Constant Summary collapse
- MSG =
'Use `%<static_name>s` instead of dynamic `%<method>s`.'
- METHOD_PATTERN =
/^find_by_(.+?)(!)?$/.freeze
- IGNORED_ARGUMENT_TYPES =
%i[hash splat].freeze
Constants included from ActiveRecordHelper
ActiveRecordHelper::WHERE_METHODS
Instance Method Summary collapse
- #on_send(node) ⇒ Object (also: #on_csend)
Methods included from ActiveRecordHelper
#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name
Instance Method Details
#on_send(node) ⇒ Object Also known as: on_csend
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rubocop/cop/rails/dynamic_find_by.rb', line 50 def on_send(node) return if (node.receiver.nil? && !inherit_active_record_base?(node)) || allowed_invocation?(node) method_name = node.method_name static_name = static_method_name(method_name) return unless static_name return unless dynamic_find_by_arguments?(node) = format(MSG, static_name: static_name, method: method_name) add_offense(node, message: ) do |corrector| autocorrect(corrector, node) end end |