Skip to content

Instantly share code, notes, and snippets.

@xn
Created January 3, 2013 08:26
Show Gist options
  • Select an option

  • Save xn/4441828 to your computer and use it in GitHub Desktop.

Select an option

Save xn/4441828 to your computer and use it in GitHub Desktop.
The book `Clean Ruby` talks about locus of attention then offers this controller at the end of Chapter 1. To me, my eyes just roll off the create method. There is simply too much going on there to get everything in one gestalt. Here's my refactor:
class ConciseTransfersController < ApplicationController
respond_to :html
def create
respond_with account do |format|
format.html { redirect_to account_transfers_path(account) }
end
end
protected
def account
transfer.execute(params[:amount])
end
def transfer
MoneyTransfer.new(params[:account_id], params[:destination_id])
end
end
class TransfersController < ApplicationController
respond_to :html
def create
transfer = MoneyTransfer.new(params[:account_id], params[:destination_id])
account = transfer.execute(params[:amount])
respond_with account do |format|
format.html { redirect_to account_transfers_path(account) }
end
end
end
@xn
Copy link
Author

xn commented Jan 3, 2013

I'd also change the #execute method to #update_account or something that would inform the developer that something 'account-y' was being returned. I would expect transfer.execute(amount) to return a receipt, not an account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment