lib/rubocop/cop/rails/uniq_before_pluck.rb in rubocop-rails-2.5.2 vs lib/rubocop/cop/rails/uniq_before_pluck.rb in rubocop-rails-2.6.0
- old
+ new
@@ -1,54 +1,55 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
- # Prefer the use of uniq (or distinct), before pluck instead of after.
+ # Prefer the use of distinct, before pluck instead of after.
#
- # The use of uniq before pluck is preferred because it executes within
+ # The use of distinct before pluck is preferred because it executes within
# the database.
#
# This cop has two different enforcement modes. When the EnforcedStyle
# is conservative (the default) then only calls to pluck on a constant
- # (i.e. a model class) before uniq are added as offenses.
+ # (i.e. a model class) before distinct are added as offenses.
#
# When the EnforcedStyle is aggressive then all calls to pluck before
- # uniq are added as offenses. This may lead to false positives as the cop
- # cannot distinguish between calls to pluck on an ActiveRecord::Relation
- # vs a call to pluck on an ActiveRecord::Associations::CollectionProxy.
+ # distinct are added as offenses. This may lead to false positives
+ # as the cop cannot distinguish between calls to pluck on an
+ # ActiveRecord::Relation vs a call to pluck on an
+ # ActiveRecord::Associations::CollectionProxy.
#
# Autocorrect is disabled by default for this cop since it may generate
# false positives.
#
# @example EnforcedStyle: conservative (default)
# # bad
- # Model.pluck(:id).uniq
+ # Model.pluck(:id).distinct
#
# # good
- # Model.uniq.pluck(:id)
+ # Model.distinct.pluck(:id)
#
# @example EnforcedStyle: aggressive
# # bad
# # this will return a Relation that pluck is called on
- # Model.where(cond: true).pluck(:id).uniq
+ # Model.where(cond: true).pluck(:id).distinct
#
# # bad
# # an association on an instance will return a CollectionProxy
- # instance.assoc.pluck(:id).uniq
+ # instance.assoc.pluck(:id).distinct
#
# # bad
- # Model.pluck(:id).uniq
+ # Model.pluck(:id).distinct
#
# # good
- # Model.uniq.pluck(:id)
+ # Model.distinct.pluck(:id)
#
class UniqBeforePluck < RuboCop::Cop::Cop
include ConfigurableEnforcedStyle
include RangeHelp
- MSG = 'Use `%<method>s` before `pluck`.'
+ MSG = 'Use `distinct` before `pluck`.'
NEWLINE = "\n"
PATTERN = '[!^block (send (send %<type>s :pluck ...) ' \
'${:uniq :distinct} ...)]'
def_node_matcher :conservative_node_match,
@@ -64,19 +65,18 @@
aggressive_node_match(node)
end
return unless method
- add_offense(node, location: :selector,
- message: format(MSG, method: method))
+ add_offense(node, location: :selector)
end
def autocorrect(node)
lambda do |corrector|
method = node.method_name
corrector.remove(dot_method_with_whitespace(method, node))
- corrector.insert_before(node.receiver.loc.dot.begin, ".#{method}")
+ corrector.insert_before(node.receiver.loc.dot.begin, '.distinct')
end
end
private