Sha256: 05b92f2a91a63c2f08f958956478856d684a4a2517e44fbf81ef417de61abefa

Contents?: true

Size: 1.57 KB

Versions: 21

Compression:

Stored size: 1.57 KB

Contents

# 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.'
        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

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/order_by_id.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/order_by_id.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/order_by_id.rb