class RuboCop::Cop::Rails::UniqBeforePluck

Prefer the use of uniq before pluck instead of after.

The use of uniq before pluck is preferred because it executes within the database.

@example

# bad
Model.where(...).pluck(:id).uniq

# good
Model.where(...).uniq.pluck(:id)

Constants

DOT_UNIQ
MSG
NEWLINE

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/rails/uniq_before_pluck.rb, line 35
def autocorrect(node)
  lines = node.source.split(NEWLINE)
  begin_remove_pos = if lines.last.strip == DOT_UNIQ
                       node.source.rindex(NEWLINE)
                     else
                       node.loc.dot.begin_pos
                     end
  receiver = node.children.first

  lambda do |corrector|
    corrector.remove(
      Parser::Source::Range.new(node.loc.expression.source_buffer,
                                begin_remove_pos,
                                node.loc.selector.end_pos)
    )
    corrector.insert_before(receiver.loc.dot.begin, DOT_UNIQ)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rails/uniq_before_pluck.rb, line 23
def on_send(node)
  receiver, method_name, *_args = *node

  unless method_name == :uniq &&
         !receiver.nil? &&
         receiver.send_type? &&
         receiver.children[1] == :pluck
    return
  end
  add_offense(node, :selector, MSG)
end