forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_foreign_key.rb
77 lines (68 loc) · 2.33 KB
/
redundant_foreign_key.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Detects cases where the `:foreign_key` option on associations
# is redundant.
#
# @example
# # bad
# class Post
# has_many :comments, foreign_key: 'post_id'
# end
#
# class Comment
# belongs_to :post, foreign_key: 'post_id'
# end
#
# # good
# class Post
# has_many :comments
# end
#
# class Comment
# belongs_to :author, foreign_key: 'user_id'
# end
class RedundantForeignKey < Base
include RangeHelp
extend AutoCorrector
MSG = 'Specifying the default value for `foreign_key` is redundant.'
RESTRICT_ON_SEND = %i[belongs_to has_one has_many has_and_belongs_to_many].freeze
def_node_matcher :association_with_foreign_key, <<~PATTERN
(send nil? ${:belongs_to :has_one :has_many :has_and_belongs_to_many} ({sym str} $_)
$(hash <$(pair (sym :foreign_key) ({sym str} $_)) ...>)
)
PATTERN
def on_send(node)
association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
if redundant?(node, type, name, options, foreign_key)
add_offense(foreign_key_pair.source_range) do |corrector|
range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
range = range_with_surrounding_comma(range, :left)
corrector.remove(range)
end
end
end
end
private
def redundant?(node, association_type, association_name, options, foreign_key)
foreign_key.to_s == default_foreign_key(node, association_type, association_name, options)
end
def default_foreign_key(node, association_type, association_name, options)
if association_type == :belongs_to
"#{association_name}_id"
elsif (as = find_as_option(options))
"#{as}_id"
else
node.parent_module_name&.foreign_key
end
end
def find_as_option(options)
options.pairs.find do |pair|
pair.key.sym_type? && pair.key.value == :as
end&.value&.value
end
end
end
end
end