Interactors

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.

  1. They’re vanilla ruby classes that wrap unhandled exceptions in a great abstraction
  2. You always get a return value that makes sense, upon which you can check for success or failure
  3. 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
  4. You can build up a pipeline of interactors into an “organizer” – this is powerful
  5. It’s very easy to write tests for interactors
  6. 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.