From 9d2cac43e25df5fec2efe174b7a92829759a6234 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 15:09:55 -0700 Subject: [PATCH 01/10] Add resolve method to BetsController --- app/controllers/bets_controller.rb | 4 ++++ app/views/bets/resolve.html.erb | 8 ++++++++ app/views/bets/show.html.erb | 12 ++++++++++++ config/routes.rb | 2 ++ 4 files changed, 26 insertions(+) create mode 100644 app/views/bets/resolve.html.erb diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index c4cd8fc..fa7c8ca 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -23,6 +23,10 @@ def create end end + def resolve + @bet = Bet.find(params[:id]) + end + private def bet_params diff --git a/app/views/bets/resolve.html.erb b/app/views/bets/resolve.html.erb new file mode 100644 index 0000000..c94a74b --- /dev/null +++ b/app/views/bets/resolve.html.erb @@ -0,0 +1,8 @@ +
+
+
+ <%= form_tag do %> + <% end %> +
+
+
diff --git a/app/views/bets/show.html.erb b/app/views/bets/show.html.erb index 72af53a..bf3819c 100644 --- a/app/views/bets/show.html.erb +++ b/app/views/bets/show.html.erb @@ -1,5 +1,17 @@ <%= render 'shared/bet_display', bet: @bet %> +<% if @bet.creator == current_user %> +
+
+
+
+ <%= link_to 'Resolve Bet', resolve_bet_path, class: 'btn btn-success', + role: 'button' %> +
+
+
+<% end %> +

diff --git a/config/routes.rb b/config/routes.rb index ba1a289..efcf26e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,8 @@ get '/signup', to: 'users#new', as: :new_user_path + get 'bets/:id/resolve', to: 'bets#resolve', as: :resolve_bet + resources :bets do resources :user_bets, only: :create resources :bet_options, shallow: true From 329d575296add1e7719c6b1c5b11d261a53035a2 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 15:32:00 -0700 Subject: [PATCH 02/10] Add calculate_debts method to BetsController --- app/controllers/bets_controller.rb | 2 ++ .../_bet_option_radio_resolve.html.erb | 8 +++++ app/views/bets/resolve.html.erb | 32 ++++++++++++++++++- config/routes.rb | 3 +- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 app/views/bet_options/_bet_option_radio_resolve.html.erb diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index fa7c8ca..ff984de 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -27,6 +27,8 @@ def resolve @bet = Bet.find(params[:id]) end + def calculate_debts; end + private def bet_params diff --git a/app/views/bet_options/_bet_option_radio_resolve.html.erb b/app/views/bet_options/_bet_option_radio_resolve.html.erb new file mode 100644 index 0000000..97bfaac --- /dev/null +++ b/app/views/bet_options/_bet_option_radio_resolve.html.erb @@ -0,0 +1,8 @@ + + + <%= radio_button_tag :bet_option_id, bet_option.id %> + <%= label_tag :bet_option, bet_option_counter + 1 %> + + <%= bet_option.option_text %> + $<%= format('%.2f', bet_option.option_total) %> + diff --git a/app/views/bets/resolve.html.erb b/app/views/bets/resolve.html.erb index c94a74b..8580345 100644 --- a/app/views/bets/resolve.html.erb +++ b/app/views/bets/resolve.html.erb @@ -1,7 +1,37 @@
- <%= form_tag do %> + <%= form_tag calculate_debts_path, method: 'POST' do %> +
+
+
+
+

Betting Options

+
+
+
+
+ + + + + + + + + + <% @bet.options.each_with_index do |option, index| %> + <%= render 'bet_options/bet_option_radio_resolve', bet_option: option, + bet_option_counter: index %> + <% end %> + +
#Description$ total
+
+ +
<% end %>
diff --git a/config/routes.rb b/config/routes.rb index efcf26e..7829a04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,8 @@ get '/signup', to: 'users#new', as: :new_user_path get 'bets/:id/resolve', to: 'bets#resolve', as: :resolve_bet - + post 'bets/:id/calculate_debts', to: 'bets#calculate_debts', + as: :calculate_debts resources :bets do resources :user_bets, only: :create resources :bet_options, shallow: true From c8f1e952d5c525fc13dbc2fc9f9710de06fa12a1 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 15:40:30 -0700 Subject: [PATCH 03/10] Define calculate_debts method for BetsController - No validation has been performed at this stage; - Bet can be resolved multiple times; --- app/controllers/bets_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index ff984de..d861795 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -27,7 +27,13 @@ def resolve @bet = Bet.find(params[:id]) end - def calculate_debts; end + def calculate_debts + bet = Bet.find(params[:id]) + winning_option = BetOption.find(params[:bet_option_id]) + bet.resolve(winning_option: winning_option) + flash[:success] = 'Bet has been resolved!' + redirect_to bet + end private From 06671b22a42c9c2e1d0f2ba6ba087ebeb6afb441 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 15:46:50 -0700 Subject: [PATCH 04/10] Add boolean column "resolved" to Bets -It will keep track of which bet has been already resolved. --- ...70410224347_add_boolean_column_resolved_to_bets.rb | 5 +++++ db/schema.rb | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20170410224347_add_boolean_column_resolved_to_bets.rb diff --git a/db/migrate/20170410224347_add_boolean_column_resolved_to_bets.rb b/db/migrate/20170410224347_add_boolean_column_resolved_to_bets.rb new file mode 100644 index 0000000..81d92f0 --- /dev/null +++ b/db/migrate/20170410224347_add_boolean_column_resolved_to_bets.rb @@ -0,0 +1,5 @@ +class AddBooleanColumnResolvedToBets < ActiveRecord::Migration[5.0] + def change + add_column :bets, :resolved, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index e323042..7a23527 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170405174138) do +ActiveRecord::Schema.define(version: 20170410224347) do create_table "bet_options", force: :cascade do |t| t.text "option_text" @@ -24,10 +24,11 @@ create_table "bets", force: :cascade do |t| t.text "description" t.integer "status" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "creator_id", null: false - t.datetime "expires_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "creator_id", null: false + t.datetime "expires_at", null: false + t.boolean "resolved", default: false end create_table "debts", force: :cascade do |t| From 5e5a69b02ebad5e329769b5f03c5d954b91a2513 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 15:59:38 -0700 Subject: [PATCH 05/10] Notify the user about bet resolution -Users won't be able to place their wagers on the particular bet if it has already been resolved. --- .../bets/_display_unresolved_bets.html.erb | 87 +++++++++++++++ app/views/bets/show.html.erb | 101 +++--------------- 2 files changed, 99 insertions(+), 89 deletions(-) create mode 100644 app/views/bets/_display_unresolved_bets.html.erb diff --git a/app/views/bets/_display_unresolved_bets.html.erb b/app/views/bets/_display_unresolved_bets.html.erb new file mode 100644 index 0000000..c98ccf4 --- /dev/null +++ b/app/views/bets/_display_unresolved_bets.html.erb @@ -0,0 +1,87 @@ +
+
+
+
+
+
+
+
+

Participants

+
+
+
+
+ + + + + + + + + + <%= render bet.user_bets %> + +
UsernameOption #Amount
+
+
+
+
+ +
+
+
+ <%= form_for [bet, user_bet] do |f| %> +
+
+
+
+

Betting Options

+
+
+
+
+ + + + + + + + + + <% bet.options.each_with_index do |option, index| %> + <%= render 'bet_options/bet_option_radio', bet_option: option, + f: f, bet_option_counter: index %> + <% end %> + +
#Description$ total
+
+ +
+ <% end %> +
+
+
+ +
+
+
+
+ Your option is not on the list? + <%= link_to 'Add it now!', new_bet_bet_option_path(bet) %> +
+
+
+
diff --git a/app/views/bets/show.html.erb b/app/views/bets/show.html.erb index bf3819c..d702d7f 100644 --- a/app/views/bets/show.html.erb +++ b/app/views/bets/show.html.erb @@ -5,97 +5,20 @@
- <%= link_to 'Resolve Bet', resolve_bet_path, class: 'btn btn-success', - role: 'button' %> -
-
-
-<% end %> - -
-
-
-
-
-
-
-
-

Participants

-
+ <% if @bet.resolved? %> +
+

This bet has been resolved.

-
-
- - - - - - - - - - <%= render @bet.user_bets %> - -
UsernameOption #Amount
-
+ <% else %> + <%= link_to 'Resolve Bet', resolve_bet_path, class: 'btn btn-success', + role: 'button' %> + <% end %>
+<% end %> -
-
-
- <%= form_for [@bet, @user_bet] do |f| %> -
-
-
-
-

Betting Options

-
-
-
-
- - - - - - - - - - <% @bet.options.each_with_index do |option, index| %> - <%= render 'bet_options/bet_option_radio', bet_option: option, - f: f, bet_option_counter: index %> - <% end %> - -
#Description$ total
-
- -
- <% end %> -
-
-
- -
-
-
-
- Your option is not on the list? - <%= link_to 'Add it now!', new_bet_bet_option_path(@bet) %> -
-
-
-
+<% if @bet.resolved? %> +<% else %> + <%= render 'display_unresolved_bets', bet: @bet, user_bet: @user_bet %> +<% end %> From 96ee8aba47afed4961202fd6abc674c5b3770078 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Mon, 10 Apr 2017 16:16:38 -0700 Subject: [PATCH 06/10] Add validation for Bet resolution - Can't resolve a bet without picking an option first. --- app/controllers/bets_controller.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index d861795..5f4ffd4 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -29,12 +29,26 @@ def resolve def calculate_debts bet = Bet.find(params[:id]) - winning_option = BetOption.find(params[:bet_option_id]) + if params[:bet_option_id].nil? + resolve_option_not_selected(bet) + else + winning_option = BetOption.find(params[:bet_option_id]) + resolve_option_selected(bet, winning_option) + end + end + + def resolve_option_selected(bet, winning_option) bet.resolve(winning_option: winning_option) flash[:success] = 'Bet has been resolved!' + bet.update(resolved: true) redirect_to bet end + def resolve_option_not_selected(bet) + flash[:danger] = 'Please select an option first' + redirect_to resolve_bet_path(bet) + end + private def bet_params From 3f8f235468c48fbaea484f3f3b7a11029091e225 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Tue, 11 Apr 2017 10:11:27 -0700 Subject: [PATCH 07/10] Prevent not bet creators from resolving bet - Prevents users from accessing bets/:id/resolve if they are not the bet creators. --- app/controllers/bets_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index 5f4ffd4..7babd81 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -1,4 +1,6 @@ class BetsController < ApplicationController + before_action :check_bet_creator, only: :resolve + def index @bets = Bet.includes(:user_bets) end @@ -54,4 +56,11 @@ def resolve_option_not_selected(bet) def bet_params params.require(:bet).permit(:description, :status, :expires_at, :creator_id) end + + def check_bet_creator + @bet = Bet.find(params[:id]) + return if current_user == @bet.creator + flash[:danger] = 'You cannot resolve this bet because you did\'t create it' + redirect_to @bet + end end From 7f807f1a019b8a97728ec29f69dc50ed0b8a9464 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Tue, 11 Apr 2017 10:23:54 -0700 Subject: [PATCH 08/10] Prevent from accessing bet/resolve if resolved. - Prevent users from accessing the bet/resolve link if the bet has already been resolved. --- app/controllers/bets_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index 7babd81..57a624f 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -1,5 +1,6 @@ class BetsController < ApplicationController before_action :check_bet_creator, only: :resolve + before_action :check_if_resolved, only: :resolve def index @bets = Bet.includes(:user_bets) @@ -63,4 +64,11 @@ def check_bet_creator flash[:danger] = 'You cannot resolve this bet because you did\'t create it' redirect_to @bet end + + def check_if_resolved + @bet = Bet.find(params[:id]) + return unless @bet.resolved + flash[:danger] = 'This bet has been already resolved' + redirect_to @bet + end end From 87294c51b1a2b68a1596f300e61c034b252ccef2 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Tue, 11 Apr 2017 09:53:06 -0700 Subject: [PATCH 09/10] Center Resolve this bet button on Bet page --- app/views/bets/show.html.erb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/bets/show.html.erb b/app/views/bets/show.html.erb index d702d7f..bd50bd6 100644 --- a/app/views/bets/show.html.erb +++ b/app/views/bets/show.html.erb @@ -4,13 +4,15 @@
-
+
<% if @bet.resolved? %>

This bet has been resolved.

<% else %> - <%= link_to 'Resolve Bet', resolve_bet_path, class: 'btn btn-success', + <%= link_to 'Resolve this bet', + resolve_bet_path, + class: 'btn btn-success resolve-bet-btn', role: 'button' %> <% end %>
From 15a2e81f82fa205991ee4109d14b82fa200fc5d1 Mon Sep 17 00:00:00 2001 From: YarekTenko Date: Tue, 11 Apr 2017 09:46:43 -0700 Subject: [PATCH 10/10] Add supporting information when resolving bet --- app/views/bets/resolve.html.erb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/views/bets/resolve.html.erb b/app/views/bets/resolve.html.erb index 8580345..268083e 100644 --- a/app/views/bets/resolve.html.erb +++ b/app/views/bets/resolve.html.erb @@ -1,4 +1,17 @@
+
+
+
+

Selecting the winning option for:

+
+
+
+
+
<%= @bet.description %>
+
+
+ +
<%= form_tag calculate_debts_path, method: 'POST' do %>