Class: RuboCop::Cop::Rails::ActiveRecordAliases

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/active_record_aliases.rb

Overview

Checks that ActiveRecord aliases are not used. The direct method names are more clear and easier to read.

Examples:

# bad
book.update_attributes!(author: 'Alice')

# good
book.update!(author: 'Alice')

Constant Summary collapse

MSG =
'Use `%<prefer>s` instead of `%<current>s`.'
ALIASES =
{ update_attributes: :update, update_attributes!: :update! }.freeze
RESTRICT_ON_SEND =
ALIASES.keys.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rails/active_record_aliases.rb', line 28

def on_send(node)
  return if node.arguments.empty?

  method_name = node.method_name
  alias_method = ALIASES[method_name]

  add_offense(
    node.loc.selector,
    message: format(MSG, prefer: alias_method, current: method_name),
    severity: :warning
  ) do |corrector|
    corrector.replace(node.loc.selector, alias_method)
  end
end