Reputation: 583
I am struggling with a little feature in the Interactor gem (from our friends at Collective idea).
What I want to do is to have a flash message in my interactor, just as what I would have in my controller.
Here is my create method in my controller, where I instantiate my interactor :
def create
buy_credits = BuyCredit.new(send_transactions_params)
buy_credits.perform
redirect_to profile_path
end
And here is the build of my interactor :
def build_transaction
if context[:transaction][:recipient_id].nil? && context[:transaction][:recipient_type].nil?
@transaction = user.transactions.build(context[:transaction])
# flash[:success] = I18n.t('.bought_credits_for_client', recipient: user)
elsif context[:transaction][:recipient_id].present? && context[:transaction][:recipient_type] == Organisation.name
current_organisation = user.organisations.find(context[:transaction][:recipient_id])
@transaction = current_organisation.transactions.build(context[:transaction])
# flash[:success] = I18n.t('.bought_credits_for_organisation', recipient: current_organisation)
else
# flash[:error] = I18n.t('.cant_buy_credits')
end
end
You can see the flash message type I would like to have, they are the commented lines.
Of course, I could have something like
if interactor.success?
redirect_to profile_path
else
in my controller, but as you can see in my commented lines, I have two "types" of success...
Thanks!
Upvotes: 1
Views: 427
Reputation: 583
Thanks to another colleague help, we managed to have a workaround this : we just pass the flash object (instantiated in the controller) to the context, which the Interactor receive. So the interactor add its messages to the object, which is available afterwards in the controller.
Here is my context construction :
def send_transactions_params
params_to_send = {
transaction: transaction_params,
buyer: current_user,
flash: flash
}
params_to_send
end
Upvotes: 1