Iker Narvaez

added bets update and leaderboard queries

......@@ -12,6 +12,7 @@ gem 'paper_trail'
gem 'will_paginate'
gem 'nested_form'
gem 'simple_form'
gem 'enum_help'
gem 'pry'
......
......@@ -84,6 +84,8 @@ GEM
crass (1.0.4)
debug_inspector (0.0.3)
diff-lcs (1.3)
enum_help (0.0.17)
activesupport (>= 3.0.0)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.25)
......@@ -232,6 +234,7 @@ DEPENDENCIES
byebug
coffee-rails (~> 4.2)
crud_controller!
enum_help
heimdall_engine!
jbuilder (~> 2.5)
jquery-rails
......
......@@ -2,23 +2,31 @@ class ApplicationController < HeimdallEngine::ApplicationController
protect_from_forgery with: :exception
def root
@leaders = check_leaderboard
@leaders = leaderboard
@upcoming_matches = check_matches
recent_matches
@results = check_results
end
private
def check_leaderboard
(User.all.sort {|a,b| a.user_score <=> b.user_score }).take(5)
def leaderboard
Bet.joins(:user).group('users.name').sum(:points).sort_by { |k,v| v }
.take(5)
end
def check_matches
Match.active.order(date: :ASC).limit(5)
.where('date > ?', Time.zone.now)
end
def check_results
return [] unless @current_user
@current_user.bets.join(:matches).order(match: { date: :ASC }).limit(5)
end
def recent_matches
@recent_matches = Match.active.where.not(result: nil)
.order(:date).limit(5)
end
end
......
class BetsController < ApplicationController
include Crud
def index
load_active_pools
end
def update
if current_user.update_attributes(object_params)
redirect_to bets_path
else
render :index
end
end
private
def load_active_pools
@pools = Pool.active
end
def object_params
params.require(:user).permit(
bets_attributes: %i[id score_local score_visit match_id]
)
end
end
......@@ -30,8 +30,8 @@ class Bet < ApplicationRecord
end
def find_result
return :tie if local_score == visit_score
return :local if local_score > visit_score
return :tie if score_local == score_visit
return :local if score_local > score_visit
:visit
end
end
......
......@@ -13,13 +13,13 @@ class Match < ApplicationRecord
def calculate
return unless local_score && visit_score
return unless score_local && score_visit
self.result = find_result
end
def find_result
return :tie if local_score == visit_score
return :local if local_score > visit_score
return :tie if score_local == score_visit
return :local if score_local > score_visit
:visit
end
......
......@@ -17,6 +17,11 @@ class Pool < ApplicationRecord
has_many :matches, dependent: :destroy, inverse_of: :pool
scope :active, -> { where(active: true) }
accepts_nested_attributes_for :matches, reject_if: :all_blank,
allow_destroy: true
def editable?
editable_until > Time.zone.now
end
end
......
......@@ -34,6 +34,9 @@ class User < ApplicationRecord
foreign_key: "user_id",
association_foreign_key: "role_id"
accepts_nested_attributes_for :bets, reject_if: :all_blank,
allow_destroy: false
def user_score
bets.active.sum :points
......
......@@ -5,8 +5,8 @@ table
th Score
tbody
tr
- @leaders.each do |l|
td = l.name
td = l.user_score
- @leaders.each do |user, points|
td = user
td = points
......
h1 Upcoming matches
table
thead
th Team 1
th Team 2
th Date
tbody
tr
- @upcoming_matches.each do |u|
td = l(u.date, format: :short)
td = u.local
td = u.visit
td = u.date
h1 Recent matches
table
tbody
tr
- @recent_matches.each do |match|
td = l(match.date, format: :short)
td = match.local
td = match.score_local
td = match.visit
td = match.score_visit
......
table
- pool.matches.each do |match|
- bet = current_user.bets.find_or_initialize_by(match_id: match.id)
= f.simple_fields_for :bets, bet, wrapper: false do|ff|
tr
td = l(match.date, format: :short)
td = match.local
td
= ff.input_field :score_visit, placeholder: 'Score local', min: 0
= ff.input :id, as: :hidden
= ff.input :match_id, as: :hidden
td = match.visit
td = ff.input_field :score_local, placeholder: 'Score visit', min: 0
td = bet.result ? "Your bet: #{bet.result_i18n.upcase}" : 'BET PENDING'
table
thead
th Day
th Home
th Your bet
th Away
th Your bet
th Result
th Home Score
th Away Score
th Points
- pool.matches.each do |match|
- bet = current_user.bets.find_or_initialize_by(match_id: match.id)
tr
td = l(match.date, format: :short)
td = match.local
td = bet.try(:score_local)
td = match.visit
td = bet.try(:score_visit)
td = match.result ? match.result.upcase : 'PENDING'
td = match.score_local
td = match.score_visit
td = bet.try(:points)
h2 My Bets
= simple_nested_form_for current_user, url: put_bets_path, method: :patch do |f|
- @pools.each do |pool|
div
h3 = pool.name
- if pool.editable?
= render 'pool_form', pool: pool, f: f
- else
= render 'readonly_pool', pool: pool
div
= f.submit 'Save'
......@@ -21,3 +21,12 @@
en:
hello: "Hello world"
date:
short: '%b %d'
enums:
bet:
result:
local: home
tie: tie
visit: away
......
......@@ -2,6 +2,11 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
HeimdallEngine.load_routes
root 'application#root'
resources :bets, only: [:index] do
collection do
patch :update, as: :put
end
end
namespace :admin do
resources :pools
......