class RuboCop::Cop::Rails::UniqBeforePluck


false positives.
Autocorrect is disabled by default for this cop since it may generate
instance.assoc.pluck(:id).uniq
# an association on an instance will return a CollectionProxy
Model.where(…).pluck(:id).uniq
# this will return a Relation that pluck is called on
@example
vs a call to pluck on an ActiveRecord::Associations::CollectionProxy.
cannot distinguish between calls to pluck on an ActiveRecord::Relation
uniq are added as offenses. This may lead to false positives as the cop
When the EnforcementMode is aggressive then all calls to pluck before
(i.e. a model class) before uniq are added as offenses.
is conservative (the default) then only calls to pluck on a constant
This cop has two different enforcement modes. When the EnforcementMode
Model.uniq.pluck(:id)
# good
Model.pluck(:id).uniq
# bad
@example
the database.
The use of uniq before pluck is preferred because it executes within
Prefer the use of uniq (or distinct), before pluck instead of after.

def autocorrect(node)

def autocorrect(node)
  send_pluck, method, *_args = *node
  lambda do |corrector|
    corrector.remove(dot_method_with_whitespace(method, node))
    corrector.insert_before(send_pluck.loc.dot.begin, ".#{method}")
  end
end

def dot_method_begin_pos(method, node)

def dot_method_begin_pos(method, node)
  lines = node.source.split(NEWLINE)
  if lines.last.strip == ".#{method}"
    node.source.rindex(NEWLINE)
  else
    node.loc.dot.begin_pos
  end
end

def dot_method_with_whitespace(method, node)

def dot_method_with_whitespace(method, node)
  Parser::Source::Range.new(node.loc.expression.source_buffer,
                            dot_method_begin_pos(method, node),
                            node.loc.selector.end_pos)
end

def mode

def mode
  @mode ||= cop_config['EnforcedMode'].to_sym
end

def on_send(node)

def on_send(node)
  method = if mode == :conservative
             conservative_node_match(node)
           else
             aggressive_node_match(node)
           end
  add_offense(node, :selector, format(MSG, method)) if method
end