Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Last active August 29, 2015 14:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tbuehlmann/283af18112a513b099f4 to your computer and use it in GitHub Desktop.
Save tbuehlmann/283af18112a513b099f4 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