forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#310. This PR adds `EnforcedStyle` to `Rails/PluckInWhere`. By default, it does not register an offense if `pluck` method's receiver is a variable.
- Loading branch information
Showing
5 changed files
with
157 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::PluckInWhere do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using `pluck` in `where`' do | ||
expect_offense(<<~RUBY) | ||
Post.where(user_id: User.pluck(:id)) | ||
^^^^^ Use `select` instead of `pluck` within `where` query method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Post.where(user_id: User.select(:id)) | ||
RUBY | ||
end | ||
RSpec.describe RuboCop::Cop::Rails::PluckInWhere, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense and corrects when using `pluck` in `rewhere`' do | ||
expect_offense(<<~RUBY) | ||
Post.rewhere('user_id IN (?)', User.pluck(:id)) | ||
^^^^^ Use `select` instead of `pluck` within `where` query method. | ||
RUBY | ||
shared_examples 'receiver is a constant for `pluck`' do | ||
it 'registers an offense and corrects when using `pluck` in `where` for constant' do | ||
expect_offense(<<~RUBY) | ||
Post.where(user_id: User.active.pluck(:id)) | ||
^^^^^ Use `select` instead of `pluck` within `where` query method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Post.rewhere('user_id IN (?)', User.select(:id)) | ||
RUBY | ||
end | ||
expect_correction(<<~RUBY) | ||
Post.where(user_id: User.active.select(:id)) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `pluck` in `rewhere` for constant' do | ||
expect_offense(<<~RUBY) | ||
Post.rewhere('user_id IN (?)', User.active.pluck(:id)) | ||
^^^^^ Use `select` instead of `pluck` within `where` query method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Post.rewhere('user_id IN (?)', User.active.select(:id)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select` in `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.where(user_id: User.active.select(:id)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `pluck` chained with other method calls in `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.where(user_id: User.pluck(:id).map(&:to_i)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select` in `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.where(user_id: User.select(:id)) | ||
RUBY | ||
it 'does not register an offense when using `select` in query methods other than `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.order(columns.pluck(:name)) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `pluck` chained with other method calls in `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.where(user_id: User.pluck(:id).map(&:to_i)) | ||
RUBY | ||
context 'EnforcedStyle: conservative' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'conservative' } | ||
end | ||
|
||
it_behaves_like 'receiver is a constant for `pluck`' | ||
|
||
context 'receiver is a variable for `pluck`' do | ||
it 'does not register an offense when using `pluck` in `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.where(user_id: users.active.pluck(:id)) | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `select` in query methods other than `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
Post.order(columns.pluck(:name)) | ||
RUBY | ||
context 'EnforcedStyle: aggressive' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'aggressive' } | ||
end | ||
|
||
it_behaves_like 'receiver is a constant for `pluck`' | ||
|
||
context 'receiver is a variable for `pluck`' do | ||
it 'registers and corrects an offense when using `pluck` in `where`' do | ||
expect_offense(<<~RUBY) | ||
Post.where(user_id: users.active.pluck(:id)) | ||
^^^^^ Use `select` instead of `pluck` within `where` query method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Post.where(user_id: users.active.select(:id)) | ||
RUBY | ||
end | ||
end | ||
end | ||
end |