tracks/app/models/tag.rb

31 lines
950 B
Ruby
Raw Normal View History

class Tag < ApplicationRecord
has_many :taggings
has_many :taggable, :through => :taggings
2014-08-14 21:05:05 -05:00
belongs_to :user
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
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