r/ruby • u/websebdev • Apr 10 '21
💎Ruby tip: Make your own callbacks using ActiveSupport::Callbacks
Do you know about the callbacks available in Rails controllers, models, mailboxes, action cable, middleware, etc? It's all using ActiveSupport's Callbacks module to do that. Well, you can make your own callbacks with it too!
Rough example on how to use the module
More complex, real-world example: StimulusReflex callbacks PR
34
Apr 10 '21
Don't use callbacks, it's more indirection and makes code harder to read and test. Cool that the option exists for framework or library authors but you don't need to be using every shiny thing.
10
u/whitet73 Apr 10 '21
Came here to say this. Less callbacks, not more. That said I do appreciate the write-up
-9
3
3
10
u/[deleted] Apr 10 '21
You can achieve same result with more simpler code than callback. I don't see the point of using callback here, callback are hard to debug, hard to test, vs a simple method taking a block.