class Category

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

Constants

NONE

Public Class Methods

export() click to toggle source
# File app/models/export/categories.rb, line 5
def Category.export
  Category.export_head
  Category.export_data
end
export_columns() click to toggle source
# File app/models/export/categories.rb, line 34
def Category.export_columns
  [ "id", "parent_id", "name" ]
end
export_data() click to toggle source
# File app/models/export/categories.rb, line 16
def Category.export_data
  Base.export(export_data_sql, "categories.csv")
end
export_data_sql() click to toggle source
# File app/models/export/categories.rb, line 26
def Category.export_data_sql
  "SELECT #{Category.export_columns.join(",")}
   INTO OUTFILE '%s'
   FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
   LINES TERMINATED BY '\\n'
   FROM categories"
end
export_head() click to toggle source
# File app/models/export/categories.rb, line 12
def Category.export_head
  Base.export(export_head_sql, "categories.txt")
end
export_head_sql() click to toggle source
# File app/models/export/categories.rb, line 20
def Category.export_head_sql
  "SELECT '#{Category.export_columns.join("','")}'
   INTO OUTFILE '%s'
   FIELDS TERMINATED BY '\\n'"
end
find_all_unknowns() click to toggle source

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

# File app/models/category.rb, line 30
def Category.find_all_unknowns
  Category.all(
    :conditions => ['parent_id is null and name <> ?', RacingAssociation.current.short_name],
    :include => :children
   )
end
short_name(name) click to toggle source

Sr, Mst, Jr, Cat, Beg, Exp

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

Public Instance Methods

<=>(other) click to toggle source

Compare by position, then by name

# File app/models/category.rb, line 64
def <=>(other)
  return 0 if self[:id] && self[:id] == other[:id]
  diff = (position <=> other.position)
  if diff == 0
    name <=> other.name
  else
    diff
  end
end
descendants() click to toggle source

All children and children childrens

# File app/models/category.rb, line 55
def descendants
  _descendants = children(true)
  children.each do |child|
    _descendants = _descendants + child.descendants
  end
  _descendants
end
parent_is_not_self() click to toggle source
# File app/models/category.rb, line 43
def parent_is_not_self
  if parent_id && parent_id == id
    errors.add 'parent', 'Category cannot be its own parent'
  end
end
short_name() click to toggle source

Sr, Mst, Jr, Cat, Beg, Exp

# File app/models/category.rb, line 50
def short_name
  Category.short_name name
end
to_s() click to toggle source
# File app/models/category.rb, line 74
def to_s
  "#<Category #{id} #{parent_id} #{position} #{name}>"
end