Skip to content

Commit e965fd9

Browse files
hoshinotsuyoshikoic
authored andcommitted
Make Rails/DynamicFindBy aware of safe navigation operator
1 parent 3b89b2e commit e965fd9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/rubocop/cop/rails/dynamic_find_by.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def on_send(node)
4242
message: format(MSG, static_name: static_name,
4343
method: node.method_name))
4444
end
45+
alias on_csend on_send
4546

4647
def autocorrect(node)
4748
keywords = column_keywords(node.method_name)

spec/rubocop/cop/rails/dynamic_find_by_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@
138138
User.find_by_sql(["select * from users where name = ?", name])
139139
RUBY
140140
end
141+
142+
context 'when using safe navigation operator', :ruby23 do
143+
context 'with dynamic find_by_*' do
144+
let(:source) { 'user&.find_by_name(name)' }
145+
146+
include_examples(
147+
'register an offense and auto correct',
148+
'Use `find_by` instead of dynamic `find_by_name`.',
149+
'user&.find_by(name: name)'
150+
)
151+
end
152+
end
141153
end

0 commit comments

Comments
 (0)