Skip to content

Commit bdca510

Browse files
hoshinotsuyoshikoic
authored andcommitted
Make Rails/SkipsModelValidations aware of safe navigation operator
1 parent c486005 commit bdca510

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/rubocop/cop/rails/skips_model_validations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def on_send(node)
6767

6868
add_offense(node, location: :selector)
6969
end
70+
alias on_csend on_send
7071

7172
private
7273

spec/rubocop/cop/rails/skips_model_validations_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@
7777
^^^^^^^^^^^^^^^^ Avoid using `update_attribute` because it skips validations.
7878
RUBY
7979
end
80+
81+
context 'when using safe navigation operator', :ruby23 do
82+
it 'registers an offense for `update_attribute`' do
83+
expect_offense(<<-RUBY.strip_indent)
84+
user&.update_attribute(:website, 'example.com')
85+
^^^^^^^^^^^^^^^^ Avoid using `update_attribute` because it skips validations.
86+
RUBY
87+
end
88+
end
8089
end
8190

8291
context 'with whitelist' do
@@ -94,6 +103,15 @@
94103
RUBY
95104
end
96105

106+
context 'when using safe navigation operator', :ruby23 do
107+
it 'registers an offense for method not in whitelist' do
108+
expect_offense(<<-RUBY.strip_indent)
109+
user&.toggle!(:active)
110+
^^^^^^^ Avoid using `toggle!` because it skips validations.
111+
RUBY
112+
end
113+
end
114+
97115
it 'accepts method in whitelist, superseding the blacklist' do
98116
expect_no_offenses('User.touch(:attr)')
99117
end

0 commit comments

Comments
 (0)