Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class UsersController < ApplicationController
def create
CreateUserService.new.call(
params[:user],
on_success: public_method(:create_successful),
on_failure: public_method(:create_failure)
)
end
def create_successful(user)
flash[:notice] ="The user was created successfully"
redirect_to :index
end
def create_failure(invalid_user)
render 'new', locals: { user: invalid_user }
end
end
class CreateUserService
attr_reader :user_factory
def initialize(user_factory: User.public_method(:new))
@user_factory = user_factory
end
def call(attributes: {}, on_success: proc{}, on_failure: proc{})
user = user_factory.call attributes
user.save ? on_success[user] : on_faiulre[user]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment