From 50371ba04b52f74bfc2ceb05a429e072ad4848bb Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 15:23:30 -0500 Subject: add a user_tournament_pair --- app/models/user_tournament_pair.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/models/user_tournament_pair.rb (limited to 'app/models') 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 -- cgit v1.2.3-2-g168b From 1788dde36e53c4ef16adc5db2d19f44797325496 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 18:55:38 -0500 Subject: implement tournament joining --- app/models/tournament.rb | 19 ++++++++++++++++++- app/models/user.rb | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/tournament.rb b/app/models/tournament.rb index cc915a0..afdd27e 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,3 +1,20 @@ class Tournament < ActiveRecord::Base - belongs_to :game + belongs_to :game + has_many :users, :through => :user_tournament_pair + + 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?(user) + return false + end + pair = new_user_tournament_pair(user: user) + return pair.save + end end diff --git a/app/models/user.rb b/app/models/user.rb index 079b870..976ecf4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + has_many :tournaments, :through => :user_tournament_pair before_save { self.email = email.downcase } before_save { self.user_name = user_name } -- cgit v1.2.3-2-g168b From 7bcd854443e368806cf1f4ece562c157db723d1a Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 20:37:53 -0500 Subject: fix joining a tournament --- app/models/tournament.rb | 7 ++++--- app/models/user.rb | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/tournament.rb b/app/models/tournament.rb index ca7fade..26dec72 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,7 +1,8 @@ class Tournament < ActiveRecord::Base belongs_to :game has_many :matches - has_many :users, :through => :user_tournament_pair + has_many :user_tournament_pairs + has_many :users, :through => :user_tournament_pairs def open? return true @@ -12,10 +13,10 @@ class Tournament < ActiveRecord::Base end def join(user) - unless joinable?(user) + unless joinable_by?(user) return false end - pair = new_user_tournament_pair(user: user) + 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 6405c8e..bad7f7b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ class User < ActiveRecord::Base - has_many :tournaments, :through => :user_tournament_pair + 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 } -- cgit v1.2.3-2-g168b