Skip to content

Commit f57c3ba

Browse files
authored
Merge pull request #1497 from r7kamura/in-where-fix
Fix bugs in `Rails/PluckId` and `Rails/PluckInWhere`
2 parents 242b6b4 + 43f7b57 commit f57c3ba

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1497](https://github.com/rubocop/rubocop-rails/pull/1497): Fix bugs in `Rails/PluckId` and `Rails/PluckInWhere`. ([@r7kamura][])

lib/rubocop/cop/mixin/active_record_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def in_where?(node)
106106
send_node = node.each_ancestor(:call).first
107107
return false unless send_node
108108

109-
return true if WHERE_METHODS.include?(send_node.method_name)
109+
return true if WHERE_METHODS.include?(send_node.method_name) && send_node.receiver != node
110110

111111
receiver = send_node.receiver
112112
return false unless receiver&.send_type?

spec/rubocop/cop/rails/pluck_id_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ def self.user_ids
5555
Post.where(user_id: User.pluck(:id))
5656
RUBY
5757
end
58+
59+
context 'when `pluck` is the receiver of `where`' do
60+
it 'registers an offense' do
61+
expect_offense(<<~RUBY)
62+
Post.pluck(:id).where(id: 1..10)
63+
^^^^^^^^^^ Use `ids` instead of `pluck(:id)`.
64+
RUBY
65+
66+
expect_correction(<<~RUBY)
67+
Post.ids.where(id: 1..10)
68+
RUBY
69+
end
70+
end
5871
end

spec/rubocop/cop/rails/pluck_in_where_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@
132132
RUBY
133133
end
134134
end
135+
136+
context 'when `pluck` is the receiver of `where`' do
137+
it 'does not register an offense' do
138+
expect_no_offenses(<<~RUBY)
139+
Post.pluck(:id).where(id: 1..10)
140+
RUBY
141+
end
142+
end
135143
end
136144

137145
context 'EnforcedStyle: aggressive' do

0 commit comments

Comments
 (0)