Class Category
In: app/models/category.rb
Parent: ActiveRecord::Base

Senior Men, Pro 1/2, Novice Masters 45+

Categories are just a simple hierarchy of names

Categories are basically labels and there is no complex hierarchy. In other words, Senior Men Pro 1/2 and Pro 1/2 are two distinct categories. They are not combinations of Pro and Senior and Men and Cat 1

friendly_param is used for friendly links on BAR pages. Example: senior_men

Methods

Included Modules

Export::Categories Comparable

Constants

NONE = Category.new(:name => "", :id => nil)

Public Class methods

[Source]

   # File app/models/export/categories.rb, line 5
5:     def Category.export
6:       Category.export_head
7:       Category.export_data
8:     end

[Source]

    # File app/models/export/categories.rb, line 34
34:     def Category.export_columns
35:       [ "id", "parent_id", "name" ]
36:     end

[Source]

    # File app/models/export/categories.rb, line 16
16:     def Category.export_data
17:       Base.export(export_data_sql, "categories.csv")
18:     end

[Source]

    # File app/models/export/categories.rb, line 26
26:     def Category.export_data_sql
27:       "SELECT #{Category.export_columns.join(",")}
28:        INTO OUTFILE '%s'
29:        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
30:        LINES TERMINATED BY '\\n'
31:        FROM categories"
32:     end

[Source]

    # File app/models/export/categories.rb, line 12
12:     def Category.export_head
13:       Base.export(export_head_sql, "categories.txt")
14:     end

[Source]

    # File app/models/export/categories.rb, line 20
20:     def Category.export_head_sql
21:       "SELECT '#{Category.export_columns.join("','")}'
22:        INTO OUTFILE '%s'
23:        FIELDS TERMINATED BY '\\n'"
24:     end

All categories with no parent (except root ‘association’ category)

[Source]

    # File app/models/category.rb, line 30
30:   def Category.find_all_unknowns
31:     Category.find(
32:       :all, 
33:       :conditions => ['parent_id is null and name <> ?', RacingAssociation.current.short_name],
34:       :include => :children
35:      )
36:   end

[Source]

    # File app/models/category.rb, line 38
38:   def Category.find_by_friendly_param(param)
39:     category_count = Category.count(:conditions => ['friendly_param = ?', param])
40:     case category_count
41:     when 0
42:       nil
43:     when 1
44:       Category.find(:first, :conditions => ['friendly_param = ?', param])
45:     else
46:       raise AmbiguousParamException, "#{category_count} occurrences of #{param}"
47:     end
48:   end

Sr, Mst, Jr, Cat, Beg, Exp

[Source]

    # File app/models/category.rb, line 51
51:   def Category.short_name(name)
52:     return name if name.blank?
53:     name.gsub('Senior', 'Sr').gsub('Masters', 'Mst').gsub('Junior', 'Jr').gsub('Category', 'Cat').gsub('Beginner', 'Beg').gsub('Expert', 'Exp').gsub("Clydesdale", "Clyd")
54:   end

Public Instance methods

Compare by position, then by name

[Source]

     # File app/models/category.rb, line 100
100:   def <=>(other)
101:     return 0 if self[:id] and self[:id] == other[:id]
102:     diff = (position <=> other.position)
103:     if diff == 0
104:       name <=> other.name
105:     else
106:       diff
107:     end
108:   end

Return Range

[Source]

    # File app/models/category.rb, line 73
73:   def ages
74:     self.ages_begin..ages_end
75:   end

Accepts an age Range like 10..18, or a String like 10-18

[Source]

    # File app/models/category.rb, line 78
78:   def ages=(value)
79:     case value
80:     when Range
81:       self.ages_begin = value.begin
82:       self.ages_end = value.end
83:     else
84:       age_split = value.strip.split('-')
85:       self.ages_begin = age_split[0].to_i unless age_split[0].nil?
86:       self.ages_end = age_split[1].to_i unless age_split[1].nil?
87:     end
88:   end

All children and children childrens

[Source]

    # File app/models/category.rb, line 91
91:   def descendants
92:     _descendants = children(true)
93:     children.each do |child|
94:       _descendants = _descendants + child.descendants
95:     end
96:     _descendants
97:   end

Default to blank

[Source]

    # File app/models/category.rb, line 63
63:   def name
64:     self[:name] || ''
65:   end

[Source]

    # File app/models/category.rb, line 56
56:   def parent_is_not_self
57:     if parent_id && parent_id == id
58:       errors.add 'parent', 'Category cannot be its own parent'
59:     end
60:   end

Sr, Mst, Jr, Cat, Beg, Exp

[Source]

    # File app/models/category.rb, line 68
68:   def short_name
69:     Category.short_name name
70:   end

Lowercase underscore

[Source]

     # File app/models/category.rb, line 111
111:   def to_friendly_param
112:     name.underscore.gsub('+', '_plus').gsub(/[^\w]+/, '_').gsub(/^_/, '').gsub(/_$/, '').gsub(/_+/, '_')
113:   end

[Source]

     # File app/models/category.rb, line 115
115:   def to_s
116:     "#<Category #{id} #{parent_id} #{position} #{name}>"
117:   end

[Validate]