diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/tournament.rb | 22 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | app/models/user_tournament_pair.rb | 4 |
3 files changed, 26 insertions, 2 deletions
diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 5e8ddfe..26dec72 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,4 +1,22 @@ class Tournament < ActiveRecord::Base - belongs_to :game - has_many :matches + belongs_to :game + has_many :matches + has_many :user_tournament_pairs + has_many :users, :through => :user_tournament_pairs + + def open? + return true + end + + def joinable_by?(user) + return ((not user.nil?) and user.in_group?(:player) and open?) + end + + def join(user) + unless joinable_by?(user) + return false + end + pair = UserTournamentPair.new(tournament: self, user: user) + return pair.save + end end diff --git a/app/models/user.rb b/app/models/user.rb index 9288ef6..bad7f7b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,6 @@ class User < ActiveRecord::Base + has_many :user_tournament_pairs + has_many :tournaments, :through => :user_tournament_pairs before_save { self.email = email.downcase } before_save { self.user_name = user_name } diff --git a/app/models/user_tournament_pair.rb b/app/models/user_tournament_pair.rb new file mode 100644 index 0000000..b2676e5 --- /dev/null +++ b/app/models/user_tournament_pair.rb @@ -0,0 +1,4 @@ +class UserTournamentPair < ActiveRecord::Base + belongs_to :user + belongs_to :tournament +end |