forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate_association.rb
More file actions
117 lines (98 loc) · 3.61 KB
/
Copy pathduplicate_association.rb
File metadata and controls
117 lines (98 loc) · 3.61 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Looks for associations that have been defined multiple times in the same file.
#
# When an association is defined multiple times on a model, Active Record overrides the
# previously defined association with the new one. Because of this, this cop's autocorrection
# simply keeps the last of any duplicates and discards the rest.
#
# @example
#
# # bad
# belongs_to :foo
# belongs_to :bar
# has_one :foo
#
# # good
# belongs_to :bar
# has_one :foo
#
# # bad
# has_many :foo, class_name: 'Foo'
# has_many :bar, class_name: 'Foo'
# has_one :baz
#
# # good
# has_many :bar, class_name: 'Foo'
# has_one :foo
#
class DuplicateAssociation < Base
include RangeHelp
extend AutoCorrector
include ClassSendNodeHelper
include ActiveRecordHelper
MSG = "Association `%<name>s` is defined multiple times. Don't repeat associations."
MSG_CLASS_NAME = "Association `class_name: %<name>s` is defined multiple times. Don't repeat associations."
def_node_matcher :association, <<~PATTERN
(send nil? {:belongs_to :has_one :has_many :has_and_belongs_to_many} ({sym str} $_) $...)
PATTERN
def_node_matcher :class_name, <<~PATTERN
(hash (pair (sym :class_name) $_))
PATTERN
def on_class(class_node)
return unless active_record?(class_node.parent_class)
association_nodes = association_nodes(class_node)
duplicated_association_name_nodes(association_nodes).each do |name, nodes|
register_offense(name, nodes, MSG)
end
duplicated_class_name_nodes(association_nodes).each do |class_name, nodes|
register_offense(class_name, nodes, MSG_CLASS_NAME)
end
end
private
def register_offense(name, nodes, message_template)
last_node = nodes.last
nodes.each_with_index do |node, index|
add_offense(node, message: format(message_template, name: name)) do |corrector|
if index.zero?
corrector.replace(node, last_node.source)
else
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
end
end
end
def association_nodes(class_node)
class_send_nodes(class_node).select do |node|
association(node)&.first
end
end
def duplicated_association_name_nodes(association_nodes)
grouped_associations = association_nodes.group_by do |node|
association(node).first.to_sym
end
leave_duplicated_association(grouped_associations)
end
def duplicated_class_name_nodes(association_nodes)
filtered_nodes = association_nodes.reject { |node| node.method?(:belongs_to) }
grouped_associations = filtered_nodes.group_by do |node|
arguments = association(node).last
next unless arguments.one?
if (class_name = class_name(arguments.first))
class_name.source
end
end
grouped_associations.delete(nil)
leave_duplicated_association(grouped_associations)
end
def leave_duplicated_association(grouped_associations)
grouped_associations.select do |_, nodes|
nodes.length > 1
end
end
end
end
end
end