Class: RuboCop::Cop::Sevencop::RailsRouteOrdered

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/sevencop/rails_route_ordered.rb

Overview

Sort routes by path and HTTP method.

Examples:

# bad
get "/users/:id" => "users#show"
get "/users" => "users#index"

# good
get "/users" => "users#index"
get "/users/:id" => "users#show"

# bad
post "/users" => "users#create"
get  "/users" => "users#index"

# good
get  "/users" => "users#index"
post "/users" => "users#create"

Constant Summary collapse

MSG =
'Sort routes by path and HTTP method.'
RESTRICT_ON_SEND =
%i[
  delete
  get
  head
  options
  patch
  post
  put
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/sevencop/rails_route_ordered.rb', line 43

def on_send(node)
  previous_older_sibling = find_previous_older_sibling(node)
  return unless previous_older_sibling

  add_offense(node) do |corrector|
    corrector.swap(
      range_with_comments_and_lines(previous_older_sibling),
      range_with_comments_and_lines(node)
    )
  end
end