diff --git a/app/models/game.rb b/app/models/game.rb index 465823a..4b0b693 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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 diff --git a/app/models/play.rb b/app/models/play.rb index 87fd546..3b4463f 100644 --- a/app/models/play.rb +++ b/app/models/play.rb @@ -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