Created
January 3, 2013 08:26
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.