Class: RuboCop::Cop::Cobra::ModelFilePlacement

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

Overview

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

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

Examples:

# bad
# path: components/my_component/app/models/foo.rb
class Foo < ApplicationRecord
  # ...
end

# good
# path: components/my_component/app/models/my_component/foo.rb
module MyComponent
  class Foo < MyComponent::ApplicationRecord
    # ...
  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/model_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, models_path)

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

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

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