69 lines
1.8 KiB
Ruby
69 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module RuboCop
|
|
module Cop
|
|
module Rails
|
|
# Use only specific action names.
|
|
#
|
|
# It is good practice to separate controller classes rather than adding more actions as needed.
|
|
# By default, the 7 CRUD action names are specified that are generated by the Rails scaffold.
|
|
#
|
|
# @example
|
|
# # bad
|
|
# class UsersController < ApplicationController
|
|
# def articles
|
|
# end
|
|
# end
|
|
#
|
|
# # good
|
|
# class UserArticlesController < ApplicationController
|
|
# def index
|
|
# end
|
|
# end
|
|
class SpecificActionNames < Base
|
|
include VisibilityHelp
|
|
|
|
MSG = "Use only specific action names."
|
|
|
|
# @param node [RuboCop::AST::DefNode]
|
|
# @return [void]
|
|
def on_def(node)
|
|
return unless bad?(node)
|
|
|
|
add_offense(
|
|
node.location.name,
|
|
:message => format(
|
|
"Use only specific action names (%<action_names>s).",
|
|
:action_names => configured_action_names.join(", ")
|
|
)
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
# @param node [RuboCop::AST::DefNode]
|
|
# @return [Boolean]
|
|
def action?(node)
|
|
node_visibility(node) == :public
|
|
end
|
|
|
|
# @param node [RuboCop::AST::DefNode]
|
|
# @return [Boolean]
|
|
def bad?(node)
|
|
action?(node) && !configured_action_name?(node)
|
|
end
|
|
|
|
# @param node [RuboCop::AST::DefNode]
|
|
# @return [Boolean]
|
|
def configured_action_name?(node)
|
|
configured_action_names.include?(node.method_name.to_s)
|
|
end
|
|
|
|
# @return [Array<String>]
|
|
def configured_action_names
|
|
cop_config["ActionNames"]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|