Class: RuboCop::Cop::Metz::ControllersTooManyDirectCollaborators

Inherits:
Base
  • Object
show all
Extended by:
Metz::CopMetadata
Defined in:
lib/rubocop/cop/metz/controllers_too_many_direct_collaborators.rb

Overview

Flags Rails controller methods that reach into more than MaxCollaborators distinct top-level collaborators. A "direct collaborator" is an application constant referenced inside a method body -- whether bare (User), as the receiver of .new (User.new), or as the receiver of another message (Mailer.confirmation(...)). Exception classes at rescue/raise/fail sites, constants owned by the controller class, and core framework/stdlib constants do not count. Multiple references to the same constant count once. The cop is path-classified through Metz::FileClassifier.controller? and is silent on any file that is not under app/controllers/.

Constant Summary collapse

MSG =
"Controller method `%<method>s` reaches into %<count>d " \
"direct collaborators (%<list>s); " \
"Max is %<max>d. Reduce by funneling work through a single coordinator."

Constants included from Metz::CopMetadata

Metz::CopMetadata::DEFAULT_FIX_SAFETY, Metz::CopMetadata::DEFAULT_SUGGESTED_NEXT_MOVES, Metz::CopMetadata::DEFAULT_WHY_IT_MATTERS

Instance Method Summary collapse

Methods included from Metz::CopMetadata

fix_safety, included, metz_metadata, suggested_next_moves, why_it_matters

Instance Method Details

#on_def(node) ⇒ Object



197
198
199
200
201
202
# File 'lib/rubocop/cop/metz/controllers_too_many_direct_collaborators.rb', line 197

def on_def(node)
  collaborators = collect_collaborators(node)
  return if collaborators.size <= max_collaborators

  report_offense(node, collaborators)
end

#relevant_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
# File 'lib/rubocop/cop/metz/controllers_too_many_direct_collaborators.rb', line 204

def relevant_file?(file)
  return super if file.nil? || file.empty? || file == "(string)"

  !file_name_matches_any?(file, "Exclude", false) &&
    ::Metz::FileClassifier.controller?(file)
end