Skip to content
43 changes: 43 additions & 0 deletions app/controllers/bets_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
8 changes: 8 additions & 0 deletions app/views/bet_options/_bet_option_radio_resolve.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<tr class="row" role="button">
<td class="col col-lg-2">
<%= radio_button_tag :bet_option_id, bet_option.id %>
<%= label_tag :bet_option, bet_option_counter + 1 %>
</td>
<td class="col col-lg-6"><%= bet_option.option_text %></td>
<td class="col col-lg-4">$<%= format('%.2f', bet_option.option_total) %></td>
</tr>
87 changes: 87 additions & 0 deletions app/views/bets/_display_unresolved_bets.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<div class="display-bet">
<hr>
<div class="row">
<div class="col col-lg-6 offset-lg-3">
<div class="card">
<div class="card-header text-muted">
<div class="row">
<div class="col col-lg-12 text-center">
<h4 class="card-header-text">Participants</h4>
</div>
</div>
</div>
<div class="card-block">
<table class="table table-hover">
<thead class="thead-default">
<tr class="row">
<th class="col col-lg-4">Username</th>
<th class="col col-lg-4">Option #</th>
<th class="col col-lg-4">Amount</th>
</tr>
</thead>
<tbody>
<%= render bet.user_bets %>
</tbody>
</table>
</div>
</div>
</div>
</div>

<hr>
<div class="row">
<div class="col col-lg-6 offset-lg-3">
<%= form_for [bet, user_bet] do |f| %>
<div class="card">
<div class="card-header text-muted">
<div class="row">
<div class="col col-lg-12 text-center">
<h4 class="card-header-text">Betting Options</h4>
</div>
</div>
</div>
<div class="card-block">
<table class="table table-hover table-selectable">
<thead class="thead-default">
<tr class="row">
<th class="col col-lg-2">#</th>
<th class="col col-lg-6">Description</th>
<th class="col col-lg-4">$ total</th>
</tr>
</thead>
<tbody>
<% bet.options.each_with_index do |option, index| %>
<%= render 'bet_options/bet_option_radio', bet_option: option,
f: f, bet_option_counter: index %>
<% end %>
</tbody>
</table>
</div>
<div class="card-footer">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-usd"></i></span>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.number_field :amount_bet, class: 'form-control money-formatter',
step: :any, placeholder: 'Enter a number, e.g. 10.35' %>
</div>
</div>
<%= f.submit 'Bet!', class: 'btn btn-success btn-full-width',
role: 'button' %>
</div>
</div>
<% end %>
</div>
</div>
</div>

<div class="row">
<div class="col col-lg-6 offset-lg-3">
<div class="card">
<div class="card-block text-center">
<span>Your option is not on the list?</span>
<%= link_to 'Add it now!', new_bet_bet_option_path(bet) %>
</div>
</div>
</div>
</div>
51 changes: 51 additions & 0 deletions app/views/bets/resolve.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<div class="picking-winning-bet">
<hr>
<div class="row">
<div class="col col-lg-12 text-center">
<h3>Selecting the winning option for:</h3>
</div>
</div>
<div class="row">
<div class="col col-lg-12 text-center text-muted">
<h5><%= @bet.description %></h5>
</div>
</div>

<hr>
<div class="row">
<div class="col col-lg-12">
<%= form_tag calculate_debts_path, method: 'POST' do %>
<div class="card">
<div class="card-header text-muted">
<div class="row">
<div class="col col-lg-12 text-center">
<h4 class="card-header-text">Betting Options</h4>
</div>
</div>
</div>
<div class="card-block">
<table class="table table-hover table-selectable">
<thead class="thead-default">
<tr class="row">
<th class="col col-lg-2">#</th>
<th class="col col-lg-6">Description</th>
<th class="col col-lg-4">$ total</th>
</tr>
</thead>
<tbody>
<% @bet.options.each_with_index do |option, index| %>
<%= render 'bet_options/bet_option_radio_resolve', bet_option: option,
bet_option_counter: index %>
<% end %>
</tbody>
</table>
</div>
<div class="card-footer text-center">
<%= submit_tag 'Declare the winner!', class: 'btn btn-success btn-full-width',
role: 'button' %>
</div>
</div>
<% end %>
</div>
</div>
</div>
99 changes: 18 additions & 81 deletions app/views/bets/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,89 +1,26 @@
<%= render 'shared/bet_display', bet: @bet %>

<div class="display-bet">
<% if @bet.creator == current_user %>
<hr>
<div class="row">
<div class="col col-lg-6 offset-lg-3">
<div class="card">
<div class="card-header text-muted">
<div class="row">
<div class="col col-lg-12 text-center">
<h4 class="card-header-text">Participants</h4>
</div>
<div class="resolve-bet">
<div class="row text-center">
<div class="col col-lg-6 offset-lg-3">
<% if @bet.resolved? %>
<div class="text-muted">
<h3>This bet has been resolved.</h3>
</div>
</div>
<div class="card-block">
<table class="table table-hover">
<thead class="thead-default">
<tr class="row">
<th class="col col-lg-4">Username</th>
<th class="col col-lg-4">Option #</th>
<th class="col col-lg-4">Amount</th>
</tr>
</thead>
<tbody>
<%= render @bet.user_bets %>
</tbody>
</table>
</div>
<% else %>
<%= link_to 'Resolve this bet',
resolve_bet_path,
class: 'btn btn-success resolve-bet-btn',
role: 'button' %>
<% end %>
</div>
</div>
</div>
<% end %>

<hr>
<div class="row">
<div class="col col-lg-6 offset-lg-3">
<%= form_for [@bet, @user_bet] do |f| %>
<div class="card">
<div class="card-header text-muted">
<div class="row">
<div class="col col-lg-12 text-center">
<h4 class="card-header-text">Betting Options</h4>
</div>
</div>
</div>
<div class="card-block">
<table class="table table-hover table-selectable">
<thead class="thead-default">
<tr class="row">
<th class="col col-lg-2">#</th>
<th class="col col-lg-6">Description</th>
<th class="col col-lg-4">$ total</th>
</tr>
</thead>
<tbody>
<% @bet.options.each_with_index do |option, index| %>
<%= render 'bet_options/bet_option_radio', bet_option: option,
f: f, bet_option_counter: index %>
<% end %>
</tbody>
</table>
</div>
<div class="card-footer">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-usd"></i></span>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.number_field :amount_bet, class: 'form-control money-formatter',
step: :any, placeholder: 'Enter a number, e.g. 10.35' %>
</div>
</div>
<%= f.submit 'Bet!', class: 'btn btn-success btn-full-width',
role: 'button' %>
</div>
</div>
<% end %>
</div>
</div>
</div>

<div class="row">
<div class="col col-lg-6 offset-lg-3">
<div class="card">
<div class="card-block text-center">
<span>Your option is not on the list?</span>
<%= link_to 'Add it now!', new_bet_bet_option_path(@bet) %>
</div>
</div>
</div>
</div>
<% if @bet.resolved? %>
<% else %>
<%= render 'display_unresolved_bets', bet: @bet, user_bet: @user_bet %>
<% end %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBooleanColumnResolvedToBets < ActiveRecord::Migration[5.0]
def change
add_column :bets, :resolved, :boolean, default: false
end
end
11 changes: 6 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand Down