Class: RuboCop::Cop::Rails::OrderArguments

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/order_arguments.rb

Overview

Prefer symbol arguments over strings in ‘order` method.

Examples:

# bad
User.order('name')
User.order('name DESC')

# good
User.order(:name)
User.order(name: :desc)

Constant Summary collapse

MSG =
'Prefer `%<prefer>s` instead.'
RESTRICT_ON_SEND =
%i[order].freeze
ORDER_EXPRESSION_REGEX =
/\A(\w+) ?(asc|desc)?\z/i.freeze

Instance Method Summary collapse

Instance Method Details

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



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rails/order_arguments.rb', line 33

def on_send(node)
  return unless (current_expressions = string_order(node))
  return unless (preferred_expressions = replacement(current_expressions))

  offense_range = find_offense_range(node)
  add_offense(offense_range, message: format(MSG, prefer: preferred_expressions)) do |corrector|
    corrector.replace(offense_range, preferred_expressions)
  end
end