Class: RuboCop::Cop::Rails::RedundantActiveRecordAllMethod

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

Overview

Detect redundant ‘all` used as a receiver for Active Record query methods.

For the methods ‘delete_all` and `destroy_all`, this cop will only check cases where the receiver is a model. It will ignore cases where the receiver is an association (e.g., `user.articles.all.delete_all`). This is because omitting `all` from an association changes the methods from `ActiveRecord::Relation` to `ActiveRecord::Associations::CollectionProxy`, which can affect their behavior.

Examples:

# bad
User.all.find(id)
User.all.order(:created_at)
users.all.where(id: ids)
user.articles.all.order(:created_at)

# good
User.find(id)
User.order(:created_at)
users.where(id: ids)
user.articles.order(:created_at)

AllowedReceivers: [‘ActionMailer::Preview’, ‘ActiveSupport::TimeZone’] (default)

# good
ActionMailer::Preview.all.first
ActiveSupport::TimeZone.all.first

Constant Summary collapse

MSG =
'Redundant `all` detected.'
RESTRICT_ON_SEND =
[:all].freeze
QUERYING_METHODS =

Defined methods in ‘ActiveRecord::Querying::QUERYING_METHODS` on activerecord 7.1.0.

%i[
  and
  annotate
  any?
  async_average
  async_count
  async_ids
  async_maximum
  async_minimum
  async_pick
  async_pluck
  async_sum
  average
  calculate
  count
  create_or_find_by
  create_or_find_by!
  create_with
  delete_all
  delete_by
  destroy_all
  destroy_by
  distinct
  eager_load
  except
  excluding
  exists?
  extending
  extract_associated
  fifth
  fifth!
  find
  find_by
  find_by!
  find_each
  find_in_batches
  find_or_create_by
  find_or_create_by!
  find_or_initialize_by
  find_sole_by
  first
  first!
  first_or_create
  first_or_create!
  first_or_initialize
  forty_two
  forty_two!
  fourth
  fourth!
  from
  group
  having
  ids
  in_batches
  in_order_of
  includes
  invert_where
  joins
  last
  last!
  left_joins
  left_outer_joins
  limit
  lock
  many?
  maximum
  merge
  minimum
  none
  none?
  offset
  one?
  only
  optimizer_hints
  or
  order
  pick
  pluck
  preload
  readonly
  references
  regroup
  reorder
  reselect
  rewhere
  second
  second!
  second_to_last
  second_to_last!
  select
  sole
  strict_loading
  sum
  take
  take!
  third
  third!
  third_to_last
  third_to_last!
  touch_all
  unscope
  update_all
  where
  with
  without
].to_set.freeze
POSSIBLE_ENUMERABLE_BLOCK_METHODS =
%i[any? count find none? one? select sum].freeze
SENSITIVE_METHODS_ON_ASSOCIATION =
%i[delete_all destroy_all].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



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rubocop/cop/rails/redundant_active_record_all_method.rb', line 159

def on_send(node)
  return unless followed_by_query_method?(node.parent)
  return if possible_enumerable_block_method?(node) || sensitive_association_method?(node)
  return if node.receiver ? allowed_receiver?(node.receiver) : !inherit_active_record_base?(node)

  range_of_all_method = offense_range(node)
  add_offense(range_of_all_method) do |collector|
    collector.remove(range_of_all_method)
    collector.remove(node.parent.loc.dot)
  end
end