Skip to content

Instantly share code, notes, and snippets.

@Spaceghost
Forked from tbuehlmann/user_creator.rb
Created June 28, 2014 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spaceghost/80a17711909da8abe898 to your computer and use it in GitHub Desktop.
Save Spaceghost/80a17711909da8abe898 to your computer and use it in GitHub Desktop.
class UserCreator
def initialize(listener)
@listener = listener
end
def create(attributes)
user = User.new(attributes)
if user.save
# send email
# do things
@listener.creation_successful
else
@listener.creation_unsuccessful(user)
end
end
end
class UsersController < ApplicationController
def create
user_creater = UserCreator.new(self)
user_creater.create(user_params)
end
def creation_successful
redirect_to users_url, notice: 'Yay.'
end
def creation_unsuccessful(user)
@user = user
render :new
end
hide_action :creation_successful, :creation_unsuccessful
private
def user_params
params.require(:user).permit(:foo, :bar)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment