Class: RuboCop::Cop::Cobra::ControllerFilePlacement

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
FilePlacementHelp
Defined in:
lib/rubocop/cop/cobra/controller_file_placement.rb

Overview

This cop disallows adding global controllers to the ‘app/controllers` directory.

The goal is to encourage developers to put new controllers inside the correct namespace, where they can be more modularly isolated and ownership is clear.

Examples:

# bad
# path: components/my_component/app/controllers/foo_controller.rb
class FooController < ApplicationController
  # ...
end

# good
# path: components/my_component/app/controllers/my_component/foo_controller.rb
module MyComponent
  class FooController < MyComponent::ApplicationController
    # ...
  end
end

Constant Summary

Constants included from FilePlacementHelp

FilePlacementHelp::FILE_PLACEMENT_MSG

Instance Method Summary collapse

Methods included from FilePlacementHelp

#applicable_component_path?, #file_placement_msg, #namespaced_correctly?

Instance Method Details

#investigate(processed_source) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/cobra/controller_file_placement.rb', line 29

def investigate(processed_source)
  return if processed_source.blank?

  path = processed_source.file_path
  return unless applicable_component_path?(path, controllers_path)

  if path.include?(controller_concerns_path)
    return if namespaced_correctly?(path, controller_concerns_path)

    add_offense(processed_source.ast,
                message: file_placement_msg(path, controller_concerns_path))
  end
  return if namespaced_correctly?(path, controllers_path)

  add_offense(processed_source.ast,
              message: file_placement_msg(path, controllers_path))
end