class CombinedTimeTrialResults

All categories’ results in a time trial by time Adds combined_results if Time Trial Event. Destroy combined_results if they exist, but should not All the calculation happens synchronously, which isn’t ideal. Logic overlaps heavily with Competition as well.

Public Class Methods

allows_combined_results?(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 42
def self.allows_combined_results?(event)
  event.discipline == "Time Trial"
end
create_combined_results(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 46
def self.create_combined_results(event)
  event.create_combined_results unless event.combined_results
end
create_or_destroy_for!(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 11
def self.create_or_destroy_for!(event)
  return event.combined_results unless event.notification_enabled?
  event.disable_notification!

  if destroy_combined_results?(event)
    destroy_combined_results(event)
  elsif requires_combined_results?(event)
    create_combined_results(event)
    destroy_combined_results(event) unless event.combined_results.has_results?
  end

  event.enable_notification!
  event.combined_results
end
destroy_combined_results(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 30
def self.destroy_combined_results(event)
  if event.combined_results
    event.combined_results.destroy_races
    event.combined_results(true).destroy
    event.combined_results = nil
  end
end
destroy_combined_results?(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 26
def self.destroy_combined_results?(event)
  !requires_combined_results?(event) || (event.combined_results(true) && !event.combined_results.has_results?)
end
requires_combined_results?(event) click to toggle source
# File app/models/combined_time_trial_results.rb, line 38
def self.requires_combined_results?(event)
  allows_combined_results?(event) && event.auto_combined_results? && event.has_results?(true)
end

Public Instance Methods

calculate!() click to toggle source
# File app/models/combined_time_trial_results.rb, line 84
def calculate!
  destroy_races
  combined_race = races.create!(:category => Category.find_or_create_by_name("Combined"))
  parent.races.each do |race|
    race.results.each do |result|
      if result.place.to_i > 0 && result.time && result.time > 0
        combined_race.results.create!(
          :person => result.person,
          :team => result.team,
          :time => result.time,
          :category => race.category
        )
      end
    end
  end
  combined_race.results.sort! do |x, y|
    if x.time
      if y.time
        x.time <=> y.time
      else
        1
      end
    else
      -1
    end
  end
  place = 1
  combined_race.results.each do |result|
    result.update_attribute(:place, place.to_s)
    place = place + 1
  end
  true
end
default_bar_points() click to toggle source
# File app/models/combined_time_trial_results.rb, line 50
def default_bar_points
  0
end
default_discipline() click to toggle source
# File app/models/combined_time_trial_results.rb, line 54
def default_discipline
  "Time Trial"
end
default_ironman() click to toggle source
# File app/models/combined_time_trial_results.rb, line 58
def default_ironman
  false
end
default_name() click to toggle source
# File app/models/combined_time_trial_results.rb, line 62
def default_name
  "Combined"
end
disable_notification!() click to toggle source
# File app/models/combined_time_trial_results.rb, line 70
def disable_notification!
  false
end
enable_notification!() click to toggle source
# File app/models/combined_time_trial_results.rb, line 66
def enable_notification!
  false
end
set_mandatory_defaults() click to toggle source
# File app/models/combined_time_trial_results.rb, line 74
def set_mandatory_defaults
  self.bar_points = default_bar_points
  self.discipline = default_discipline
  self.ironman = default_ironman
  self.name = default_name
  self.auto_combined_results = false
  self.notification = false
  true
end