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

Allows passing a block to be executed when command succeeded #20

Conversation

jairovm
Copy link

@jairovm jairovm commented Aug 22, 2019

Hi guys!

This small change is aimed to simplify controller logic, moving from something like this:

  def create
    command = AuthenticateUser.call(session_params[:email], session_params[:password])

    if command.success?
      redirect_to root_path
    else
      render :new
    end
  end

To this

  def create
    AuthenticateUser.call(session_params[:email], session_params[:password]) do
      # return keyword is key here to avoid `duplicate render/redirect error`
      return redirect_to root_path      
    end

    # executed only when command fails
    render :new
  end

The block passed to Command.call will be executed only when command is success

@kennyadsl
Copy link
Member

Personally, I'm not sure about this. To save an if statement we are introducing a behavior that is not intuitive since you always have to remember to use return at the end of the block.

Also, there could be circumstances when you may want to not return since you have other code to be executed, for example, something that should happen after both successful and failing commands.

@jairovm
Copy link
Author

jairovm commented Sep 4, 2019

@kennyadsl return keyword is only needed when you want to stop execution like might be the case when dealing with Controllers.

I agree with you, my example might not be the best one and remembering to use return adds some complexity.

But I think on this like adding a shortcut when you want something to be executed on success only and dont care about failures ( it might be useful when writing tests ).

AuthenticateUser.call(session_params[:email], session_params[:password]) do |command|
  do_something_with_command_object(command)
end

Passing a block is optional so if else way is still possible, besides doing something after command gets executed.

command = AuthenticateUser.call(session_params[:email], session_params[:password])

do_something_regardless_of_status(command)

@jairovm jairovm closed this Nov 5, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants