Last active
December 23, 2015 07:18
-
-
Save cored/6599407 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def create | |
@user = User.new params[:user] | |
return render 'new' unless @user.save | |
flash[:notice] = "The user was created succesfully" | |
redirect_to :index | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def create | |
CreateUser.new(User).call( | |
params[:user], | |
on_success: ->(user) do | |
flash[:notice] ="The user was created successfully" | |
redirect_to :index | |
end, | |
on_failure: ->(user) do | |
render 'new' | |
end | |
end | |
end | |
class CreateUser | |
def initialize(users_repository) | |
@users_repository = users_repository | |
end | |
def call(attributes: {}, on_success: nil, on_failure: nil) | |
user = @users_repository.new attributes | |
return on_faiulre[user] unless user.save | |
on_success[user] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like how you inject
User
into theCreateUser
model, rather than using it directly inCreateUser#call
. In the codebase I have at work, I might default the argument in theCreateUser
constructor so that client code doesn't need to worry about the collaborators.However, I can definitely see why you wouldn't want to do that, so that CreateUser isn't tightly coupled to User.