tracks/app/models/tag.rb

32 lines
924 B
Ruby
Raw Permalink Normal View History

2008-12-22 23:26:38 +01:00
class Tag < ActiveRecord::Base
2014-08-14 21:05:05 -05:00
has_many :taggings
has_many :taggable, :through => :taggings
2014-08-14 21:05:05 -05:00
DELIMITER = ",".freeze # Controls how to split and join tagnames from strings. You may need to change the <tt>validates_format_of parameters</tt> if you change this.
JOIN_DELIMITER = ", ".freeze
2008-12-22 23:26:38 +01:00
# If database speed becomes an issue, you could remove these validations and
# rescue the ActiveRecord database constraint errors instead.
validates_presence_of :name
validates_uniqueness_of :name, :case_sensitive => false
2014-08-14 21:05:05 -05:00
before_create :before_create
2014-08-14 21:05:05 -05:00
2008-12-22 23:26:38 +01:00
# Callback to strip extra spaces from the tagname before saving it. If you
# allow tags to be renamed later, you might want to use the
# <tt>before_save</tt> callback instead.
2011-09-30 12:06:43 +02:00
def before_create
self.name = name.downcase.strip.squeeze(' '.freeze)
2008-12-22 23:26:38 +01:00
end
2013-02-18 15:01:56 -07:00
def label
@label ||= name.tr(' '.freeze, '-'.freeze)
2013-02-18 15:01:56 -07:00
end
def to_s
name
end
2008-12-22 23:26:38 +01:00
end