-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
Showing
7 changed files
with
191 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,103 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop enforces that `ActiveRecord#find` is used instead of | ||
# `where.take!`, `find_by!`, and `find_by_id!` to retrieve a single record | ||
# by primary key when you expect it to be found. | ||
# | ||
# @example | ||
# # bad | ||
# User.where(id: id).take! | ||
# User.find_by_id!(id) | ||
# User.find_by!(id: id) | ||
# | ||
# # good | ||
# User.find(id) | ||
# | ||
class ActiveRecordFind < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.' | ||
|
||
def_node_matcher :where_take?, <<~PATTERN | ||
(send | ||
$(send _ :where | ||
(hash | ||
(pair (sym :id) $_))) :take!) | ||
PATTERN | ||
|
||
def_node_matcher :find_by?, <<~PATTERN | ||
{ | ||
(send _ :find_by_id! $_) | ||
(send _ :find_by! (hash (pair (sym :id) $_))) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
where_take?(node) do |where, id_value| | ||
range = where_take_offense_range(node, where) | ||
|
||
good_method = build_good_method(id_value) | ||
bad_method = build_where_take_bad_method(id_value) | ||
message = format(MSG, good_method: good_method, bad_method: bad_method) | ||
|
||
add_offense(node, location: range, message: message) | ||
end | ||
|
||
find_by?(node) do |id_value| | ||
range = find_by_offense_range(node) | ||
|
||
good_method = build_good_method(id_value) | ||
bad_method = build_find_by_bad_method(node, id_value) | ||
message = format(MSG, good_method: good_method, bad_method: bad_method) | ||
|
||
add_offense(node, location: range, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
if (matches = where_take?(node)) | ||
where, id_value = *matches | ||
range = where_take_offense_range(node, where) | ||
elsif (id_value = find_by?(node)) | ||
range = find_by_offense_range(node) | ||
end | ||
|
||
lambda do |corrector| | ||
replacement = build_good_method(id_value) | ||
corrector.replace(range, replacement) | ||
end | ||
end | ||
|
||
private | ||
|
||
def where_take_offense_range(node, where) | ||
range_between(where.loc.selector.begin_pos, node.loc.expression.end_pos) | ||
end | ||
|
||
def find_by_offense_range(node) | ||
range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) | ||
end | ||
|
||
def build_good_method(id_value) | ||
"find(#{id_value.source})" | ||
end | ||
|
||
def build_where_take_bad_method(id_value) | ||
"where(id: #{id_value.source}).take!" | ||
end | ||
|
||
def build_find_by_bad_method(node, id_value) | ||
case node.method_name | ||
when :find_by_id! | ||
"find_by_id!(#{id_value.source})" | ||
when :find_by! | ||
"find_by!(id: #{id_value.source})" | ||
end | ||
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ActiveRecordFind do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using `where(id: ...).take!`' do | ||
expect_offense(<<~RUBY) | ||
User.where(id: 1).take! | ||
^^^^^^^^^^^^^^^^^^ Use `find(1)` instead of `where(id: 1).take!`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
User.find(1) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `find_by_id!`' do | ||
expect_offense(<<~RUBY) | ||
User.find_by_id!(1) | ||
^^^^^^^^^^^^^^ Use `find(1)` instead of `find_by_id!(1)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
User.find(1) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `find_by!(id: ...)`' do | ||
expect_offense(<<~RUBY) | ||
User.find_by!(id: 1) | ||
^^^^^^^^^^^^^^^ Use `find(1)` instead of `find_by!(id: 1)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
User.find(1) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `find`' do | ||
expect_no_offenses(<<~RUBY) | ||
User.find(1) | ||
RUBY | ||
end | ||
end |