From b8ce4ab9ad7e6359080e894958a3181cbb3895d2 Mon Sep 17 00:00:00 2001
From: Luke Shumaker
Date: Sun, 27 Apr 2014 20:24:36 -0400
Subject: touch up tournament creation
---
app/controllers/tournaments_controller.rb | 17 ++++++++++++++---
app/models/game.rb | 4 +++-
app/models/tournament.rb | 24 +++++++++++++-----------
app/views/tournaments/_form.html.erb | 25 ++++++++++++-------------
4 files changed, 42 insertions(+), 28 deletions(-)
(limited to 'app')
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 9c8ad7a..2e854a2 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -31,6 +31,17 @@ class TournamentsController < ApplicationController
# GET /tournaments/new
def new
@tournament = Tournament.new(tournament_attribute_params)
+ if @tournament.game
+ @tournament.game.settings.each do |game_setting|
+ @tournament.tournament_settings.build(
+ name: game_setting.name,
+ value: game_setting.value,
+ vartype: game_setting.vartype,
+ type_opt: game_setting.type_opt,
+ description: game_setting.description,
+ display_order: game_setting.display_order)
+ end
+ end
end
# GET /tournaments/1/edit
@@ -46,17 +57,17 @@ class TournamentsController < ApplicationController
ok = true
begin
ActiveRecord::Base.transaction do
- ok &= @tournament.save
ok &= @tournament.update(tournament_setting_params)
ok &= @tournament.hosts.push(current_user)
for i in 1..(params[:num_stages].to_i) do
begin
- ok &= @tournament.stages.create(tournament_stage_params(i))
+ ok &= @tournament.stages.build(tournament_stage_params(i))
rescue ActionController::ParameterMissing => e
ok = false
- @tournament.errors.add("stages[#{i}]", "Stage #{i} not set")
+ @tournament.errors.add("stages[#{i}]", "needs to be set")
end
end
+ ok &= @tournament.save
end
rescue ActiveRecord::RecordNotUnique => e
ok = false
diff --git a/app/models/game.rb b/app/models/game.rb
index 5f4c46d..c5cb32a 100644
--- a/app/models/game.rb
+++ b/app/models/game.rb
@@ -1,5 +1,7 @@
class Game < ActiveRecord::Base
belongs_to :parent, class_name: "Game"
has_many :children, class_name: "Game"
- has_many :settings, class_name: "GameSetting"
+ has_many :game_settings
+
+ alias_attribute :settings, :game_settings
end
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index 97eee57..a569213 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,11 +1,13 @@
class Tournament < ActiveRecord::Base
belongs_to :game
- has_many :stages, class_name: "TournamentStage"
+ has_many :tournament_stages
has_many :brackets
- has_many :settings_raw, class_name: "TournamentSetting"
+ has_many :tournament_settings
has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments"
has_and_belongs_to_many :hosts, class_name: "User", association_foreign_key: "host_id", join_table: "hosts_tournaments"
+ alias_attribute :stages, :tournament_stages
+
# Settings #################################################################
def settings
@@ -25,7 +27,7 @@ class Tournament < ActiveRecord::Base
end
def [](setting_name)
- tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
+ tournament_setting = @tournament.tournament_settings.find{|s|s.name==setting_name}
if tournament_setting.nil?
return nil
else
@@ -34,13 +36,13 @@ class Tournament < ActiveRecord::Base
end
def []=(setting_name, val)
- tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
+ tournament_setting = @tournament.tournament_settings.find{|s|s.name==setting_name}
if tournament_setting.nil?
game_setting = @tournament.game.settings.find_by_name(setting_name)
- @tournament.settings_raw.create(name: setting, value: val,
- vartype: game_setting.vartype,
- type_opt: game_setting.type_opt,
- description: game_setting.description,
+ @tournament.tournament_settings.build(name: setting_name, value: val,
+ vartype: game_setting.vartype,
+ type_opt: game_setting.type_opt,
+ description: game_setting.description,
display_order: game_setting.display_order)
else
tournament_setting.value = val
@@ -48,7 +50,7 @@ class Tournament < ActiveRecord::Base
end
def keys
- @tournament.settings_raw.all.collect { |x| x.name }
+ @tournament.tournament_settings.all.collect { |x| x.name }
end
def empty?() keys.empty? end
@@ -58,9 +60,9 @@ class Tournament < ActiveRecord::Base
def method_missing(name, *args)
if name.to_s.ends_with?('=')
- self[name.to_s.sub(/=$/, '').to_sym] = args.first
+ self[name.to_s.sub(/=$/, '').to_s] = args.first
else
- return self[name.to_sym]
+ return self[name.to_s]
end
end
end
diff --git a/app/views/tournaments/_form.html.erb b/app/views/tournaments/_form.html.erb
index cb4c175..329fc5f 100644
--- a/app/views/tournaments/_form.html.erb
+++ b/app/views/tournaments/_form.html.erb
@@ -7,7 +7,7 @@
<%= f.label :game_id, "Select a game type" %>
<%= f.select(:game_id, Game.all.map{|game| [game.name, game.id]}) %>
- <%= submit_tag("Select", :class => "btn") %>
+ <%= f.submit("Select") %>
<% if @tournament.game.nil? %>
@@ -50,32 +50,31 @@
<% end %>
<% end %>
@@ -83,8 +82,8 @@
<%= f.fields_for :stages do |stages_fields| %>