Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/splits-io. a speedrunning data store and analysis engine splits.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566class Entry < ApplicationRecord belongs_to :race, touch: true belongs_to :runner, class_name: 'User', optional: true belongs_to :creator, class_name: 'User' belongs_to :run, dependent: :destroy, optional: true
validates_with EntryValidator # Validators are not called before destroy's, so manually hook and prevent if race is started before_destroy :validate_destroy
scope :ready, -> { where.not(readied_at: nil) } scope :nonready, -> { where(readied_at: nil) }
scope :active, -> { where(finished_at: nil, forfeited_at: nil) } scope :inactive, -> { where(finished_at: nil, forfeited_at: nil) }
scope :ghosts, -> { where(ghost: true) } scope :nonghosts, -> { where(ghost: false) }
scope :unfinished, -> { where(finished_at: nil) } scope :unforfeited, -> { where(forfeited_at: nil) }
# Returns this race's entry for the given user, not including ghosts. def self.find_for(user) find_by(runner: user, ghost: false) end
def ready? readied_at.present? end
def finished? finished_at.present? end
def forfeited? forfeited_at.present? end
def done? finished? || forfeited? end
def place return nil unless finished?
race.entries.order(finished_at: :asc).pluck(:id).index(id) + 1 end
def duration return Duration.new((finished_at - race.started_at) * 1000) if finished? return Duration.new((forfeited_at - race.started_at) * 1000) if forfeited?
Duration.new(nil) end
private
def validate_destroy return unless race.started?
errors[:base] << 'Cannot leave race once it has started' throw(:abort) endend