Class: RuboCop::Cop::Guardrails::StandardAppDirectories

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/guardrails/standard_app_directories.rb

Overview

Prevents non-standard directories under ‘app/`.

Rails provides a conventional directory structure. Custom directories like ‘app/services/`, `app/decorators/`, or `app/form_objects/` scatter domain logic that belongs in `app/models/`.

The ‘AllowedDirectories` option lists the standard Rails directories. Add to it if your project has a legitimate need for additional directories.

Examples:

# bad — app/services/post_publisher.rb
class PostPublisher
  def call
    post.update!(published: true)
  end
end

# bad — app/decorators/post_decorator.rb
class PostDecorator < SimpleDelegator
end

# good — app/models/post/publisher.rb
class Post::Publisher
  def call
    post.update!(published: true)
  end
end

Constant Summary collapse

MSG =
'Non-standard app directory `%<directory>s`. Keep domain logic in `app/models/`.'
DEFAULT_DIRECTORIES =
%w[
  channels
  controllers
  helpers
  jobs
  mailers
  mailboxes
  models
  views
].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



49
50
51
52
53
54
# File 'lib/rubocop/cop/guardrails/standard_app_directories.rb', line 49

def on_class(node)
  dir = app_subdirectory
  if dir && !allowed_directories.include?(dir)
    add_offense(node.loc.name, message: format(MSG, directory: dir))
  end
end