Class OverallBar
In: app/models/competitions/overall_bar.rb
Parent: Competition

See Bar class

Methods

Public Instance methods

Only count the top 5 disciplines

[Source]

    # File app/models/competitions/overall_bar.rb, line 79
79:   def after_create_competition_results_for(race)
80:     race.results.each do |result|
81:       result.scores.sort! { |x, y| y.points <=> x.points }
82:       remove_duplicate_discipline_results result.scores
83: 
84:       if result.scores.size > 5
85:         lowest_scores = result.scores[5, result.scores.size - 5]
86:         lowest_scores.each do |lowest_score|
87:           result.scores.destroy lowest_score
88:         end
89:         # Rails destroys Score in database, but doesn't update the current association
90:         result.scores true
91:       end
92:     end
93:   end

[Source]

     # File app/models/competitions/overall_bar.rb, line 148
148:   def create_children
149:     Discipline.find_all_bar.reject { |discipline| [Discipline[:age_graded], Discipline[:overall], Discipline[:team]].include?(discipline) }.each do |discipline|
150:       bar = Bar.find(:first, :conditions => { :date => date, :discipline => discipline.name })
151:       unless bar
152:         bar = Bar.create!(
153:           :parent => self,
154:           :name => "#{year} #{discipline.name} BAR",
155:           :date => date,
156:           :discipline => discipline.name
157:         )
158:       end
159:     end
160:   end

[Source]

     # File app/models/competitions/overall_bar.rb, line 136
136:   def create_races
137:     for category_name in [
138:       'Senior Men', 'Category 3 Men', 'Category 4/5 Men',
139:       'Senior Women', 'Category 3 Women', 'Category 4 Women', 
140:       'Junior Men', 'Junior Women', 'Masters Men', 'Masters Women', 
141:       'Singlespeed/Fixed', 'Tandem', "Clydesdale"]
142: 
143:       category = Category.find_or_create_by_name(category_name)
144:       self.races.create(:category => category)
145:     end
146:   end

[Source]

     # File app/models/competitions/overall_bar.rb, line 166
166:   def default_discipline
167:     "Overall"
168:   end

[Source]

    # File app/models/competitions/overall_bar.rb, line 15
15:   def equivalent_category_for(category_friendly_param, discipline)
16:     return nil unless category_friendly_param && discipline
17:     
18:     if discipline == Discipline[:overall]
19:       event = self
20:     else
21:       event = children.detect { |child| child.discipline == discipline.name }
22:       return unless event
23:     end
24:     
25:     category = event.categories.detect { |cat| cat.friendly_param == category_friendly_param }
26: 
27:     if category.nil?
28:       event.categories.detect { |cat| cat.friendly_param == category_friendly_param || cat.parent.friendly_param == category_friendly_param } || 
29:       event.categories.sort.first
30:     else
31:       category
32:     end
33:   end

[Source]

    # File app/models/competitions/overall_bar.rb, line 3
 3:   def find_race(discipline, category)
 4:     if Discipline[:overall] == discipline
 5:       event = self
 6:     else
 7:       event = children.detect { |e| e.discipline == discipline.name }
 8:     end
 9:     
10:     if event
11:       event.races.detect { |e| e.category == category }
12:     end
13:   end

[Source]

     # File app/models/competitions/overall_bar.rb, line 162
162:   def friendly_name
163:     'Overall BAR'
164:   end

Array of ids (integers) race category, race category‘s siblings, and any competition categories Overall BAR does some awesome mappings for MTB and DH

[Source]

    # File app/models/competitions/overall_bar.rb, line 55
55:   def mtb_category_ids_for(race)
56:     return "NULL" unless race.category
57:     
58:     case race.category.name
59:     when "Senior Men"
60:       categories = [Category.find_or_create_by_name("Pro Men")]
61:     when "Senior Women"
62:       categories = [Category.find_or_create_by_name("Pro Women"), Category.find_or_create_by_name("Category 1 Women")]
63:     when "Category 3 Men"
64:       categories = [Category.find_or_create_by_name("Category 1 Men")]
65:     when "Category 3 Women"
66:       categories = [Category.find_or_create_by_name("Category 2 Women")]
67:     when "Category 4/5 Men"
68:       categories = [Category.find_or_create_by_name("Category 2 Men"), Category.find_or_create_by_name("Category 3 Men")]
69:     when "Category 4 Women"
70:       categories = [Category.find_or_create_by_name("Category 3 Women")]
71:     else
72:       categories = [race.category]      
73:     end
74:     
75:     categories.map(&:id).join ", "
76:   end

[Source]

    # File app/models/competitions/overall_bar.rb, line 35
35:   def points_for(scoring_result)
36:     301 - scoring_result.place.to_i
37:   end

If person scored in more than one category that maps to same overall category in a discipline, count only highest-placing category. This typically happens for age-based categories like Masters and Juniors Assume scores sorted in preferred order (usually by points descending) For the Category 4/5 Overall BAR, if a person has both a Cat 4 and Cat 5 result for the same discipline, we only count the Cat 4 result

[Source]

     # File app/models/competitions/overall_bar.rb, line 101
101:   def remove_duplicate_discipline_results(scores)
102:     cat_4 = Category.find_by_name("Category 4 Men")
103:     cat_5 = Category.find_by_name("Category 5 Men")
104:     scores_to_delete = []
105:     cat_4_disciplines = []
106: 
107:     scores.each do |score|
108:       race = score.source_result.race
109:       if race.category == cat_4 
110:         cat_4_disciplines << race.discipline
111:       end
112:     end
113: 
114:     scores.each do |score|
115:       race = score.source_result.race
116:       if race.category == cat_5 && cat_4_disciplines.include?(race.discipline)
117:         logger.debug("Cat 4 result already exists: #{race.discipline} results for #{score.source_result.person}: #{race.category.name}")
118:         scores_to_delete << score
119:       end
120:     end
121:     scores_to_delete.each { |score| scores.delete(score) }
122:     
123:     scores_to_delete = []
124:     disciplines = []
125:     scores.each do |score|
126:       if disciplines.include?(score.source_result.race.discipline)
127:         logger.debug("Multiple #{score.source_result.race.discipline} results for #{score.source_result.person}: #{score.source_result.race.category.name}")
128:         scores_to_delete << score
129:       else
130:         disciplines << score.source_result.race.discipline
131:       end
132:     end
133:     scores_to_delete.each { |score| scores.delete(score) }
134:   end

[Source]

    # File app/models/competitions/overall_bar.rb, line 39
39:   def source_results(race)
40:     Result.find(:all,
41:                 :include => [:race, {:person => :team}, :team, {:race => [:event, :category]}],
42:                 :conditions => [%Q{events.type = 'Bar' 
43:                   and place between 1 and 300
44:                   and ((events.discipline not in ("Mountain Bike", "Downhill", "Short Track") and categories.id in (#{category_ids_for(race)}))
45:                     or ((events.discipline in ("Mountain Bike", "Downhill", "Short Track")) and categories.id in (#{mtb_category_ids_for(race)})))
46:                   and events.date >= '#{date.year}-01-01' 
47:                   and events.date <= '#{date.year}-12-31'}],
48:                 :order => 'person_id'
49:     )
50:   end

[Validate]