validate game startable
This commit is contained in:
parent
0c621d8f89
commit
52ab57fc51
@ -1,31 +1,31 @@
|
||||
# The game class
|
||||
class Game < ActiveRecord::Base
|
||||
has_many :players, :dependent => :destroy
|
||||
has_many :players, dependent: :destroy
|
||||
has_many :users, through: :players
|
||||
accepts_nested_attributes_for :players
|
||||
validates :title, presence: true
|
||||
|
||||
def start
|
||||
# Save the game to the database
|
||||
return false unless startable?
|
||||
save
|
||||
# Start the game TODO
|
||||
end
|
||||
|
||||
def validate_startable
|
||||
@player_count = players.size
|
||||
@player_count = players.count
|
||||
errors[:notice] = 'Must be at least 2 players.' if @player_count < 2
|
||||
errors[:players] = 'Must be less than 5 players.' if @player_count > 4
|
||||
end
|
||||
|
||||
def is_startable?
|
||||
def startable?
|
||||
@player_count = players.size
|
||||
@player_count.between?(2, 4)
|
||||
end
|
||||
|
||||
def add_player_from_user(user)
|
||||
if can_accomodate(user)
|
||||
Player.create(user: user, game: self)
|
||||
end
|
||||
return false unless can_accomodate(user)
|
||||
self.players.create(user: user)
|
||||
end
|
||||
|
||||
def can_accomodate(user)
|
||||
|
||||
@ -42,6 +42,6 @@ RSpec.describe Game, type: :model do
|
||||
end
|
||||
|
||||
it 'Can not be started if game is invalid' do
|
||||
expect(@game.start).to eq false
|
||||
expect(@game.start).to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user