53 lines
1.2 KiB
Ruby

class Seat < ActiveRecord::Base
belongs_to :table
belongs_to :game
belongs_to :user
def sit(current_user)
if self.occupied?
return "Error. Seat is occupied."
else
# Stand up from any other seats at this table
current_user.stand_from(self.table)
self.user_id = current_user.id
self.user_soft_token = current_user.soft_token
self.save
self.table.current_game.add_player_from_user(current_user)
return "Successfully sat."
end
end
def stand(current_user)
unless self.occupied_by?(current_user)
return "Error. Seat not occupied by you."
else
self.user_id = nil
self.user_soft_token = nil
self.save
self.table.current_game.remove_player_from_user(current_user)
return "Successfully stood up."
end
end
def occupied?
self.user_id.present? || self.user_soft_token.present?
end
def occupied_by? user
if user.id.present?
self.user_id == user.id
end
if user.soft_token.present?
self.user_soft_token == user.soft_token
end
end
def user_display_name
return nil unless self.occupied?
self.user.try(:email) || "Guest" + self.user_soft_token.to_s[0..4]
end
end