Talking to a Rails CTO last night about loosely coupling business logic in Rails. My way of doing this is using interactors. https://github.com/collectiveidea/interactor
Interactors are a ruby implementation of the command pattern. I like them for a couple reasons.
- They’re vanilla ruby classes that wrap unhandled exceptions in a great abstraction
- You always get a return value that makes sense, upon which you can check for success or failure
- You can implement an “up” and “down” for every interactor as you would a database migration to support both undo functionality and to address unhandled exceptions
- You can build up a pipeline of interactors into an “organizer” – this is powerful
- It’s very easy to write tests for interactors
- At the end of the day, IMO the most important architectural decision is to keep your business logic separated from the framework code. Even if it’s a mess, just separate the mess from the framework. Interactors are a great way to do that.