Iker Narvaez

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

...@@ -2,5 +2,23 @@ class ApplicationController < HeimdallEngine::ApplicationController ...@@ -2,5 +2,23 @@ class ApplicationController < HeimdallEngine::ApplicationController
2 protect_from_forgery with: :exception 2 protect_from_forgery with: :exception
3 3
4 def root 4 def root
5 + @leaderboard = check_leaderboard
6 + @upcoming_matches = check_matches
7 + @results = check_results
8 + end
9 +
10 + private
11 +
12 + def check_leaderboard
13 + User.all.order_by(&:user_score).take(5)
14 + end
15 +
16 + def check_matches
17 + Match.active.order(date: :ASC).limit(5)
18 + end
19 +
20 + def check_results
21 + return [] unless @current_user
22 + @current_user.bets.join(:matches).order(match: { date: :ASC }).limit(5)
5 end 23 end
6 end 24 end
......
1 +#
2 +class Bet < ApplicationRecord
3 + validates :score_local, :score_visit, presence: true
4 + belongs_to :user, inverse_of: :bets
5 +
6 + scope :active, -> { joins(match: :pool).where(pools: { active: true }) }
7 +end
...@@ -26,6 +26,8 @@ class User < ApplicationRecord ...@@ -26,6 +26,8 @@ class User < ApplicationRecord
26 26
27 before_save :encrypt_password 27 before_save :encrypt_password
28 28
29 + has_many :bets, inverse_of: :user
30 +
29 has_and_belongs_to_many :roles, inverse_of: :users, 31 has_and_belongs_to_many :roles, inverse_of: :users,
30 class_name: 'HeimdallEngine::Role', 32 class_name: 'HeimdallEngine::Role',
31 join_table: 'users_roles', 33 join_table: 'users_roles',
...@@ -33,6 +35,10 @@ class User < ApplicationRecord ...@@ -33,6 +35,10 @@ class User < ApplicationRecord
33 association_foreign_key: "role_id" 35 association_foreign_key: "role_id"
34 36
35 37
38 + def user_score
39 + bets.active.sum :points
40 + end
41 +
36 def encrypt_password 42 def encrypt_password
37 return unless password.present? 43 return unless password.present?
38 self.password_salt = BCrypt::Engine.generate_salt.force_encoding('UTF-8') 44 self.password_salt = BCrypt::Engine.generate_salt.force_encoding('UTF-8')
......