summaryrefslogtreecommitdiff
path: root/app/models/tournament.rb
diff options
context:
space:
mode:
authorguntasgrewal <guntasgrewal@gmail.com>2014-04-06 23:30:47 -0400
committerguntasgrewal <guntasgrewal@gmail.com>2014-04-06 23:30:47 -0400
commit3602d46e95f9a13ec2a8ff0b0909059af64c55ba (patch)
tree1dbf1149e8f6915f0c07b7a9aef1bf790a7b41c5 /app/models/tournament.rb
parent2d097c71a32646fce3b90608cbffde9992c979ef (diff)
parentedec37791164f1c32c8071784a8a02aa63afae55 (diff)
Merge branch 'master' of https://github.com/LukeShu/leaguer
Diffstat (limited to 'app/models/tournament.rb')
-rw-r--r--app/models/tournament.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index 72c3ac8..fdcdba2 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,9 +1,56 @@
class Tournament < ActiveRecord::Base
belongs_to :game
has_many :matches
+ has_many :preferences_raw, class_name: "TournamentPreference"
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"
+ def preferences
+ @preferences ||= Preferences.new(self)
+ end
+ def preferences=(pref)
+ pref.each do |key, value|
+ value = false if valuedd == "0"
+ preferences[key] = value
+ end
+ end
+
+ class Preferences
+ def initialize(tournament)
+ @tournament = tournament
+ end
+
+ def [](preference)
+ p = @tournament.preferences_raw.find_by_name(preference)
+ if p.nil?
+ return nil
+ else
+ return p.value
+ end
+ end
+
+ def []=(preference, value)
+ p = @tournament.preferences_raw.find_by_name(preference)
+ if p.nil?
+ # TODO: create it
+ else
+ p.value = value
+ end
+ end
+
+ def keys
+ @tournament.preferences_raw.all.collect { |x| x.name }
+ end
+
+ def method_missing(name, *args)
+ if name.to_s.ends_with?('=')
+ self[name.to_s.sub(/=$/, '').to_sym] = args.first
+ else
+ return self[name.to_sym]
+ end
+ end
+ end
+
def open?
return true
end