Comes in where MVC fails: capturing behavior Places interaction in obvious places Models represent entities, not behavior Splits what an object is from what it does
@destination = destination assign_transferrer(@source) end def execute(amount) @source.transfer_to(@destination, amount) end private def assign_transferrer(account) account.extend(Transferrer) end module Transferrer def transfer_to(destination, amount) self.balance -= amount destination.balance += amount end end end
before(:each) do @s = FactoryGirl.create :account, balance: 500 @d = FactoryGirl.create :account, balance: 1000 end it 'should transfer money between accounts' do post :create, format: :json, source: @s.id, destination: @d.id, amount: 500 # Yikes :-( [ @s, @d ].map(&:reload) expect(response.status).to eq(201) expect(@s.balance).to eq(0) expect(@d.balance).to eq(1500) end end end