Question regarding best practices
Hey guys,
This isn't a bug, but rather a couple of questions. Would it be considered bad form for one Interactor to call another Interactor, or an Organizer? Lastly, is it OK to call an Interactor/Organizer from an ActiveRecord model, ActiveJob, or a Rake task?
Hello! Thanks for writing with your question!
Regarding calling interactors from other interactors, not bad form at all! That's completely conventional. When it comes down to it, that's exactly what an organizer does! In fact, if you have some branching logic inside an interactor that diverts your context to yet another interactor, the context keeps track of all of the interactors that have acted upon it. That way, if you roll back, it will unwind that chain and call the rollback methods for all of those interactors. This was an intentional design choice made specifically so people could easily call interactors from within other interactors. So go for it!
Next up… where to call interactors. This is also a great question. The purpose of interactors is to encapsulate your business logic, meaning the high level stuff of what your app does. For that reason, it makes perfect sense to call interactors from wherever a person or some automation interacts with the app. In a Rails app, that's commonly a controller action that an external user has sent a HTTP request to. But that could also be a rake task that an internal admin runs periodically. It could also be a background job that runs as a result of a user-initiated action. All of those make sense to me. The one location that you listed that I'd think twice about is from an Active Record model. Your app's models hold logic pertaining to validation, protection, and handling of your data. Your models are low level. Your interactors are high level. It makes sense to me that your interactors would call out to your models to get things done, but not the other way around.
I hope that all makes sense and is helpful. Please feel free to continue the conversation here despite me closing the issue. Thank you!
That was a really great explanation, and makes total sense. Thank you for your response!
I do have one other question, if I may. Is there a preferred way of encapsulating multiple Interactors within one ActiveRecord transaction so that no database changes are committed, where using the provided rollback function may not make the most sense?
Anywhere you use your interactor(s), you can use the call! method which will raise an error instead of returning a failed context if anything goes south. That's helpful in cases like running interactors from command line tasks or doing what you're describing.
ActiveRecord::Base.transaction do
SomeInteractor.call!(hello: "world")
end
Because call! will raise an exception on failure, the Active Record transaction will roll back. Of course, you could also rescue that exception if needed. Alternatively, you could do something like this if you still need to interact with the result of the interactor.
ActiveRecord::Base.transaction do
result = SomeInteractor.call(hello: "world")
if result.success?
# TODO: do something with result
else
raise ActiveRecord::Rollback
end
end
Jumping in...: This really was a great Q&A!
I've been using this gem for 4 years now and I've been asking myself those questions many times. While I have been doing all recommendations you, @laserlemon, are doing, reading you confirming best practices is really a relief.
I'm sure a lot of people could benefit from this exchange, not sure how you could make it more visible (Leveraging the wiki with the best practices written down, maybe?)
Anyway, thanks! 🙏