Skip to content

Instantly share code, notes, and snippets.

@kurko
Forked from lucashungaro/gist:3784008
Created September 26, 2012 03:07
Show Gist options
  • Save kurko/3785764 to your computer and use it in GitHub Desktop.
Save kurko/3785764 to your computer and use it in GitHub Desktop.
removing conditional and working with provided callbacks
class UsersController < ApplicationController
def create
@user_creation = UserCreation.new(self)
@user_creation.on_success { redirect_to users_path }
@user_creation.on_failure { render :new }
@user_creation.please_create_an_user_as_admin(params[:user])
end
end
# This could be provided through a block, adding a bit of syntactic sugar:
@user_creation = UserCreation.new(self) do
on_success { redirect_to users_path }
on_failure { render :new }
end
class UsersController < ApplicationController
def create
user = UserCreation.new(self)
user_created = user.create_an_user_as_admin(params[:user])
if user_created
redirect_to users_path
else
@user = user.to_active_record
render :new
end
end
end
# obs.: the .to_active_record is here just to represent the idea that
# we usually send the AR object to the view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment