Use a custom cop to check controller action names

SpecificActionNames cop taken from https://github.com/rubocop/rubocop-rails/pull/827
This commit is contained in:
Andy Allan 2024-10-16 19:49:22 +01:00
parent 7b19ba580c
commit 0edac40638
2 changed files with 109 additions and 0 deletions

View file

@ -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'

View file

@ -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 (%<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