Module: Karafka::Web::Ui::Controllers::Requests::Hookable
- Included in:
- BaseController
- Defined in:
- lib/karafka/web/ui/controllers/requests/hookable.rb
Overview
Adds before/after hook support for controller methods.
This module allows registering callbacks that run before and after
any named method (e.g., call, show) or for all methods.
Hooks are inherited from parent controllers, making it easy to define shared behavior across a hierarchy.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
Hook into class inclusion to extend DSL.
Instance Method Summary collapse
-
#run_after_hooks(action_name) ⇒ Object
Run all after hooks matching the action.
-
#run_before_hooks(action_name) ⇒ Object
Run all before hooks matching the action.
Class Method Details
.included(base) ⇒ Object
Hook into class inclusion to extend DSL
33 34 35 |
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 33 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#run_after_hooks(action_name) ⇒ Object
Run all after hooks matching the action
94 95 96 97 98 |
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 94 def run_after_hooks(action_name) self.class.after_hooks.each do |actions, block| instance_exec(&block) if actions.empty? || actions.include?(action_name) end end |
#run_before_hooks(action_name) ⇒ Object
Run all before hooks matching the action
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 80 def run_before_hooks(action_name) # Track the dispatched action so controllers can resolve per-action configuration # (e.g. `filterable_attributes`) from within the hooks and their action body, before # `render` is ever reached @current_action_name = action_name self.class.before_hooks.each do |actions, block| instance_exec(&block) if actions.empty? || actions.include?(action_name) end end |