Class: RuboCop::Rails::ErrorsAddWithSymbol

Inherits:
Cop::Cop
  • Object
show all
Defined in:
lib/rubocop/rails/errors_add_with_symbol.rb

Overview

This cop enforces usage of ActiveModel::Errors#add with symbols

When a symbol is passed to this method, then the I18n lookup happens within the method, allowing to generate error messages in different languages. That’s not possible when already translated strings are passed to this method.

Constant Summary collapse

MSG =
'Only pass symbols to `errors.add`'.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/rails/errors_add_with_symbol.rb', line 23

def on_send(node)
  captures = errors_add?(node)
  return unless captures

  message_node = captures[1]

  return if message_node.nil? || message_node.sym_type? || symbolizes?(message_node)

  generate_offense(node)
end