Iker Narvaez

leaderboard by user groups

...@@ -11,8 +11,13 @@ class ApplicationController < HeimdallEngine::ApplicationController ...@@ -11,8 +11,13 @@ class ApplicationController < HeimdallEngine::ApplicationController
11 private 11 private
12 12
13 def leaderboard 13 def leaderboard
14 - Bet.joins(:user).group('users.name').sum(:points).sort_by { |k,v| v } 14 + Group.for(current_user.id).inject({}) do |h, group|
15 - .take(5) 15 + h[group.name] = {
16 + pool: group.pool_name,
17 + leaderboard: Bet.for_pool(group.pool_id).board.take(5)
18 + }
19 + h
20 + end
16 end 21 end
17 22
18 def check_matches 23 def check_matches
......
...@@ -9,6 +9,7 @@ class BetsController < ApplicationController ...@@ -9,6 +9,7 @@ class BetsController < ApplicationController
9 if current_user.update_attributes(object_params) 9 if current_user.update_attributes(object_params)
10 redirect_to bets_path 10 redirect_to bets_path
11 else 11 else
12 + load_active_pools
12 render :index 13 render :index
13 end 14 end
14 end 15 end
......
1 # 1 #
2 class Bet < ApplicationRecord 2 class Bet < ApplicationRecord
3 - validates :score_local, :score_visit, presence: true 3 + validates :visit, :local, :date, presence: true
4 belongs_to :user, inverse_of: :bets 4 belongs_to :user, inverse_of: :bets
5 belongs_to :match, inverse_of: :bets 5 belongs_to :match, inverse_of: :bets
6 + has_one :group, through: :user, inverse_of: :bets
6 enum result: [:visit, :tie, :local] 7 enum result: [:visit, :tie, :local]
7 8
8 scope :active, -> { joins(match: :pool).where(pools: { active: true }) } 9 scope :active, -> { joins(match: :pool).where(pools: { active: true }) }
10 + scope :for_pool, -> (pool_id) { joins(:match).where(matches: { pool_id: pool_id } ) }
11 + scope :board, (lambda do
12 + joins(:user)
13 + .group('users.name')
14 + .order('sum_points')
15 + .sum(:points)
16 + end)
17 +
9 before_save :assign_result 18 before_save :assign_result
10 19
11 def calculate 20 def calculate
......
...@@ -5,6 +5,12 @@ class Group < ApplicationRecord ...@@ -5,6 +5,12 @@ class Group < ApplicationRecord
5 validates :name, :pool, presence: true 5 validates :name, :pool, presence: true
6 delegate :name, to: :pool, prefix: true 6 delegate :name, to: :pool, prefix: true
7 7
8 + scope :active, -> { joins(:pool).where(pools: { active: true}) }
9 + scope :for, -> (user_id) {
10 + joins(:users).where('users.id = ?', user_id).distinct
11 + }
12 +
13 +
8 def users_count 14 def users_count
9 users.count 15 users.count
10 end 16 end
......
1 h1 Leaderboard 1 h1 Leaderboard
2 table.leaderboard 2 table.leaderboard
3 tbody 3 tbody
4 - - @leaders.each do |user, points| 4 + - @leaders.each do |group_name, data|
5 tr 5 tr
6 - td = user 6 + td colspan=2 = group_name
7 + - data[:leaderboard].each do |user_name, points|
8 + tr
9 + td = user_name
7 td = points 10 td = points
8 - - if @leaders.empty? 11 + - if data[:leaderboard].empty?
9 tr 12 tr
10 td colspan=2 There are no results yet 13 td colspan=2 There are no results yet
......
1 +- if current_user.errors.any?
2 + div = current_user.errors.full_messages.to_sentence
1 table.pools 3 table.pools
2 - pool.matches.each do |match| 4 - pool.matches.each do |match|
3 - bet = current_user.bets.find_or_initialize_by(match_id: match.id) 5 - bet = current_user.bets.find_or_initialize_by(match_id: match.id)
......