Skip to content

Instantly share code, notes, and snippets.

@jarednorman
Created July 1, 2018 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarednorman/96bf696b05b061cf03e647a04423ac9f to your computer and use it in GitHub Desktop.
Save jarednorman/96bf696b05b061cf03e647a04423ac9f to your computer and use it in GitHub Desktop.
The Elo handling from PorkChop, without the other stuff in the model. (Original source: https://github.com/PorkChopClub/porkchop/blob/master/app/models/player.rb)
class Player < ActiveRecord::Base
BASE_ELO = 1000
has_many :elo_ratings
after_save :record_rating
attr_writer :elo
def elo
elo_ratings.most_recent_rating || BASE_ELO
end
def elo_on(date)
elo_ratings.rating_on(date) || BASE_ELO
end
private
def record_rating
return unless @elo
elo_ratings.create(rating: @elo)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment