psanchezp

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

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