You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
During the discussion in #74 it occurred to me that there maybe an unnecessary fragmentation or friction with the way interactors and their contexts are currently defined.
Proposal
I propose a whole new pattern for interactor definition that would be some what similar to how rake tasks are written in a given project. The idea would be you would define an interactor namespace like :user and within that namespace you would define a set of contexts and a set of interactors like this:
# interactors/user.rbActiveInteractor::InteractorNamespace.new(:user)do# we define a default interactor context# this context would be used by all interactors in this namespace# unless the interactor specifically specifies otherwise contextdoattributes:first_name,:last_name,:email,:userendcontext:user_and_accountdoattributes:account,:uservalidates:account,on: :calledendcontext:create_accountdoattributes:account,:uservalidates:user,presence: true,on: :callingend# defining an interactor would be the equivalent of defining the #perform# method in current interactor classes. # this interactor would use the default context as it doesn't# specify one nor is there a context with a matching nameinteractor:finderdo |context|
context.user=User.find_by(email: context.email,first_name: context.first_name,last_name: context.last_name)end# this interactor would use the default context as it doesn't# specify one nor is there a context with a matching nameinteractor:downcase_emaildo |context|
context.email=context.email&.downcaseend# this interactor has a dependency on another interactorinteractorcreator: %i[downcase_email]do |context|
context.user ||= User.create(email: context.email,first_name: context.first_name,last_name: context.last_name)end# we don't need to specify a context here it will# use the :create_account context by defaultinteractor:create_accountdo |context|
context.account=context.user.generate_account!end# we can define an organizer that specifies a contextorganizer:create_with_account,contextualize_with: :user_and_accountdoorganize:finder,:creator,:create_accountendend
given this pattern we can then just call a single simple api to invoke the interaction like this:
Problem
During the discussion in #74 it occurred to me that there maybe an unnecessary fragmentation or friction with the way interactors and their contexts are currently defined.
Proposal
I propose a whole new pattern for interactor definition that would be some what similar to how rake tasks are written in a given project. The idea would be you would define an interactor namespace like
:user
and within that namespace you would define a set of contexts and a set of interactors like this:given this pattern we can then just call a single simple api to invoke the interaction like this:
additionally we can call a different interactor within that namespace
Additionally we could even provide helper methods for rails
ActionController::Base
so that this:could be refactored to this:
The goal here would be to provide a single api for every interactor in your project something like:
Things to consider before answering
The text was updated successfully, but these errors were encountered: