Class: RuboCop::Cop::Guardrails::RestfulActions

Inherits:
Base
  • Object
show all
Includes:
VisibilityHelpers
Defined in:
lib/rubocop/cop/guardrails/restful_actions.rb

Overview

Prevents non-RESTful actions in controllers.

Rails advocates for RESTful resources. When you need a custom action, extract a new controller with standard RESTful actions.

Examples:

# bad
class PostsController < ApplicationController
  def publish
  end
end

# good - extract a new resource
class Posts::PublicationsController < ApplicationController
  def create
  end
end

Constant Summary collapse

MSG =
'Non-RESTful action `%<method>s`. Add a new resource to represent this action.'
RESTFUL_ACTIONS =
%i[index show new create edit update destroy].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object



30
31
32
33
34
# File 'lib/rubocop/cop/guardrails/restful_actions.rb', line 30

def on_def(node)
  if !RESTFUL_ACTIONS.include?(node.method_name) && in_class?(node) && public_method?(node)
    add_offense(node.loc.name, message: format(MSG, method: node.method_name))
  end
end