Skip to content

Commit a3db656

Browse files
authored
Merge pull request #1216 from fatkodima/pluck_in_where-ids
Update `Rails/PluckInWhere` to check for `.ids` call
2 parents 1431257 + b9e40e1 commit a3db656

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1213](https://github.com/rubocop/rubocop-rails/issues/1213): Update `Rails/PluckInWhere` to check for `.ids` call. ([@fatkodima][])

lib/rubocop/cop/rails/pluck_in_where.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Rails
2222
# @example
2323
# # bad
2424
# Post.where(user_id: User.active.pluck(:id))
25+
# Post.where(user_id: User.active.ids)
2526
# Post.where.not(user_id: User.active.pluck(:id))
2627
#
2728
# # good
@@ -42,17 +43,26 @@ class PluckInWhere < Base
4243
include ConfigurableEnforcedStyle
4344
extend AutoCorrector
4445

45-
MSG = 'Use `select` instead of `pluck` within `where` query method.'
46-
RESTRICT_ON_SEND = %i[pluck].freeze
46+
MSG_SELECT = 'Use `select` instead of `pluck` within `where` query method.'
47+
MSG_IDS = 'Use `select(:id)` instead of `ids` within `where` query method.'
48+
RESTRICT_ON_SEND = %i[pluck ids].freeze
4749

4850
def on_send(node)
4951
return unless in_where?(node)
5052
return if style == :conservative && !root_receiver(node)&.const_type?
5153

5254
range = node.loc.selector
5355

54-
add_offense(range) do |corrector|
55-
corrector.replace(range, 'select')
56+
if node.method?(:ids)
57+
replacement = 'select(:id)'
58+
message = MSG_IDS
59+
else
60+
replacement = 'select'
61+
message = MSG_SELECT
62+
end
63+
64+
add_offense(range, message: message) do |corrector|
65+
corrector.replace(range, replacement)
5666
end
5767
end
5868

spec/rubocop/cop/rails/pluck_in_where_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
RUBY
1818
end
1919

20+
it 'registers an offense and corrects when using `ids` in `where` for constant' do
21+
expect_offense(<<~RUBY)
22+
Post.where(user_id: User.active.ids)
23+
^^^ Use `select(:id)` instead of `ids` within `where` query method.
24+
RUBY
25+
26+
expect_correction(<<~RUBY)
27+
Post.where(user_id: User.active.select(:id))
28+
RUBY
29+
end
30+
2031
it 'registers an offense and corrects when using `pluck` in `where.not` for constant' do
2132
expect_offense(<<~RUBY)
2233
Post.where.not(user_id: User.active.pluck(:id))
@@ -28,6 +39,17 @@
2839
RUBY
2940
end
3041

42+
it 'registers an offense and corrects when using `ids` in `where.not` for constant' do
43+
expect_offense(<<~RUBY)
44+
Post.where.not(user_id: User.active.ids)
45+
^^^ Use `select(:id)` instead of `ids` within `where` query method.
46+
RUBY
47+
48+
expect_correction(<<~RUBY)
49+
Post.where.not(user_id: User.active.select(:id))
50+
RUBY
51+
end
52+
3153
it 'registers an offense and corrects when using `pluck` in `rewhere` for constant' do
3254
expect_offense(<<~RUBY)
3355
Post.rewhere('user_id IN (?)', User.active.pluck(:id))
@@ -39,6 +61,17 @@
3961
RUBY
4062
end
4163

64+
it 'registers an offense and corrects when using `ids` in `rewhere` for constant' do
65+
expect_offense(<<~RUBY)
66+
Post.rewhere('user_id IN (?)', User.active.ids)
67+
^^^ Use `select(:id)` instead of `ids` within `where` query method.
68+
RUBY
69+
70+
expect_correction(<<~RUBY)
71+
Post.rewhere('user_id IN (?)', User.active.select(:id))
72+
RUBY
73+
end
74+
4275
it 'does not register an offense when using `select` in `where`' do
4376
expect_no_offenses(<<~RUBY)
4477
Post.where(user_id: User.active.select(:id))
@@ -51,11 +84,23 @@
5184
RUBY
5285
end
5386

87+
it 'does not register an offense when using `ids` chained with other method calls in `where`' do
88+
expect_no_offenses(<<~RUBY)
89+
Post.where(user_id: User.ids.map(&:to_i))
90+
RUBY
91+
end
92+
5493
it 'does not register an offense when using `select` in query methods other than `where`' do
5594
expect_no_offenses(<<~RUBY)
5695
Post.order(columns.pluck(:name))
5796
RUBY
5897
end
98+
99+
it 'does not register an offense when using `ids` in query methods other than `where`' do
100+
expect_no_offenses(<<~RUBY)
101+
Post.order(columns.ids)
102+
RUBY
103+
end
59104
end
60105

61106
context 'EnforcedStyle: conservative' do
@@ -69,6 +114,12 @@
69114
Post.where(user_id: users.active.pluck(:id))
70115
RUBY
71116
end
117+
118+
it 'does not register an offense when using `ids` in `where`' do
119+
expect_no_offenses(<<~RUBY)
120+
Post.where(user_id: users.active.ids)
121+
RUBY
122+
end
72123
end
73124
end
74125

@@ -88,6 +139,17 @@
88139
Post.where(user_id: users.active.select(:id))
89140
RUBY
90141
end
142+
143+
it 'registers and corrects an offense when using `ids` in `where`' do
144+
expect_offense(<<~RUBY)
145+
Post.where(user_id: users.active.ids)
146+
^^^ Use `select(:id)` instead of `ids` within `where` query method.
147+
RUBY
148+
149+
expect_correction(<<~RUBY)
150+
Post.where(user_id: users.active.select(:id))
151+
RUBY
152+
end
91153
end
92154
end
93155
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy