Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creditors can resolve debts #76

Open
wants to merge 2 commits into
base: users_can_see_debts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/debts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 26 additions & 17 deletions app/views/debts/_debt.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
<tbody class="thead-default">
<tr>
<% if current_user == debt.debtor %>
<td class="col-md-3 text-danger"><h4>Me</h4></td>
<% else %>
<td class="col-md-3 text-muted"><%= debt.debtor.username %></td>
<% end %>
<td class="col-md-3"><%= debt.debtor.username %></td>
<td class="col-md-3">
<span class="debt-amount">$<%= debt.amount %></span>
<div><i class="underarrow text-danger fa fa-long-arrow-right"></i></div>
<span>$<%= debt.amount %></span>
<span><i class="fa fa-long-arrow-right"></i></span>
</td>
<td class="col-md-3 text-muted"><%= debt.creditor.username %></td>
<% if debt.payment_date.nil? %>
<td class="col-md-3 text-danger">
Not paid
</td>
<% else %>
<td class="col-md-3 text-success">
Paid on <%= debt.payment_date %>
</td>
<% end %>
<td class="col-md-3">
<% if debt.creditor == current_user %>
<h4 class="text-primary">Me</h4>
<% else %>
<%= debt.creditor.username %>
<% end %>
</td>
<% if debt.creditor == current_user && debt.payment_date.nil?%>
<td class="col-md-3">
<%= link_to 'Resolve', user_debt_path(debt.debtor, debt, bet_id: @bet), method: :patch,
class: 'btn btn-primary', role: 'button' %>
</td>
<% else %>
<% if debt.payment_date.nil? %>
<td class="col-md-3 text-danger">
Not paid
</td>
<% else %>
<td class="col-md-3 text-success">
Paid on <%= debt.payment_date.strftime('%A, %B %d %Y, %l:%M %P') %>
</td>
<% end %>
<% end %>
</tr>
</tbody>
23 changes: 23 additions & 0 deletions spec/controllers/debts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
password: '321')
end
let(:creditor) do
User.create(username: 'Jack', email: '[email protected]',
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