Module: Charming::Controller::ActionHooks::ClassMethods

Defined in:
lib/charming/controller/action_hooks.rb

Instance Method Summary collapse

Instance Method Details

#action_hooksObject

All registered hooks, inherited from superclass.



38
39
40
# File 'lib/charming/controller/action_hooks.rb', line 38

def action_hooks
  @action_hooks ||= superclass.respond_to?(:action_hooks) ? superclass.action_hooks.dup : []
end

#after_action(method_name, only: nil, except: nil) ⇒ Object

Registers an after hook. Runs after the action even if the action rendered early.



22
23
24
# File 'lib/charming/controller/action_hooks.rb', line 22

def after_action(method_name, only: nil, except: nil)
  action_hooks << {type: :after, method: method_name, only: normalize_filter(only), except: normalize_filter(except)}
end

#around_action(method_name, only: nil, except: nil) ⇒ Object

Registers an around hook. The hook method must yield to invoke the action.



27
28
29
# File 'lib/charming/controller/action_hooks.rb', line 27

def around_action(method_name, only: nil, except: nil)
  action_hooks << {type: :around, method: method_name, only: normalize_filter(only), except: normalize_filter(except)}
end

#before_action(method_name, only: nil, except: nil) ⇒ Object

Registers a before hook that runs before the given actions (or all actions when only: is omitted). except: excludes specific actions.



17
18
19
# File 'lib/charming/controller/action_hooks.rb', line 17

def before_action(method_name, only: nil, except: nil)
  action_hooks << {type: :before, method: method_name, only: normalize_filter(only), except: normalize_filter(except)}
end

#rescue_from(*classes, with:) ⇒ Object

Registers an exception handler. When an action raises an exception matching klass (or any of classes), the controller calls with: instead of propagating.



33
34
35
# File 'lib/charming/controller/action_hooks.rb', line 33

def rescue_from(*classes, with:)
  rescue_handlers << {classes: classes.flatten, with: with}
end

#rescue_handlersObject

All registered rescue handlers, inherited from superclass.



43
44
45
# File 'lib/charming/controller/action_hooks.rb', line 43

def rescue_handlers
  @rescue_handlers ||= superclass.respond_to?(:rescue_handlers) ? superclass.rescue_handlers.dup : []
end