collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Migration from mess of observers to interactors #95

Closed mingan closed 9 years ago

mingan commented 9 years ago

We've adopted interactors for new features some time ago but our codebase still contains a mess of observers which work but nobody exactly knows how. I want to cut through this for both readability and as a preparation for the feared upgrade to Rails 4.

I can see several ways where a set of interactors combines in a few organizers can simplify the different create/update scenarios, clean up the code and speed things up. My problem is this: there are a few operations triggered in after_destroy callbacks, they queue rčecompilation of data for reports and notify external services. OK, so let's move this in an interactor called DestroyX and be done with it. But these changes are often chained together by model associations with dependent; the longest chain I could find goes from A has many B is a tree of Bs has many C has many D. It's simple to handle the deletion of D with an interactor, but how do I handle deletion of A?

The only way I see is to get rid of the dependent option and re-implement the whole thing using interactors which sounds wasteful and sort of all-in approach instead of step-by-step changes. Am I missing something obvious here?

laserlemon commented 9 years ago

Ooh, I don't know how well Interactor will solve the problems that observers "solve." I think that keeping your observers is fine as long as they're operating on data. If you find that your observers are sending emails, making third-party API calls, or other business logic, that's probably a good sign that those operations should be moved out of the observers. If on the other hand, the observers are simply maintaining data integrity, I probably wouldn't do much to change them.

Without knowing more (and possibly even if I knew more) about your application, it's hard to say what the right way forward for you would be. But I don't think you're missing anything obvious. I think you just have a tricky migration on your hands!

I hope that helps!

mingan commented 9 years ago

Thank you for your answer and giving me the ok, makes me feel slightly less worried.