From: Andy Allan Date: Wed, 16 Oct 2024 18:49:22 +0000 (+0100) Subject: Use a custom cop to check controller action names X-Git-Tag: live~172^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/0edac40638b82a87ae1528bd89d3b81388dcbaca?ds=inline;hp=--cc Use a custom cop to check controller action names SpecificActionNames cop taken from https://github.com/rubocop/rubocop-rails/pull/827 --- 0edac40638b82a87ae1528bd89d3b81388dcbaca diff --git a/.rubocop.yml b/.rubocop.yml index 60d144544..d1b961dd5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,6 +7,7 @@ require: - rubocop-performance - rubocop-rails - rubocop-rake + - ./.rubocop/specific_action_names.rb AllCops: TargetRubyVersion: 3.1 @@ -110,3 +111,42 @@ Style/StringLiterals: 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' diff --git a/.rubocop/specific_action_names.rb b/.rubocop/specific_action_names.rb new file mode 100644 index 000000000..e78790bac --- /dev/null +++ b/.rubocop/specific_action_names.rb @@ -0,0 +1,69 @@ +# 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 (%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] + def configured_action_names + cop_config["ActionNames"] + end + end + end + end +end