Skip to content

Commit 26b18c5

Browse files
committed
Fix an error for Rails/WhereNot without second argument
This is basically #401 again, and a bit of #1321
1 parent d4a4ea7 commit 26b18c5

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1330](https://github.com/rubocop/rubocop-rails/pull/1330): Fix an error for `Rails/WhereNot` when using placeholder without second argument. ([@earlopain][])

lib/rubocop/cop/rails/where_not.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def on_send(node)
4343

4444
range = offense_range(node)
4545

46-
column_and_value = extract_column_and_value(template_node, value_node)
47-
return unless column_and_value
46+
column, value = extract_column_and_value(template_node, value_node)
47+
return unless value
4848

49-
good_method = build_good_method(node.loc.dot&.source, *column_and_value)
49+
good_method = build_good_method(node.loc.dot&.source, column, value)
5050
message = format(MSG, good_method: good_method)
5151

5252
add_offense(range, message: message) do |corrector|
@@ -72,9 +72,9 @@ def extract_column_and_value(template_node, value_node)
7272
value =
7373
case template_node.value
7474
when NOT_EQ_ANONYMOUS_RE, NOT_IN_ANONYMOUS_RE
75-
value_node.source
75+
value_node&.source
7676
when NOT_EQ_NAMED_RE, NOT_IN_NAMED_RE
77-
return unless value_node.hash_type?
77+
return unless value_node&.hash_type?
7878

7979
pair = value_node.pairs.find { |p| p.key.value.to_sym == Regexp.last_match(2).to_sym }
8080
pair.value.source

spec/rubocop/cop/rails/where_not_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
RUBY
1313
end
1414

15+
it 'registers no offense when using `!=` and anonymous placeholder without second argument' do
16+
expect_no_offenses(<<~RUBY)
17+
User.where('name != ?')
18+
RUBY
19+
end
20+
1521
it 'registers an offense and corrects when using `!=` and anonymous placeholder with safe navigation' do
1622
expect_offense(<<~RUBY)
1723
User&.where('name != ?', 'Gabe')
@@ -89,6 +95,12 @@
8995
RUBY
9096
end
9197

98+
it 'registers no offense when using `NOT IN` and named placeholder without second argument' do
99+
expect_no_offenses(<<~RUBY)
100+
User.where("name NOT IN (:names)")
101+
RUBY
102+
end
103+
92104
it 'registers an offense and corrects when using `!=` and namespaced columns' do
93105
expect_offense(<<~RUBY)
94106
Course.where('enrollments.student_id != ?', student.id)

0 commit comments

Comments
 (0)