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

  def root
    @leaders = leaderboard
    @upcoming_matches = check_matches
    recent_matches
    @results = check_results
  end

  private

  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