class OregonCup

Year-long best rider competition for senior men and women. obra.org/oregon_cup

Public Instance Methods

category_ids_for(race) click to toggle source
# File app/models/competitions/oregon_cup.rb, line 54
def category_ids_for(race)
  ids = [race.category_id]
  ids = ids + race.category.descendants.map(&:id)
  if race.category == Category.find_or_create_by_name('Senior Women')
    cat_3_women = Category.find_or_create_by_name('Category 3 Women')
    ids << cat_3_women.id
    ids = ids + cat_3_women.descendants.map(&:id)
  end
  ids
end
create_races() click to toggle source
# File app/models/competitions/oregon_cup.rb, line 65
def create_races
  category = Category.find_or_create_by_name('Senior Men')
  races.create :category => category

  category = Category.find_or_create_by_name('Senior Women')
  races.create :category => category
end
friendly_name() click to toggle source
# File app/models/competitions/oregon_cup.rb, line 3
def friendly_name
  'Oregon Cup'
end
point_schedule() click to toggle source
# File app/models/competitions/oregon_cup.rb, line 7
def point_schedule
  [ 0, 100, 75, 60, 50, 45, 40, 35, 30, 25, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10 ]
end
remove_duplicate_results(results) click to toggle source

Women are often raced together and then scored separately. Combined Women 1/2/3 results count for Oregon Cup. Mark Oregon Cup race by adding “Oregon Cup” to event name, race name, event notes, or race notes.

# File app/models/competitions/oregon_cup.rb, line 39
def remove_duplicate_results(results)
  results.delete_if do |result|
    results.any? do |other_result|
      result != other_result &&
      result.race_id != other_result.race_id &&
      result.event.root == other_result.event.root &&
      (
        other_result.race.notes.include?("Oregon Cup") ||
        other_result.event.notes.include?("Oregon Cup") || 
        other_result.event.name.include?("Oregon Cup")
      )
    end
  end
end
source_results(race) click to toggle source

#source_results must be in person-order

# File app/models/competitions/oregon_cup.rb, line 12
def source_results(race)
  return [] if source_events(true).empty?
  
  event_ids = source_events.collect do |event|
    event.id
  end
  event_ids = event_ids.join(', ')
  
  results = Result.find_by_sql(
    %Q{SELECT results.* FROM results  
        LEFT OUTER JOIN races ON races.id = results.race_id 
        LEFT OUTER JOIN categories ON categories.id = races.category_id
        LEFT OUTER JOIN events ON races.event_id = events.id 
          WHERE races.category_id is not null 
            and place between 1 and 20
            and categories.id in (#{category_ids_for(race).join(", ")})
            and (results.category_id is null or results.category_id in (#{category_ids_for(race).join(", ")}))
            and (events.id in (#{event_ids}) or events.parent_id in (#{event_ids}))
       order by person_id
     }
  )
  remove_duplicate_results(results)
  results
end