Skip to content

Commit 529b3a3

Browse files
hoshinotsuyoshikoic
authored andcommitted
Make Rails/ActiveRecordAliases aware of safe navigation operator
1 parent 9064595 commit 529b3a3

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/rubocop/cop/rails/active_record_aliases.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def on_send(node)
3232
end
3333
end
3434

35+
alias on_csend on_send
36+
3537
def autocorrect(node)
3638
lambda do |corrector|
3739
corrector.replace(

spec/rubocop/cop/rails/active_record_aliases_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
book.update(author: "Alice")
1515
RUBY
1616
end
17+
18+
context 'when using safe navigation operator', :ruby23 do
19+
it 'registers an offense' do
20+
expect_offense(<<-RUBY.strip_indent)
21+
book&.update_attributes(author: "Alice")
22+
^^^^^^^^^^^^^^^^^ Use `update` instead of `update_attributes`.
23+
RUBY
24+
end
25+
26+
it 'is autocorrected' do
27+
new_source = autocorrect_source(
28+
'book&.update_attributes(author: "Alice")'
29+
)
30+
expect(new_source).to eq 'book&.update(author: "Alice")'
31+
end
32+
end
1733
end
1834

1935
describe '#update_attributes!' do

0 commit comments

Comments
 (0)