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

Users can see debts #75

Open
wants to merge 3 commits into
base: debts_are_displayed_on_a_bet
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: 7 additions & 0 deletions app/controllers/debts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DebtsController < ApplicationController
def index
@debts = current_user.debts
end

def update; end
end
8 changes: 8 additions & 0 deletions app/models/debt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ class Debt < ApplicationRecord
belongs_to :debtor, class_name: 'User'
belongs_to :creditor, class_name: 'User'
belongs_to :bet, optional: true

def won_option
bet.options.winners.first
end

def user_option(user)
user.user_bets.find_by(bet: bet).bet_option
end
end
6 changes: 6 additions & 0 deletions app/views/debts/_my_debt.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tr>
<td class="col-md-3"><%= debt.creditor.username %></td>
<td class="col-md-3">$<%= debt.amount %></td>
<td class="col-md-3"><%= debt.won_option.option_text %></td>
<td class="col-md-3"><%= debt.user_option(current_user).option_text %></td>
</tr>
27 changes: 27 additions & 0 deletions app/views/debts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<hr>
<div class="row">
<div class="col col-lg-12 text-center">
<h3>Viewing my debts</h3>
</div>
</div>

<hr>
<div class="row">
<div class="col col-lg-12">
<table class="table table-hover">
<thead class="thead-default">
<tr>
<th class="col-md-3">I owe</th>
<th class="col-md-3">this much</th>
<th class="col-md-3">because</th>
<th class="col-md-3">and I bet on</th>
</tr>
</thead>
<tbody>
<% @debts.each do |debt| %>
<%= render 'my_debt', debt: debt %>
<% end %>
</tbody>
</table>
</div>
</div>
Empty file added app/views/debts/show.htlm.erb
Empty file.
4 changes: 4 additions & 0 deletions app/views/shared/_user_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<li class="nav-item">
<%= link_to 'View Bets', bets_path, class: 'nav-link' %>
</li>
<li class="nav-item">
<%= link_to 'My Debts', user_debts_path(current_user),
class: 'nav-link' %>
</li>
</ul>
<form class="form-inline dropdown">
<button id="avatar-dropdown" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" type="button" style="width: 100%">
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
resources :user_bets, only: :create
resources :bet_options, shallow: true
end
resources :users

resources :users do
resources :debts, only: [:index, :update]
end
end
16 changes: 16 additions & 0 deletions spec/controllers/debts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe DebtsController, type: :controller do
describe('GET #index') do
subject { get :index, params: { user_id: current_user.id } }
let(:user) do
User.create(username: 'Bob', email: '[email protected]',
password: '123')
end
before { log_in(user) }
it { is_expected.to be_ok }
end

describe('PATCH #update') do
end
end