2008-12-22 23:26:38 +01:00
class Tag < ActiveRecord :: Base
2014-08-14 21:05:05 -05:00
2012-04-08 22:10:43 +02:00
has_many :taggings
has_many :taggable , :through = > :taggings
2014-08-14 21:05:05 -05:00
2015-08-19 15:24:35 +02: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
2012-04-17 16:47:37 +02: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
2015-08-19 15:24:35 +02:00
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
2015-08-19 15:24:35 +02:00
@label || = name . tr ( ' ' . freeze , '-' . freeze )
2013-02-18 15:01:56 -07:00
end
2013-02-18 18:48:22 -07:00
def to_s
name
end
2008-12-22 23:26:38 +01:00
end