application_controller.rb 898 Bytes
class ApplicationController < HeimdallEngine::ApplicationController
  protect_from_forgery with: :exception

  def root
    leaderboard
    check_matches
    recent_matches
    @results = check_results
  end

  private

  def leaderboard
    @leaders = Group.for(current_user.id).inject({}) do |h, group|
      h[group.name] = {
        pool: group.pool_name,
        leaderboard: Bet.for_pool(group.pool_id).board.take(5)
      }
      h
    end
  end

  def check_matches
    @upcoming_matches =  Match.active.order(date: :ASC).limit(5)
                              .where('date > ?', Time.zone.now)
  end

  def recent_matches
    @recent_matches = Match.active.where.not(result: nil)
      .order(:date).limit(5)
  end

  def check_results
    return [] unless current_user
    current_user.bets.joins(:match).order('matches.date desc')
      .where.not(result: nil)
      .limit(5)
  end
end