Fix missing method for nil associations

This commit is contained in:
Jesse C. Fisher 2015-09-08 17:07:56 -07:00
parent f70d728c57
commit d03459746c
2 changed files with 14 additions and 7 deletions

View File

@ -109,7 +109,7 @@ class Game < ActiveRecord::Base
end
def hand_to_beat
self.play_to_beat.play.hand
self.play_to_beat.try(:play).try(:hand)
end
def player_to_beat

View File

@ -24,16 +24,19 @@ class Play < ActiveRecord::Base
# Validation methods
def hand_valid?
# Gather the truths about the hand
@truths = []
# Validations
# Is there a hand_to_beat ?
if self.game.play_to_beat
if game.hand_to_beat
# current player is not the player to beat
if (self.game.player_to_beat != self.player)
if (game.player_to_beat != player)
# hand_type match?
@truths << (self.hand_type == self.game.play_to_beat.play.hand_type)
@truths << (self.hand_type == game.hand_to_beat.hand_type) if game.hand_to_beat.try(:hand_type)
# beats?
@truths << (self.beats? self.game.play_to_beat.play)
@truths << (self.beats? self.game.hand_to_beat.try(:play))
end
end
@ -49,13 +52,17 @@ class Play < ActiveRecord::Base
if @truths.include? false
errors.add(:player_cards, "invalid hand")
end
# Did any validations return false?
return (!@truths.include? false)
end
# Check if the value of this hand is higher than the hand_to_beat
def beats?(play_to_beat)
@truth = self.high_card_value > play_to_beat.high_card_value
return @truth
if play_to_beat
self.high_card_value > play_to_beat.high_card_value
else
false
end
end
def high_card