Class: RuboCop::Cop::ViewComponent::NoGlobalState
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::ViewComponent::NoGlobalState
- Includes:
- Base
- Defined in:
- lib/rubocop/cop/view_component/no_global_state.rb
Overview
Prevents direct access to global state within ViewComponent classes.
ViewComponent’s own documentation notes that accessing ‘request` directly “introduces coupling that inhibits encapsulation & reuse, often making testing difficult.” The same principle applies to `params`, `session`, `cookies`, and `flash`.
Constant Summary collapse
- MSG =
"Avoid accessing `%<method>s` directly in ViewComponents. " \ "Pass necessary data through the constructor instead."
- GLOBAL_STATE_METHODS =
%i[ params request session cookies flash ].freeze
- RESTRICT_ON_SEND =
GLOBAL_STATE_METHODS
Instance Method Summary collapse
Methods included from Base
#component_namespaces, #enclosing_class, #inside_view_component?, #view_component_class?, #view_component_parent?
Instance Method Details
#on_send(node) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/rubocop/cop/view_component/no_global_state.rb', line 52 def on_send(node) return unless inside_view_component?(node) method_name = global_state_access?(node) return unless method_name add_offense(node, message: format(MSG, method: method_name)) end |