-
-
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
146 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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for places where ordering by `id` column is used. | ||
# | ||
# Don't use the `id` column for ordering. The sequence of ids is not guaranteed | ||
# to be in any particular order, despite often (incidentally) being chronological. | ||
# Use a timestamp column to order chronologically. As a bonus the intent is clearer. | ||
# | ||
# NOTE: Make sure the changed order column does not introduce performance | ||
# bottlenecks and appropriate database indexes are added. | ||
# | ||
# @example | ||
# # bad | ||
# scope :chronological, -> { order(id: :asc) } | ||
# scope :chronological, -> { order(primary_key => :asc) } | ||
# | ||
# # good | ||
# scope :chronological, -> { order(created_at: :asc) } | ||
# | ||
class OrderById < Base | ||
include RangeHelp | ||
|
||
MSG = 'Do not use the `id` column for ordering. '\ | ||
'Use a timestamp column to order chronologically.' | ||
|
||
def_node_matcher :order_by_id?, <<~PATTERN | ||
(send _ :order | ||
{ | ||
(sym :id) | ||
(hash (pair (sym :id) _)) | ||
(send _ :primary_key) | ||
(hash (pair (send _ :primary_key) _)) | ||
}) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless node.method?(:order) | ||
|
||
add_offense(offense_range(node)) if order_by_id?(node) | ||
end | ||
|
||
private | ||
|
||
def offense_range(node) | ||
range_between(node.loc.selector.begin_pos, node.source_range.end_pos) | ||
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::OrderById do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when ordering by `:id` with implicit direction' do | ||
expect_offense(<<~RUBY) | ||
User.order(:id) | ||
^^^^^^^^^^ Do not use the `id` column for ordering. Use a timestamp column to order chronologically. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when ordering by `:id` with explicit direction' do | ||
expect_offense(<<~RUBY) | ||
User.order(id: :asc) | ||
^^^^^^^^^^^^^^^ Do not use the `id` column for ordering. Use a timestamp column to order chronologically. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when ordering by `primary_key` with implicit direction' do | ||
expect_offense(<<~RUBY) | ||
scope :chronological, -> { order(primary_key) } | ||
^^^^^^^^^^^^^^^^^^ Do not use the `id` column for ordering. Use a timestamp column to order chronologically. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when ordering by `primary_key` with explicit direction' do | ||
expect_offense(<<~RUBY) | ||
scope :chronological, -> { order(primary_key => :asc) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the `id` column for ordering. Use a timestamp column to order chronologically. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when ordering by non id column' do | ||
expect_no_offenses(<<~RUBY) | ||
User.order(:created_at) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when ordering by multiple columns, including id' do | ||
expect_no_offenses(<<~RUBY) | ||
User.order(id: :asc, created_at: :desc) | ||
RUBY | ||
end | ||
end |