psanchezp

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

...@@ -13,4 +13,5 @@ ...@@ -13,4 +13,5 @@
13 //= require jquery 13 //= require jquery
14 //= require jquery_ujs 14 //= require jquery_ujs
15 //= require turbolinks 15 //= require turbolinks
16 +//= require jquery_nested_form
16 //= require_tree . 17 //= require_tree .
......
1 -class PoolsController < CrudController::Base 1 +class Admin::PoolsController < CrudController::Base
2 - enable_gatekeeper 2 + # enable_gatekeeper
3 - guard_bifrost 3 + # guard_bifrost
4 4
5 + def update
6 + if update!
7 + redirect_to admin_pools_path
8 + else
9 + render :edit
10 + end
11 + end
12 +
13 + private
14 +
15 + def object_params
16 + params.require(:pool).permit(
17 + :name, :editable_until, :active,
18 + matches_attributes: [:id, :_destroy, :local, :visit, :score_local,
19 + :score_visit, :date]
20 + )
21 + end
5 end 22 end
......
...@@ -4,4 +4,32 @@ class Bet < ApplicationRecord ...@@ -4,4 +4,32 @@ class Bet < ApplicationRecord
4 belongs_to :user, inverse_of: :bets 4 belongs_to :user, inverse_of: :bets
5 5
6 scope :active, -> { joins(match: :pool).where(pools: { active: true }) } 6 scope :active, -> { joins(match: :pool).where(pools: { active: true }) }
7 + before_save :assign_result
8 +
9 + def calculate
10 + return unless match.result
11 + update_column :points, calculate_result
12 + end
13 +
14 + def calculate_result
15 + return 5 if final_score_is_equal
16 + return 3 if result == match.result
17 + 0
18 + end
19 +
20 + def assign_result
21 + self.result = find_result
22 + end
23 +
24 + private
25 +
26 + def final_score_is_equal
27 + match.score_local == score_local && match.score_visit == score_visit
28 + end
29 +
30 + def find_result
31 + return :tie if local_score == visit_score
32 + return :local if local_score > visit_score
33 + :visit
34 + end
7 end 35 end
......
...@@ -7,4 +7,24 @@ class Match < ApplicationRecord ...@@ -7,4 +7,24 @@ class Match < ApplicationRecord
7 7
8 scope :active, -> { joins(:pool).where(pools: { active: true}) } 8 scope :active, -> { joins(:pool).where(pools: { active: true}) }
9 scope :upcoming, -> { active.where('date > ?', Time.zone.now) } 9 scope :upcoming, -> { active.where('date > ?', Time.zone.now) }
10 +
11 + before_save :calculate
12 + after_save :calculate_bets
13 +
14 +
15 + def calculate
16 + return unless local_score && visit_score
17 + self.result = find_result
18 + end
19 +
20 + def find_result
21 + return :tie if local_score == visit_score
22 + return :local if local_score > visit_score
23 + :visit
24 + end
25 +
26 + def calculate_bets
27 + return unless result
28 + bets.each(&:calculate)
29 + end
10 end 30 end
......
1 += simple_nested_form_for [:admin, @object] do |f|
2 + = f.input :name
3 + = f.input :editable_until
4 + = f.input :active
5 + hr
6 + h4
7 + | Matches
8 + = f.link_to_add 'Add Match', :matches, data: { target: '#matches' }, style: 'float:right'
9 +
10 + table
11 + tbody#matches
12 + = f.fields_for :matches, wrapper: false do |ff|
13 + tr
14 + td = ff.input_field :date, placeholder: 'Date'
15 + td = ff.input_field :local, placeholder: 'Local'
16 + td = ff.input_field :visit, placeholder: 'Visit'
17 + td = ff.input_field :score_local, placeholder: 'Score Local'
18 + td = ff.input_field :score_visit, placeholder: 'Score Visit'
19 + td = ff.object.result ? ff.object.result.upcase : 'Pending Result'
20 + td
21 + = ff.link_to_remove 'Remove'
22 + div style='text-align: center'
23 + = f.submit 'Save'
24 +
25 +
1 +h2
2 + | New Pool
3 +
4 += render 'form'
1 +h2
2 + | Pools
3 + = link_to 'New Pool', new_admin_pool_path, style: 'float: right'
4 +
5 +- attrs = %i[name editable_until active]
6 +table
7 + thead
8 + - attrs.each do |a|
9 + th = Pool.human_attribute_name(a)
10 + th Matches
11 + th
12 + tbody
13 + tbody
14 + - @object_collection.each do |model|
15 + - attrs.each do |a|
16 + td = model.send(a)
17 + td #{model.matches.count} matches
18 + td
19 + = link_to 'Edit', edit_admin_pool_path(model)
20 +
1 +h2
2 + | New Pool
3 +
4 += render 'form'
...@@ -3,5 +3,7 @@ Rails.application.routes.draw do ...@@ -3,5 +3,7 @@ Rails.application.routes.draw do
3 HeimdallEngine.load_routes 3 HeimdallEngine.load_routes
4 root 'application#root' 4 root 'application#root'
5 5
6 + namespace :admin do
6 resources :pools 7 resources :pools
8 + end
7 end 9 end
......