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(‘great’, ‘gmosx’, ‘nitro’) article.tags article.tag_names Article.find_with_tags(‘great’, ‘gmosx’) Article.find_with_any_tag(‘name’, ‘gmosx’)
Tag.find_by_name(‘ruby’).articles
- clear_tags
- delete_all_tags
- delete_tag
- included
- tag
- tag!
- tag_names
- tag_string
- tag_string_linked
- tagged_by?
- tagged_with?
- tags_to_names
[ show source ]
# File lib/og/model/taggable.rb, line 247
247: def self.included(base)
248: Tag.many_to_many base
249: base.extend ClassMethods
250: base.many_to_many Tag
251: #--
252: # FIXME: Og should handle this automatically.
253: #++
254: base.before :on_delete do
255: tags.clear
256: end
257: end
Helper.
[ show source ]
# File lib/og/model/taggable.rb, line 261
261: def self.tags_to_names(the_tags, separator = Taggable.separator)
262: if the_tags.is_a? Array
263: names = the_tags
264: elsif the_tags.is_a? String
265: names = the_tags.split(separator)
266: end
267:
268: names = names.flatten.uniq.compact
269:
270: return names
271: end
Alias for #delete_all_tags
Delete all tags from this taggable object.
[ show source ]
# File lib/og/model/taggable.rb, line 157
157: def delete_all_tags
158: for tag in tags
159: tag.reload
160: tag.unlink
161: end
162: tags.clear
163: end
Delete a single tag from this taggable object.
[ show source ]
# File lib/og/model/taggable.rb, line 149
149: def delete_tag(name)
150: if dtag = (tags.delete_if { |t| t.name == name }).first
151: dtag.unlink
152: end
153: end
Add a tag for this object.
[ show source ]
# File lib/og/model/taggable.rb, line 133
133: def tag(the_tags, options = {})
134: options = {
135: :clear => true
136: }.merge(options)
137:
138: delete_all_tags() if options[:clear]
139:
140: for name in Taggable.tags_to_names(the_tags)
141: the_tag = Tag.find_or_create_by_name(name)
142: the_tag.tag(self)
143: end
144: end
Alias for #tag
Return the names of the tags.
[ show source ]
# File lib/og/model/taggable.rb, line 168
168: def tag_names
169: tags.collect { |t| t.name }
170: end
Return the tag string
[ show source ]
# File lib/og/model/taggable.rb, line 174
174: def tag_string(separator = ' ')
175: tags.collect { |t| t.name }.join(separator)
176: end
Return the linked tag string. Typically you will overrie this in your application.
[ show source ]
# File lib/og/model/taggable.rb, line 181
181: def tag_string_linked(separator = ' ')
182: tags.collect { |t| %|<a href="/tags/#{t.to_s_safe}">#{t.name}</a>| }.join(separator)
183: end
Alias for #tagged_with?
Checks to see if this object has been tagged with tag_name.
[ show source ]
# File lib/og/model/taggable.rb, line 188
188: def tagged_with?(tag_name)
189: tag_names.include?(tag_name)
190: end