-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathorder_by_id.rb
51 lines (45 loc) · 1.54 KB
/
order_by_id.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# 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.'
RESTRICT_ON_SEND = %i[order].freeze
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)
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