diff options
author | Luke Shumaker <shumakl@purdue.edu> | 2014-04-27 20:24:36 -0400 |
---|---|---|
committer | Luke Shumaker <shumakl@purdue.edu> | 2014-04-27 20:24:36 -0400 |
commit | b8ce4ab9ad7e6359080e894958a3181cbb3895d2 (patch) | |
tree | 2d559ccffad353643a92286dd6836bbf491ef53b /app | |
parent | 410bbdce9752fb49ab711d4c148af1c6bbc2a53f (diff) |
touch up tournament creation
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/tournaments_controller.rb | 17 | ||||
-rw-r--r-- | app/models/game.rb | 4 | ||||
-rw-r--r-- | app/models/tournament.rb | 24 | ||||
-rw-r--r-- | app/views/tournaments/_form.html.erb | 25 |
4 files changed, 42 insertions, 28 deletions
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 @@ <p> <%= 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") %> </p> <% if @tournament.game.nil? %> @@ -50,32 +50,31 @@ <fieldset><legend>Settings</legend> <%= f.fields_for :settings do |setting_fields| %> - <% (@tournament.settings.empty? ? @tournament.game.settings : @tournament.settings).each do |setting| %><p> - <% name = setting.name.parameterize.underscore %> - <%= setting_fields.label name %> + <% @tournament.tournament_settings.each do |setting| %><p> + <%= setting_fields.label setting.name %> <br> <% case setting.vartype %> <% when 0 %> - <%= setting_fields.text_field( name ) %> + <%= setting_fields.text_field( setting.name ) %> <% when 1 %> - <%= setting_fields.text_area( name ) %> + <%= setting_fields.text_area( setting.name ) %> <% when 2 %> <ul> <% setting.type_opt.split(',').each do |option|%> - <li><label><%= setting_fields.radio_button( name, option ) %><%= option %></label></li> + <li><label><%= setting_fields.radio_button( setting.name, option ) %><%= option.humanize %></label></li> <% end %> </ul> <% when 3 %> <ul> <% setting.type_opt.split(',').each do |option|%> - <li><label><%= check_box_tag(name, value = option, checked = false, options = {}) %><%= option %></label></li> + <li><label><%= check_box_tag("tournament[settings][#{setting.name}][]", option, setting.value.split(',').include?(option)) %><%= option.humanize %></label></li> <% end %> </ul> <% when 4 %> - <%= setting_fields.radio_button( name, "true" ) %> True - <%= setting_fields.radio_button( name, "false" ) %> False + <%= setting_fields.radio_button( setting.name, "true" ) %> True + <%= setting_fields.radio_button( setting.name, "false" ) %> False <% when 5 %> - <%= setting_fields.select( name, setting.type_opt.split(',') ) %> + <%= setting_fields.select( setting.name, setting.type_opt.split(',') ) %> <% end %> </p><% end %> <% end %> @@ -83,8 +82,8 @@ <%= f.fields_for :stages do |stages_fields| %><fieldset><legend>Stages</legend> <label for="num_stages">Number of tournament stages</label> - <input type="number" name="num_stages" min="1" value="<%= params[:num_stages] ? params[:num_stages].to_i : 1 %>"> - <%# submit_tag("Set Stages", :class => "btn") %> + <input type="number" name="num_stages" min="1" value="<%= params[:num_stages].to_i %>"> + <%# stage_fields.submit("Set Stages") %> <% for i in 1..(params[:num_stages].to_i) do %> <%= stages_fields.fields_for i.to_s do |stage_fields| %><fieldset><legend>Stage <%= i %></legend> <%= stage_fields.label :scheduling_method %> |