Showing
15 changed files
with
132 additions
and
17 deletions
... | @@ -84,6 +84,8 @@ GEM | ... | @@ -84,6 +84,8 @@ GEM |
84 | crass (1.0.4) | 84 | crass (1.0.4) |
85 | debug_inspector (0.0.3) | 85 | debug_inspector (0.0.3) |
86 | diff-lcs (1.3) | 86 | diff-lcs (1.3) |
87 | + enum_help (0.0.17) | ||
88 | + activesupport (>= 3.0.0) | ||
87 | erubis (2.7.0) | 89 | erubis (2.7.0) |
88 | execjs (2.7.0) | 90 | execjs (2.7.0) |
89 | ffi (1.9.25) | 91 | ffi (1.9.25) |
... | @@ -232,6 +234,7 @@ DEPENDENCIES | ... | @@ -232,6 +234,7 @@ DEPENDENCIES |
232 | byebug | 234 | byebug |
233 | coffee-rails (~> 4.2) | 235 | coffee-rails (~> 4.2) |
234 | crud_controller! | 236 | crud_controller! |
237 | + enum_help | ||
235 | heimdall_engine! | 238 | heimdall_engine! |
236 | jbuilder (~> 2.5) | 239 | jbuilder (~> 2.5) |
237 | jquery-rails | 240 | jquery-rails | ... | ... |
... | @@ -2,23 +2,31 @@ class ApplicationController < HeimdallEngine::ApplicationController | ... | @@ -2,23 +2,31 @@ 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 | - @leaders = check_leaderboard | 5 | + @leaders = leaderboard |
6 | @upcoming_matches = check_matches | 6 | @upcoming_matches = check_matches |
7 | + recent_matches | ||
7 | @results = check_results | 8 | @results = check_results |
8 | end | 9 | end |
9 | 10 | ||
10 | private | 11 | private |
11 | 12 | ||
12 | - def check_leaderboard | 13 | + def leaderboard |
13 | - (User.all.sort {|a,b| a.user_score <=> b.user_score }).take(5) | 14 | + Bet.joins(:user).group('users.name').sum(:points).sort_by { |k,v| v } |
15 | + .take(5) | ||
14 | end | 16 | end |
15 | 17 | ||
16 | def check_matches | 18 | def check_matches |
17 | Match.active.order(date: :ASC).limit(5) | 19 | Match.active.order(date: :ASC).limit(5) |
20 | + .where('date > ?', Time.zone.now) | ||
18 | end | 21 | end |
19 | 22 | ||
20 | def check_results | 23 | def check_results |
21 | return [] unless @current_user | 24 | return [] unless @current_user |
22 | @current_user.bets.join(:matches).order(match: { date: :ASC }).limit(5) | 25 | @current_user.bets.join(:matches).order(match: { date: :ASC }).limit(5) |
23 | end | 26 | end |
27 | + | ||
28 | + def recent_matches | ||
29 | + @recent_matches = Match.active.where.not(result: nil) | ||
30 | + .order(:date).limit(5) | ||
31 | + end | ||
24 | end | 32 | end | ... | ... |
app/controllers/bets_controller.rb
0 → 100644
1 | +class BetsController < ApplicationController | ||
2 | + include Crud | ||
3 | + | ||
4 | + def index | ||
5 | + load_active_pools | ||
6 | + end | ||
7 | + | ||
8 | + def update | ||
9 | + if current_user.update_attributes(object_params) | ||
10 | + redirect_to bets_path | ||
11 | + else | ||
12 | + render :index | ||
13 | + end | ||
14 | + end | ||
15 | + private | ||
16 | + | ||
17 | + def load_active_pools | ||
18 | + @pools = Pool.active | ||
19 | + end | ||
20 | + | ||
21 | + def object_params | ||
22 | + params.require(:user).permit( | ||
23 | + bets_attributes: %i[id score_local score_visit match_id] | ||
24 | + ) | ||
25 | + end | ||
26 | +end |
... | @@ -30,8 +30,8 @@ class Bet < ApplicationRecord | ... | @@ -30,8 +30,8 @@ class Bet < ApplicationRecord |
30 | end | 30 | end |
31 | 31 | ||
32 | def find_result | 32 | def find_result |
33 | - return :tie if local_score == visit_score | 33 | + return :tie if score_local == score_visit |
34 | - return :local if local_score > visit_score | 34 | + return :local if score_local > score_visit |
35 | :visit | 35 | :visit |
36 | end | 36 | end |
37 | end | 37 | end | ... | ... |
... | @@ -13,13 +13,13 @@ class Match < ApplicationRecord | ... | @@ -13,13 +13,13 @@ class Match < ApplicationRecord |
13 | 13 | ||
14 | 14 | ||
15 | def calculate | 15 | def calculate |
16 | - return unless local_score && visit_score | 16 | + return unless score_local && score_visit |
17 | self.result = find_result | 17 | self.result = find_result |
18 | end | 18 | end |
19 | 19 | ||
20 | def find_result | 20 | def find_result |
21 | - return :tie if local_score == visit_score | 21 | + return :tie if score_local == score_visit |
22 | - return :local if local_score > visit_score | 22 | + return :local if score_local > score_visit |
23 | :visit | 23 | :visit |
24 | end | 24 | end |
25 | 25 | ... | ... |
... | @@ -17,6 +17,11 @@ class Pool < ApplicationRecord | ... | @@ -17,6 +17,11 @@ class Pool < ApplicationRecord |
17 | 17 | ||
18 | has_many :matches, dependent: :destroy, inverse_of: :pool | 18 | has_many :matches, dependent: :destroy, inverse_of: :pool |
19 | 19 | ||
20 | + scope :active, -> { where(active: true) } | ||
20 | accepts_nested_attributes_for :matches, reject_if: :all_blank, | 21 | accepts_nested_attributes_for :matches, reject_if: :all_blank, |
21 | allow_destroy: true | 22 | allow_destroy: true |
23 | + | ||
24 | + def editable? | ||
25 | + editable_until > Time.zone.now | ||
26 | + end | ||
22 | end | 27 | end | ... | ... |
... | @@ -34,6 +34,9 @@ class User < ApplicationRecord | ... | @@ -34,6 +34,9 @@ class User < ApplicationRecord |
34 | foreign_key: "user_id", | 34 | foreign_key: "user_id", |
35 | association_foreign_key: "role_id" | 35 | association_foreign_key: "role_id" |
36 | 36 | ||
37 | + accepts_nested_attributes_for :bets, reject_if: :all_blank, | ||
38 | + allow_destroy: false | ||
39 | + | ||
37 | 40 | ||
38 | def user_score | 41 | def user_score |
39 | bets.active.sum :points | 42 | bets.active.sum :points | ... | ... |
1 | h1 Upcoming matches | 1 | h1 Upcoming matches |
2 | table | 2 | table |
3 | - thead | ||
4 | - th Team 1 | ||
5 | - th Team 2 | ||
6 | - th Date | ||
7 | tbody | 3 | tbody |
8 | tr | 4 | tr |
9 | - @upcoming_matches.each do |u| | 5 | - @upcoming_matches.each do |u| |
6 | + td = l(u.date, format: :short) | ||
10 | td = u.local | 7 | td = u.local |
11 | td = u.visit | 8 | td = u.visit |
12 | - td = u.date | ||
13 | - | ||
14 | 9 | ||
10 | +h1 Recent matches | ||
11 | +table | ||
12 | + tbody | ||
13 | + tr | ||
14 | + - @recent_matches.each do |match| | ||
15 | + td = l(match.date, format: :short) | ||
16 | + td = match.local | ||
17 | + td = match.score_local | ||
18 | + td = match.visit | ||
19 | + td = match.score_visit | ... | ... |
app/views/bets/_pool_form.slim
0 → 100644
1 | +table | ||
2 | + - pool.matches.each do |match| | ||
3 | + - bet = current_user.bets.find_or_initialize_by(match_id: match.id) | ||
4 | + = f.simple_fields_for :bets, bet, wrapper: false do|ff| | ||
5 | + tr | ||
6 | + td = l(match.date, format: :short) | ||
7 | + td = match.local | ||
8 | + td | ||
9 | + = ff.input_field :score_visit, placeholder: 'Score local', min: 0 | ||
10 | + = ff.input :id, as: :hidden | ||
11 | + = ff.input :match_id, as: :hidden | ||
12 | + td = match.visit | ||
13 | + td = ff.input_field :score_local, placeholder: 'Score visit', min: 0 | ||
14 | + td = bet.result ? "Your bet: #{bet.result_i18n.upcase}" : 'BET PENDING' | ||
15 | + |
app/views/bets/_readonly_pool.slim
0 → 100644
1 | +table | ||
2 | + thead | ||
3 | + th Day | ||
4 | + th Home | ||
5 | + th Your bet | ||
6 | + th Away | ||
7 | + th Your bet | ||
8 | + th Result | ||
9 | + th Home Score | ||
10 | + th Away Score | ||
11 | + th Points | ||
12 | + - pool.matches.each do |match| | ||
13 | + - bet = current_user.bets.find_or_initialize_by(match_id: match.id) | ||
14 | + tr | ||
15 | + td = l(match.date, format: :short) | ||
16 | + td = match.local | ||
17 | + td = bet.try(:score_local) | ||
18 | + td = match.visit | ||
19 | + td = bet.try(:score_visit) | ||
20 | + td = match.result ? match.result.upcase : 'PENDING' | ||
21 | + td = match.score_local | ||
22 | + td = match.score_visit | ||
23 | + td = bet.try(:points) | ||
24 | + |
app/views/bets/index.html.slim
0 → 100644
1 | +h2 My Bets | ||
2 | += simple_nested_form_for current_user, url: put_bets_path, method: :patch do |f| | ||
3 | + - @pools.each do |pool| | ||
4 | + div | ||
5 | + h3 = pool.name | ||
6 | + - if pool.editable? | ||
7 | + = render 'pool_form', pool: pool, f: f | ||
8 | + - else | ||
9 | + = render 'readonly_pool', pool: pool | ||
10 | + div | ||
11 | + = f.submit 'Save' |
... | @@ -2,6 +2,11 @@ Rails.application.routes.draw do | ... | @@ -2,6 +2,11 @@ Rails.application.routes.draw do |
2 | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | 2 | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html |
3 | HeimdallEngine.load_routes | 3 | HeimdallEngine.load_routes |
4 | root 'application#root' | 4 | root 'application#root' |
5 | + resources :bets, only: [:index] do | ||
6 | + collection do | ||
7 | + patch :update, as: :put | ||
8 | + end | ||
9 | + end | ||
5 | 10 | ||
6 | namespace :admin do | 11 | namespace :admin do |
7 | resources :pools | 12 | resources :pools | ... | ... |
-
Please register or login to post a comment