Created
April 21, 2015 12:31
-
-
Save novohispano/0014876af32f09624ebf to your computer and use it in GitHub Desktop.
Decorators & Presenters
This file contains 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 AccountPresenter | |
extend Forwardable | |
attr_reader :account | |
def_delegators :account, :amount, :created_at | |
def initialize(account) | |
@account = account | |
end | |
def owner | |
account.user.name | |
end | |
end |
This file contains 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
require 'test_helper' | |
class AccountPresenterTest < ActiveSupport::TestCase | |
test 'it has an owner' do | |
user = User.create(name: 'Arya') | |
account = Account.create(amount: 500, user_id: user.id) | |
account_presenter = AccountPresenter.new(account) | |
assert_equal 'Arya', account_presenter.owner | |
end | |
end |
This file contains 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 AccountsController < ApplicationController | |
def show | |
@account = AccountPresenter.new(current_user.account) | |
end | |
end |
This file contains 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 TransferFunds | |
extend Forwardable | |
def self.create(params, user) | |
self.new(params, user) | |
end | |
attr_reader :transfer, :user | |
def_delegators :transfer, :amount, :id | |
def initialize(params, user) | |
@transfer = Transfer.create(params) | |
@user = user | |
end | |
def save | |
if transfer.save && user.account.amount >= transfer.amount | |
account = user.account | |
account.amount = (account.amount - transfer.amount) | |
account.save | |
end | |
end | |
end |
This file contains 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
require 'test_helper' | |
class TransferFundsTest < ActiveSupport::TestCase | |
test 'it creates' do | |
user_1 = User.create(name: 'DJ') | |
transfer_params = { amount: '1212', incoming_id: '3', outgoing_id: '1' } | |
transfer = TransferFunds.create(transfer_params, user_1) | |
assert_equal TransferFunds, transfer.class | |
assert Transfer.find_by(incoming_id: '3') | |
end | |
test 'it saves' do | |
user_1 = User.create(name: 'DJ') | |
user_2 = User.create(name: 'Miriam') | |
outgoing_account = Account.create(amount: 1000, user_id: user_1.id) | |
incoming_account = Account.create(amount: 1000, user_id: user_2.id) | |
transfer_params = { amount: 500, incoming_id: incoming_account.id, outgoing_id: outgoing_account.id } | |
transfer = TransferFunds.create(transfer_params, user_1) | |
assert transfer.save | |
assert_equal 500, user_1.account.amount | |
end | |
end |
This file contains 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 | |
def index | |
@transfers = current_user.transfers | |
end | |
def show | |
@transfer = current_user.account.outgoing_transfers.find_by(id: params[:id]) | |
end | |
def new | |
@transfer = Transfer.new | |
end | |
def create | |
@transfer = TransferFunds.create(transfer_params, current_user) | |
if @transfer.save | |
redirect_to transfer_path(id: @transfer.id), success: "Your transfer of #{@transfer.amount} dollars was successful." | |
else | |
flash.now[:warning] = 'Your transfer could not be completed. Please, try again.' | |
render :new | |
end | |
end | |
private | |
def transfer_params | |
params.require(:transfer).permit(:amount, :incoming_id, :outgoing_id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment