diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index c4cd8fc..57a624f 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -1,4 +1,7 @@ class BetsController < ApplicationController + before_action :check_bet_creator, only: :resolve + before_action :check_if_resolved, only: :resolve + def index @bets = Bet.includes(:user_bets) end @@ -23,9 +26,49 @@ def create end end + def resolve + @bet = Bet.find(params[:id]) + end + + def calculate_debts + bet = Bet.find(params[: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 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 + + 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 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/_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/resolve.html.erb b/app/views/bets/resolve.html.erb new file mode 100644 index 0000000..268083e --- /dev/null +++ b/app/views/bets/resolve.html.erb @@ -0,0 +1,51 @@ +
+
+
+
+

Selecting the winning option for:

+
+
+
+
+
<%= @bet.description %>
+
+
+ +
+
+
+ <%= 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/app/views/bets/show.html.erb b/app/views/bets/show.html.erb index 72af53a..bd50bd6 100644 --- a/app/views/bets/show.html.erb +++ b/app/views/bets/show.html.erb @@ -1,89 +1,26 @@ <%= render 'shared/bet_display', bet: @bet %> -
+<% if @bet.creator == current_user %>
-
-
-
-
-
-
-

Participants

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

This bet has been resolved.

-
-
- - - - - - - - - - <%= render @bet.user_bets %> - -
UsernameOption #Amount
-
+ <% else %> + <%= link_to 'Resolve this bet', + resolve_bet_path, + class: 'btn btn-success resolve-bet-btn', + 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 %> diff --git a/config/routes.rb b/config/routes.rb index ba1a289..7829a04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,9 @@ 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 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|