Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cop to standardrb to enforce Mod::ModeratorController #1393 #1405

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .custom_cops.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require:
- ./lib/custom_cops/inherits_moderator_controller.rb
# to use additional cops, list individual ruby files here
# (listing directories or glob patterns doesn't seem to work)
# ex:
# - ./lib/custom_cops/<cop-filename>.rb

CustomCops/InheritsModeratorController:
Enabled: true
Include:
- "app/controllers/mod/**/*.rb"
3 changes: 3 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -20,3 +20,6 @@ plugins:
# - use_form_with:
# require_path: extras/prohibit_form_for_and_form_tag
# plugin_class_name: RuboCop::Cop::Style::DisallowFormForandFormTag

extend_config:
- .custom_cops.yml
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ class Application < Rails::Application
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w[assets tasks])
config.autoload_lib(ignore: %w[assets custom_cops tasks])

# Configuration for the application, engines, and railties goes here.
#
47 changes: 47 additions & 0 deletions lib/custom_cops/inherits_moderator_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "active_support/core_ext/string" # String.underscore

module CustomCops
class InheritsModeratorController < RuboCop::Cop::Base
MOD_DIRECTORY = "app/controllers/mod/"
MOD_CONTROLLER = "Mod::ModeratorController"
MSG = "All controllers in the Mod namespace should inherit from #{MOD_CONTROLLER}"

def on_class(node)
rel_path = RuboCop::PathUtil.relative_path(filename(node))

# make sure we're in the right subdirectory
return unless rel_path.start_with?(MOD_DIRECTORY)

class_name = node.identifier.const_name
# We only care about the class if it matches the expected name for the current filename.
# If the class name and filepath don't match, Zeitwerk should complain.
return unless rel_path.end_with?(class_name.underscore + ".rb")

parent_module_name = node.parent_module_name

full_class_name = case parent_module_name
when "Object", ""
class_name
else
"#{parent_module_name}::#{class_name}"
end

return if full_class_name == MOD_CONTROLLER

parent_class = node.parent_class&.const_name || ""

if parent_class == MOD_CONTROLLER ||
(parent_module_name.split("::").include?("Mod") && "Mod::#{parent_class}" == MOD_CONTROLLER)
return
end

add_offense(node)
end

def filename(node)
node.location.expression.source_buffer.name
end
end
end