Class: RuboCop::Cop::Rails::PluckId

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, ActiveRecordHelper
Defined in:
lib/rubocop/cop/rails/pluck_id.rb

Overview

Enforces the use of ‘ids` over `pluck(:id)` and `pluck(primary_key)`.

Examples:

# bad
User.pluck(:id)
user.posts.pluck(:id)

def self.user_ids
  pluck(primary_key)
end

# good
User.ids
user.posts.ids

def self.user_ids
  ids
end

Constant Summary collapse

MSG =
'Use `ids` instead of `%<bad_method>s`.'
RESTRICT_ON_SEND =
%i[pluck].freeze

Constants included from ActiveRecordHelper

ActiveRecordHelper::WHERE_METHODS

Instance Method Summary collapse

Methods included from ActiveRecordHelper

#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/rails/pluck_id.rb', line 40

def on_send(node)
  return if !pluck_id_call?(node) || in_where?(node)

  range = offense_range(node)
  message = format(MSG, bad_method: range.source)

  add_offense(range, message: message) do |corrector|
    corrector.replace(offense_range(node), 'ids')
  end
end