Module: Glue::Taggable
- Includes:
- Og::EntityMixin
- Defined in:
- lib/glue/taggable.rb
Overview
Add tagging methods to the target class. For more information on the algorithms used surf: www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
Example
class Article
include Taggable
..
end
article.tag(‘navel’, ‘gmosx’, ‘nitro’) article.tags article.tag_names Article.find_with_tags(‘navel’, ‘gmosx’) Article.find_with_any_tag(‘name’, ‘gmosx’)
Tag.find_by_name(‘ruby’).articles
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#delete_all_tags ⇒ Object
(also: #clear_tags)
Delete all tags from this taggable object.
-
#delete_tag(name) ⇒ Object
Delete a single tag from this taggable object.
-
#tag(the_tags, options = {}) ⇒ Object
(also: #tag!)
Add a tag for this object.
-
#tag_names ⇒ Object
Return the names of the tags.
-
#tag_string(separator = Taggable.separator) ⇒ Object
Return the tag string.
-
#tagged_with?(tag_name) ⇒ Boolean
(also: #tagged_by?)
Checks to see if this object has been tagged with
tag_name
.
Methods included from Og::EntityMixin
#assign_properties, #delete, #force_save!, #insert, #og_clone, #og_quote, #properties_to_hash, #reload, #save, #saved?, #to_rexml, #to_xml, #transaction, #update, #update_by_sql, #update_properties
Class Method Details
.included(base) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/glue/taggable.rb', line 225 def self.included(base) Tag.module_eval do many_to_many base end base.extend(ClassMethods) #-- # FIXME: Og should handle this automatically. #++ base.send :include, ::Aspects base.before 'tags.clear', :on => [:og_delete] end |
.tags_to_names(the_tags, separator = Taggable.separator) ⇒ Object
Helper.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/glue/taggable.rb', line 239 def self.(, separator = Taggable.separator) if .is_a? Array names = elsif .is_a? String names = .split(separator) end names = names.flatten.uniq.compact return names end |
Instance Method Details
#delete_all_tags ⇒ Object Also known as:
Delete all tags from this taggable object.
143 144 145 146 147 148 |
# File 'lib/glue/taggable.rb', line 143 def for tag in tag.unlink end .clear end |
#delete_tag(name) ⇒ Object
Delete a single tag from this taggable object.
135 136 137 138 139 |
# File 'lib/glue/taggable.rb', line 135 def delete_tag(name) if dtag = (.delete_if { |t| t.name == name }).first dtag.unlink end end |
#tag(the_tags, options = {}) ⇒ Object Also known as: tag!
Add a tag for this object.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/glue/taggable.rb', line 119 def tag(, = {}) = { :clear => true }.merge() () if [:clear] for name in Taggable.() the_tag = Tag.find_or_create_by_name(name) the_tag.tag(self) end end |
#tag_names ⇒ Object
Return the names of the tags.
153 154 155 |
# File 'lib/glue/taggable.rb', line 153 def tag_names .collect { |t| t.name } end |
#tag_string(separator = Taggable.separator) ⇒ Object
Return the tag string
159 160 161 |
# File 'lib/glue/taggable.rb', line 159 def tag_string(separator = Taggable.separator) .collect { |t| t.name }.join(separator) end |
#tagged_with?(tag_name) ⇒ Boolean Also known as: tagged_by?
Checks to see if this object has been tagged with tag_name
.
166 167 168 |
# File 'lib/glue/taggable.rb', line 166 def tagged_with?(tag_name) tag_names.include?(tag_name) end |