From a4eeef4a42a7b9f7077a74f3df9ff754d67f52cc Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Thu, 23 Mar 2017 13:42:59 -0700 Subject: [PATCH 1/3] Create DebtsController -Added empty index and update methods for DebtsController. The index method will output all the debts for a particular user; the update method will update the debt's :payment_date column when the debt is resolved; -Added debts as subresources for users. --- app/controllers/debts_controller.rb | 5 +++++ app/views/debts/index.html.erb | 0 app/views/debts/show.htlm.erb | 0 config/routes.rb | 5 ++++- 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 app/controllers/debts_controller.rb create mode 100644 app/views/debts/index.html.erb create mode 100644 app/views/debts/show.htlm.erb diff --git a/app/controllers/debts_controller.rb b/app/controllers/debts_controller.rb new file mode 100644 index 0000000..75bfcc3 --- /dev/null +++ b/app/controllers/debts_controller.rb @@ -0,0 +1,5 @@ +class DebtsController < ApplicationController + def index; end + + def update; end +end diff --git a/app/views/debts/index.html.erb b/app/views/debts/index.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/debts/show.htlm.erb b/app/views/debts/show.htlm.erb new file mode 100644 index 0000000..e69de29 diff --git a/config/routes.rb b/config/routes.rb index 7829a04..36b3bec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 From 2abd186a7d67802b60dd20043a7d6150b9a8b233 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Fri, 24 Mar 2017 11:00:25 -0700 Subject: [PATCH 2/3] Add rspec for DebtsController --- spec/controllers/debts_controller_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/controllers/debts_controller_spec.rb diff --git a/spec/controllers/debts_controller_spec.rb b/spec/controllers/debts_controller_spec.rb new file mode 100644 index 0000000..1ebbca2 --- /dev/null +++ b/spec/controllers/debts_controller_spec.rb @@ -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: 'bob@mail.com', + password: '123') + end + before { log_in(user) } + it { is_expected.to be_ok } + end + + describe('PATCH #update') do + end +end From 6db96befe3c5dc78e8f8f38f481e6b544583f394 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Thu, 23 Mar 2017 14:32:27 -0700 Subject: [PATCH 3/3] Add ability to view Debts for User --- app/controllers/debts_controller.rb | 4 +++- app/models/debt.rb | 8 ++++++++ app/views/debts/_my_debt.html.erb | 6 ++++++ app/views/debts/index.html.erb | 27 +++++++++++++++++++++++++++ app/views/shared/_user_bar.html.erb | 4 ++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/views/debts/_my_debt.html.erb diff --git a/app/controllers/debts_controller.rb b/app/controllers/debts_controller.rb index 75bfcc3..97ad838 100644 --- a/app/controllers/debts_controller.rb +++ b/app/controllers/debts_controller.rb @@ -1,5 +1,7 @@ class DebtsController < ApplicationController - def index; end + def index + @debts = current_user.debts + end def update; end end diff --git a/app/models/debt.rb b/app/models/debt.rb index 951347c..629bc51 100644 --- a/app/models/debt.rb +++ b/app/models/debt.rb @@ -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 diff --git a/app/views/debts/_my_debt.html.erb b/app/views/debts/_my_debt.html.erb new file mode 100644 index 0000000..3c8cdcd --- /dev/null +++ b/app/views/debts/_my_debt.html.erb @@ -0,0 +1,6 @@ + + <%= debt.creditor.username %> + $<%= debt.amount %> + <%= debt.won_option.option_text %> + <%= debt.user_option(current_user).option_text %> + diff --git a/app/views/debts/index.html.erb b/app/views/debts/index.html.erb index e69de29..00b2103 100644 --- a/app/views/debts/index.html.erb +++ b/app/views/debts/index.html.erb @@ -0,0 +1,27 @@ +
+
+
+

Viewing my debts

+
+
+ +
+
+
+ + + + + + + + + + + <% @debts.each do |debt| %> + <%= render 'my_debt', debt: debt %> + <% end %> + +
I owethis muchbecauseand I bet on
+
+
diff --git a/app/views/shared/_user_bar.html.erb b/app/views/shared/_user_bar.html.erb index bc92d01..e9286ce 100644 --- a/app/views/shared/_user_bar.html.erb +++ b/app/views/shared/_user_bar.html.erb @@ -13,6 +13,10 @@ +