collectiveidea / interactor

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

Add passing context between interactors #209

Closed dixent closed 2 years ago

dixent commented 2 years ago

Problem

I have an interactor that calls another interactor. So the situation is:

class MainInteractor
  def call
     ...
     result = SubInteractor.call(foo1: 'bar1', foo2: 'bar2', foo3: 'bar3')
     if result. failure?
         context.fail!(foo1: result.foo1, foo2: result.foo2, foo3: result.foo3) # or context.fail!(result.to_h)
     else
        ...
     end
     ...
  end
end

It is so annoying to write result.to_h all time and looks dirty in the service.

Aprouch

Instead of passing the result by key/value or hash. I suggest parsing context={} to Hash. It helps to pass failed context between interactors.

Proc and Cons

Proc:

  1. In the situation when the interactor has two or more levels of subcontractors. This is easy to pass a failed context and push it to the top.

Cons:

  1. If you pass context all time, result context of top-level interactor will contain a lot of context attributes from all sub-interactors.