Iker Narvaez

Merge branch 'master' of git.ukko.mx:psanchezp/quiniela-mundial

......@@ -2,5 +2,23 @@ class ApplicationController < HeimdallEngine::ApplicationController
protect_from_forgery with: :exception
def root
@leaderboard = check_leaderboard
@upcoming_matches = check_matches
@results = check_results
end
private
def check_leaderboard
User.all.order_by(&:user_score).take(5)
end
def check_matches
Match.active.order(date: :ASC).limit(5)
end
def check_results
return [] unless @current_user
@current_user.bets.join(:matches).order(match: { date: :ASC }).limit(5)
end
end
......
#
class Bet < ApplicationRecord
validates :score_local, :score_visit, presence: true
belongs_to :user, inverse_of: :bets
scope :active, -> { joins(match: :pool).where(pools: { active: true }) }
end
......@@ -26,6 +26,8 @@ class User < ApplicationRecord
before_save :encrypt_password
has_many :bets, inverse_of: :user
has_and_belongs_to_many :roles, inverse_of: :users,
class_name: 'HeimdallEngine::Role',
join_table: 'users_roles',
......@@ -33,6 +35,10 @@ class User < ApplicationRecord
association_foreign_key: "role_id"
def user_score
bets.active.sum :points
end
def encrypt_password
return unless password.present?
self.password_salt = BCrypt::Engine.generate_salt.force_encoding('UTF-8')
......