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

  def root
    load_leaderboard
    check_matches
    recent_matches
    @results = check_results
  end

  def leaderboard
    @leaders = Bet.joins(:user).group('users.name').sum(:points)
                  .sort_by { |k,v| v }
  end

  private

  def load_leaderboard
    @leaders = Bet.where.not(result: nil)
                  .joins(:user).group('users.name').sum(:points)
                  .sort_by { |k,v| v }.take(5)
  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(points: nil)
      .limit(5)
  end
end