- rubocop-performance
- rubocop-rails
- rubocop-rake
+ - ./.rubocop/specific_action_names.rb
AllCops:
TargetRubyVersion: 3.1
Style/SymbolArray:
EnforcedStyle: brackets
+
+Rails/SpecificActionNames:
+ Description: Use only specific action names.
+ Enabled: true
+ ActionNames:
+ - index
+ - show
+ - new
+ - edit
+ - create
+ - update
+ - destroy
+ Include:
+ - app/controllers/**/*.rb
+ Exclude:
+ # This is a todo list, but is currently too long for `rubocop --auto-gen-config`
+ - 'app/controllers/api/changeset_comments_controller.rb'
+ - 'app/controllers/api/changesets_controller.rb'
+ - 'app/controllers/api/nodes_controller.rb'
+ - 'app/controllers/api/notes_controller.rb'
+ - 'app/controllers/api/old_elements_controller.rb'
+ - 'app/controllers/api/relations_controller.rb'
+ - 'app/controllers/api/user_preferences_controller.rb'
+ - 'app/controllers/api/users_controller.rb'
+ - 'app/controllers/api/ways_controller.rb'
+ - 'app/controllers/browse_controller.rb'
+ - 'app/controllers/changesets_controller.rb'
+ - 'app/controllers/confirmations_controller.rb'
+ - 'app/controllers/diary_comments_controller.rb'
+ - 'app/controllers/diary_entries_controller.rb'
+ - 'app/controllers/directions_controller.rb'
+ - 'app/controllers/errors_controller.rb'
+ - 'app/controllers/export_controller.rb'
+ - 'app/controllers/geocoder_controller.rb'
+ - 'app/controllers/issues_controller.rb'
+ - 'app/controllers/messages_controller.rb'
+ - 'app/controllers/site_controller.rb'
+ - 'app/controllers/traces_controller.rb'
+ - 'app/controllers/users_controller.rb'
--- /dev/null
+# 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