r/ruby • • Oct 12 '26

Blog post Hanami, Why?: Class Action

https://aaronmallen.me/writing/hanami-why-class-action
27 Upvotes

6 comments sorted by

40

u/AnonyDev01 Oct 12 '26

I thought this was about a lawsuit when I read the subject.

7

u/northrupthebandgeek Oct 12 '26

Meanwhile I thought it'd have something to do with why? the lucky stiff.

2

u/Windglen0 Oct 12 '26

same, had to do a double take on that title

7

u/ryans_bored Oct 12 '26

> "Why is each action its own class?"

I think Rails should adopt this approach for controllers as a default. Working in this paradigm the answers become obvious.

6

u/emptyflask Oct 12 '26

I haven't looked closely into Hanami in a while...

include Deps['foo'] seemed like a strange thing to do, but the docs cleared it up. It's sugar for optional dependency injection. Pretty clever.

4

u/jodosha Hanami author Oct 14 '26

There are few things to know about callbacks in Hanami Actions:

  1. They can be shared across actions. Create a plain old Ruby module and include in the actions that need them. Classic example is authentication.
  2. You can actually skip a callback. Just override the method without implementing it. Example: instead of “skip_before_filter :authenticate!” (Rails), do “def authenticate!; end” in the action that must go w/o authentication.
  3. You can enhance the behavior of an inherited callback. Override the method, use “super” and your custom code.

If you pay closer attention, this is possible not because of Hanami, but because of OOP. Using objects as actions unlocks native Ruby features.