Skip to content

Instantly share code, notes, and snippets.

@sjaveed
Last active August 29, 2015 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjaveed/9c554a8f67b55348f323 to your computer and use it in GitHub Desktop.
Save sjaveed/9c554a8f67b55348f323 to your computer and use it in GitHub Desktop.
class Game < ActiveRecord::Base
belongs_to :schedule
belongs_to :contestant_a, polymorphic: true
belongs_to :contestant_b, polymorphic: true
has_one :score, dependent: :destroy
def winner
score.winner
end
end
# This is the concrete game contestant
class GameContestant < ActiveRecord::Base
belongs_to :schedule
# Commented out because I don't think these are needed if we'll store accumulated scores here
# We can get those by iterating through the schedule, resolving all ResultContestants into GameContestants
# and then add scores to accumulated_score
# has_many :games_as_contestant_a, class_name: 'Game', foreign_key: :contestant_a_id, dependent: :destroy
# has_many :games_as_contestant_b, class_name: 'Game', foreign_key: :contestant_b_id, dependent: :destroy
validates_presence_of :name, :accumulated_score
def resolve
self
end
end
create_table :games do |t|
t.belongs_to :schedule, index: true
t.belongs_to :contestant_a, polymorphic: true
t.belongs_to :contestant_b, polymorphic: true
t.date :start_date
t.time :start_time
t.timestamps null: false
end
create_table :game_contestants do |t|
t.belongs_to :schedule, index: true
t.string :name, null: false
# Maybe a different name for this? Maybe just "points" since a "score" seems to imply number of goals and the like
t.integer :accumulated_score, null: false, default: 0
t.timestamps null: false
end
create_table :result_contestants do |t|
t.belongs_to :schedule, index: true
t.belongs_to :game
# Added this to store whether to use the winner of the game this belongs to.
# If it's set to false, it means to use the loser. Thoughts?
t.boolean :winner, null: false
# Is this meant to store the winner/loser functionality?
t.string :result_slot
t.timestamps null: false
end
class ResultContestant < ActiveRecord::Base
belongs_to :schedule
# has_many :games_as_contestant_a, class_name: 'Game', foreign_key: :contestant_a_id, dependent: :destroy
# has_many :games_as_contestant_b, class_name: 'Game', foreign_key: :contestant_b_id, dependent: :destroy
# this one is to allow referring to the game for which this result contestant talks about the winner/loser of
# halp, english?!
belongs_to :game
def resolve
if winner?
game.winner.resolve
else
game.loser.resolve
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment