forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_record_aliases.rb
48 lines (42 loc) · 1.15 KB
/
active_record_aliases.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks that ActiveRecord aliases are not used. The direct method names
# are more clear and easier to read.
#
# @example
# #bad
# Book.update_attributes!(author: 'Alice')
#
# #good
# Book.update!(author: 'Alice')
class ActiveRecordAliases < Cop
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
ALIASES = {
update_attributes: :update,
update_attributes!: :update!
}.freeze
def on_send(node)
ALIASES.each do |bad, good|
next unless node.method?(bad)
add_offense(node,
message: format(MSG, prefer: good, current: bad),
location: :selector,
severity: :warning)
break
end
end
alias on_csend on_send
def autocorrect(node)
lambda do |corrector|
corrector.replace(
node.loc.selector,
ALIASES[node.method_name].to_s
)
end
end
end
end
end
end