Class: RuboCop::Cop::YiffSpace::ResolvableUser
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::YiffSpace::ResolvableUser
show all
- Extended by:
- AutoCorrector
- Includes:
- ActiveRecordHelper, NodeFormattingHelper
- Defined in:
- lib/rubocop/cop/yiff_space/resolvable_user.rb
Overview
Detects ‘resolvable` calls that should instead use `belongs_to_user` because the corresponding `<attr>_id` column exists in the database schema. It auto-corrects to `belongs_to_user`, adding `ip: true` when a `<attr>_ip_addr` column also exists.
Constant Summary
collapse
- MSG =
"use `belongs_to_user(%<attr>s)` when `%<id_column>s` column exists"
- MSG_IP =
"use `belongs_to_user(%<attr>s, ip: true)` when `%<id_column>s` and `%<ip_column>s` columns exist"
NodeFormattingHelper::BASIC
Instance Method Summary
collapse
#format_node
Instance Method Details
#on_csend(node) ⇒ Object
69
70
71
|
# File 'lib/rubocop/cop/yiff_space/resolvable_user.rb', line 69
def on_csend(node)
on_send(node)
end
|
#on_send(node) ⇒ Object
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
|
# File 'lib/rubocop/cop/yiff_space/resolvable_user.rb', line 42
def on_send(node)
resolvable?(node) do |receiver, code|
return unless receiver.type?(:str, :sym)
attr = format_node(receiver)
return if attr.nil? || attr.empty?
options = format_node(code.last, {})
return unless schema
table = table(node)
return unless table
column = "#{attr}_id"
ip_column = "#{attr}_ip_addr"
exists = table.with_column?(name: column)
ip_exists = table.with_column?(name: ip_column)
return unless exists
options[:ip] = true if ip_exists
register_offense(node, attr, options, column, ip_column)
end
end
|
#resolvable?(node) ⇒ Object
38
39
40
|
# File 'lib/rubocop/cop/yiff_space/resolvable_user.rb', line 38
def_node_matcher(:resolvable?, <<~PATTERN)
(send nil? :resolvable $_ $...)
PATTERN
|