-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3117a2f
commit b549626
Showing
7 changed files
with
141 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop prevents usage of `"*"` on an Arel::Table column reference. | ||
# | ||
# Using `arel_table["*"]` causes the outputted string to be a literal | ||
# quoted asterisk (e.g. <tt>`my_model`.`*`</tt>). This causes the | ||
# database to look for a column named <tt>`*`</tt> (or `"*"`) as opposed | ||
# to expanding the column list as one would likely expect. | ||
# | ||
# @example | ||
# # bad | ||
# MyTable.arel_table["*"] | ||
# | ||
# # good | ||
# MyTable.arel_table[Arel.star] | ||
# | ||
class ArelStar < Cop | ||
MSG = 'Use `Arel.star` instead of `"*"` for expanded column lists.' | ||
|
||
RESTRICT_ON_SEND = %i[[]].freeze | ||
|
||
def_node_matcher :star_bracket?, <<~PATTERN | ||
(send {const (send _ :arel_table)} :[] $(str "*")) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless (star = star_bracket?(node)) | ||
|
||
add_offense(star) | ||
end | ||
|
||
def autocorrect(node) | ||
->(corrector) { corrector.replace(node.loc.expression, 'Arel.star') } | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ArelStar do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when an asterisk is used on an Arel::Table column reference' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
scope :my_scope, -> { select(arel_table["*"]) } | ||
^^^ Use `Arel.star` instead of `"*"` for expanded column lists. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
scope :my_scope, -> { select(arel_table[Arel.star]) } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense on the `arel_table` object in a void' do | ||
expect_offense(<<~RUBY) | ||
arel_table["*"] | ||
^^^ Use `Arel.star` instead of `"*"` for expanded column lists. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
arel_table[Arel.star] | ||
RUBY | ||
end | ||
|
||
it 'registers an offense on the `arel_table` object on a model' do | ||
expect_offense(<<~RUBY) | ||
MyModel.arel_table["*"] | ||
^^^ Use `Arel.star` instead of `"*"` for expanded column lists. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
MyModel.arel_table[Arel.star] | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for ArelExtensions asterisks' do | ||
expect_offense(<<~RUBY) | ||
MyModel["*"] | ||
^^^ Use `Arel.star` instead of `"*"` for expanded column lists. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
MyModel[Arel.star] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense on an object not named `arel_table`' do | ||
expect_no_offenses(<<~RUBY) | ||
hsh = { '*' => 'foo' } | ||
hsh['*'] | ||
RUBY | ||
end | ||
end |