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 end
def hand_to_beat def hand_to_beat
self.play_to_beat.play.hand self.play_to_beat.try(:play).try(:hand)
end end
def player_to_beat def player_to_beat

View File

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