Class: RuboCop::Cop::Rails::ActionOrder

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
DefNode, RangeHelp, VisibilityHelp
Defined in:
lib/rubocop/cop/rails/action_order.rb

Overview

Enforces consistent ordering of the standard Rails RESTful controller actions.

The cop is configurable and can enforce any ordering of the standard actions. All other methods are ignored. So, the actions specified in ‘ExpectedOrder` should be defined before actions not specified.

source,yaml

Rails/ActionOrder:
  ExpectedOrder:
    - index
    - show
    - new
    - edit
    - create
    - update
    - destroy

Examples:

# bad
def index; end
def destroy; end
def show; end

# good
def index; end
def show; end
def destroy; end

Constant Summary collapse

MSG =
'Action `%<current>s` should appear before `%<previous>s`.'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rails/action_order.rb', line 45

def on_class(node)
  action_declarations(node, actions).each_cons(2) do |previous, current|
    next if node_visibility(current) != :public || non_public?(current)
    next if find_index(current) >= find_index(previous)

    register_offense(previous, current)
  end
end