Cannot use Rectify::Command as a simple command after upgrade to 0.12.0
Hello!
In some cases I used to use Rectify::Command as a simple command like this
@subscriptions = Subscriptions::BuildFor.(current_user)
But after upgrade to 0.12.0 it started to return hash instead of value returned by Users::BuildFor#call
Could you change Rectify::Command.call to return value provided by Rectify::Command#call?
Or I need to change my codebase?
Thanks
This is an intended change. Commands now return a hash of the events that were broadcast inside the #call method. As commands are designed to use events to communicate results this allows for normally calling of #call as well as using a block to handle events. For example, you can do this as before:
RegisterAccount.call(@form) do
on(:ok) { |account| redirect_to account_dashboard_path(account) }
on(:invalid) { render :new }
on(:already_registered) { redirect_to login_path }
end
But now you can also do:
events = RegisterAccount.call(@form)
Where events is a hash where keys are the event names and the value is the arguments:
{
:ok => account
}
This in effect allows multiple return values and allows you switch on a key existing:
events = RegisterAccount.call(@form)
if events.key?(:ok)
redirect_to account_dashboard_path(events[:ok])
end
This just adds flexibility to how you can call commands and handle the outcomes. If you want to return of #call then it sounds like you aren't using the built-in events and so maybe just a normal Ruby class is what you need?
Thanks
Andy
Hi Andy,
thanks for your response.
If you want to return of #call then it sounds like you aren't using the built-in events and so maybe just a normal Ruby class is what you need?
Yes, you are right. In some cases I do not use events so I replaced rectify with the SimpleCommand gem
What do you think about adding #result method to the value returned by rectify command without changing the hash interface? #result will return value generated by #call method. Like this
register = RegisterAccount.call(@form)
if register.key?(:ok)
register.result
end
That's an interesting idea, thanks. I'll have a think about that :+1:
@andypike great!
If you approve this idea I could add a PR for it. Just let me know!