Class: RuboCop::Cop::Sevencop::RailsRouteAs

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sevencop/rails_route_as.rb

Overview

Always use as option on routing methods.

Examples:

# bad
delete "/users/:id" => "users#destroy"
get    "/users/:id" => "users#show"

# good
delete "/users/:id" => "users#destroy", as: "user"
get    "/users/:id" => "users#show", as: nil

Constant Summary collapse

MSG =
"Always use `as` option on routing methods. Use `as: nil` if you don't need named routes."
RESTRICT_ON_SEND =
i[
  delete
  get
  head
  patch
  post
  put
].freeze

Instance Method Summary collapse

Instance Method Details

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

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


30
31
32
33
34
35
36
# File 'lib/rubocop/cop/sevencop/rails_route_as.rb', line 30

def on_send(node)
  last_argument = node.last_argument
  return unless last_argument&.hash_type?
  return if last_argument.pairs.any? { |pair| pair.key.value == :as }

  add_offense(node)
end