2018-11-03 15:57:14 -05:00
class Tag < ApplicationRecord
2012-04-08 22:10:43 +02:00
has_many :taggings
has_many :taggable , :through = > :taggings
2014-08-14 21:05:05 -05:00
2019-06-25 03:15:23 +03:00
belongs_to :user
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.
2020-10-27 21:39:19 +02:00
validates :name , presence : true , uniqueness : { :scope = > " user_id " , :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