2008-12-22 23:26:38 +01:00
# The Tag model. This model is automatically generated and added to your app if
# you run the tagging generator included with has_many_polymorphs.
class Tag < ActiveRecord :: Base
DELIMITER = " , " # 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 = " , "
# 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
2011-09-30 12:06:43 +02:00
2008-12-22 23:26:38 +01:00
# Change this validation if you need more complex tag names.
# validates_format_of :name, :with => /^[a-zA-Z0-9\_\-]+$/, :message => "can not contain special characters"
2011-09-30 12:06:43 +02:00
2008-12-22 23:26:38 +01:00
# Set up the polymorphic relationship.
2011-09-30 12:06:43 +02:00
has_many_polymorphs :taggables ,
:from = > [ :todos , :recurring_todos ] ,
:through = > :taggings ,
2008-12-22 23:26:38 +01:00
:dependent = > :destroy ,
2011-09-30 12:06:43 +02:00
:skip_duplicates = > false ,
2008-12-22 23:26:38 +01:00
:parent_extend = > proc {
# Defined on the taggable models, not on Tag itself. Return the tagnames
# associated with this record as a string.
def to_s
self . map ( & :name ) . sort . join ( Tag :: JOIN_DELIMITER )
end
2011-09-30 12:06:43 +02:00
def all_except_starred
self . reject { | tag | tag . name == Todo :: STARRED_TAG_NAME }
end
2008-12-22 23:26:38 +01:00
}
2011-09-30 12:06:43 +02: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
2008-12-22 23:26:38 +01:00
self . name = name . downcase . strip . squeeze ( " " )
end
def on ( taggable , user )
taggings . create :taggable = > taggable , :user = > user
end
2011-09-30 12:06:43 +02:00
2008-12-22 23:26:38 +01:00
# Tag::Error class. Raised by ActiveRecord::Base::TaggingExtensions if
# something goes wrong.
class Error < StandardError
end
2011-09-30 12:06:43 +02:00
2008-12-22 23:26:38 +01:00
end