Class: RuboCop::Cop::Metz::ViewsDeepNavigation

Inherits:
DemeterTrainWreck show all
Includes:
OnSendCsendBridge
Defined in:
lib/rubocop/cop/metz/views_deep_navigation.rb

Overview

Flags Rails view templates whose ERB / HAML / SLIM expressions traverse the object graph deeper than MaxChainLength. Reuses Metz/DemeterTrainWreck's chain walker and value-object skip logic, so a chain like name.upcase.strip.split(' ').first (every link a recognised value-object call) stays silent. Path-classified through Metz::FileClassifier.view?: a deep chain inside an .erb file outside app/views/ will not fire. DDR: docs/ddrs/2026-06-24-views-deep-navigation-inherits-demeter.md explains why this specialization uses inheritance instead of composition.

Constant Summary collapse

MSG =
"View object-graph traversal of %<count>d exceeds MaxChainLength (%<max>d). " \
"Push the lookup into the controller or expose it via a presenter."

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 OnSendCsendBridge

included

Methods included from Metz::CopMetadata

#fix_safety, included, #metz_metadata, #suggested_next_moves, #why_it_matters

Instance Method Details

#on_send(node) ⇒ Object

RuboCop's dispatcher needs the subclass to own the callback, while the inherited implementation remains the shared chain analyzer. rubocop:disable Lint/UselessMethodDefinition -- required for subclass callback registration.



37
38
39
# File 'lib/rubocop/cop/metz/views_deep_navigation.rb', line 37

def on_send(node)
  super
end

#relevant_file?(file) ⇒ Boolean

rubocop:enable Lint/UselessMethodDefinition

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/rubocop/cop/metz/views_deep_navigation.rb', line 42

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

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