Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Rectify::Command as a simple command after upgrade to 0.12.0 #49

Open
Hirurg103 opened this issue Aug 6, 2018 · 4 comments

Comments

@Hirurg103
Copy link

Hirurg103 commented Aug 6, 2018

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

@andypike
Copy link
Owner

andypike commented Oct 17, 2018

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

@Hirurg103
Copy link
Author

Hirurg103 commented Oct 17, 2018

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

@andypike
Copy link
Owner

That's an interesting idea, thanks. I'll have a think about that 👍

@Hirurg103
Copy link
Author

@andypike great!

If you approve this idea I could add a PR for it. Just let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants