Class: RuboCop::Cop::Rails::DynamicFindBy
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Rails::DynamicFindBy
- Defined in:
- lib/rubocop/cop/rails/dynamic_find_by.rb
Overview
This cop 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
Instance Method Summary collapse
- #autocorrect(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
Instance Method Details
#autocorrect(node) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubocop/cop/rails/dynamic_find_by.rb', line 51 def autocorrect(node) keywords = column_keywords(node.method_name) return if keywords.size != node.arguments.size lambda do |corrector| autocorrect_method_name(corrector, node) autocorrect_argument_keywords(corrector, node, keywords) end end |
#on_send(node) ⇒ Object Also known as: on_csend
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rubocop/cop/rails/dynamic_find_by.rb', line 38 def on_send(node) return if allowed_invocation?(node) method_name = node.method_name static_name = static_method_name(method_name) return unless static_name add_offense(node, message: format(MSG, static_name: static_name, method: method_name)) end |