From 7a1035742d62aed89a1b3430d5e37b133168f2f9 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Thu, 23 Mar 2017 15:44:05 -0700 Subject: [PATCH 1/2] Add ability to resolve debts --- app/controllers/debts_controller.rb | 7 ++++- app/views/debts/_debt.html.erb | 43 +++++++++++++++++------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/controllers/debts_controller.rb b/app/controllers/debts_controller.rb index 97ad838..ec8324d 100644 --- a/app/controllers/debts_controller.rb +++ b/app/controllers/debts_controller.rb @@ -3,5 +3,10 @@ def index @debts = current_user.debts end - def update; end + def update + @debt = Debt.find(params[:id]) + @bet = Bet.find(params[:bet_id]) + @debt.update(payment_date: DateTime.current) + redirect_to @bet + end end diff --git a/app/views/debts/_debt.html.erb b/app/views/debts/_debt.html.erb index d4166e9..71343ea 100644 --- a/app/views/debts/_debt.html.erb +++ b/app/views/debts/_debt.html.erb @@ -1,23 +1,32 @@ - <% if current_user == debt.debtor %> -

Me

- <% else %> - <%= debt.debtor.username %> - <% end %> + <%= debt.debtor.username %> - $<%= debt.amount %> -
+ $<%= debt.amount %> + - <%= debt.creditor.username %> - <% if debt.payment_date.nil? %> - - Not paid - - <% else %> - - Paid on <%= debt.payment_date %> - - <% end %> + + <% if debt.creditor == current_user %> +

Me

+ <% else %> + <%= debt.creditor.username %> + <% end %> + + <% if debt.creditor == current_user && debt.payment_date.nil?%> + + <%= link_to 'Resolve', user_debt_path(debt.debtor, debt, bet_id: @bet), method: :patch, + class: 'btn btn-primary', role: 'button' %> + + <% else %> + <% if debt.payment_date.nil? %> + + Not paid + + <% else %> + + Paid on <%= debt.payment_date.strftime('%A, %B %d %Y, %l:%M %P') %> + + <% end %> + <% end %> From 85716f225a143160b4c21fa44321e66d482075d1 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Fri, 24 Mar 2017 11:00:25 -0700 Subject: [PATCH 2/2] Add rspec for DebtsController --- spec/controllers/debts_controller_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/controllers/debts_controller_spec.rb b/spec/controllers/debts_controller_spec.rb index 1ebbca2..8e6de8d 100644 --- a/spec/controllers/debts_controller_spec.rb +++ b/spec/controllers/debts_controller_spec.rb @@ -12,5 +12,28 @@ end describe('PATCH #update') do + subject(:patch_update) do + patch :update, params: { user_id: debtor.id, bet_id: bet.id, + id: debt.id } + end + let(:debtor) do + User.create(username: 'Pete', email: 'pete@mail.com', + password: '321') + end + let(:creditor) do + User.create(username: 'Jack', email: 'jack@mail.com', + password: 'j4cK') + end + let(:bet) do + Bet.create(description: 'Test bet', status: '1', creator_id: creditor.id, + expires_at: DateTime.tomorrow) + end + let(:debt) do + Debt.create(debtor: debtor, creditor: creditor, amount: 50, bet: bet) + end + it { is_expected.to redirect_to bet } + it 'changes the payment date for the debt' do + expect { patch_update }.to change { debt.reload.payment_date }.from(nil) + end end end