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(‘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
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.
-
#tag_string_linked(separator = Taggable.separator) ⇒ Object
Return the linked 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_attributes, #delete, #force_save!, #insert, #instance_attribute_set, #og_quote, #properties_to_hash, #reload, #save, #save_building_collections, #saved?, #set_attributes, #transaction, #update, #update_attributes, #update_by_sql
Class Method Details
.included(base) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/glue/taggable.rb', line 239 def self.included(base) Tag.many_to_many base base.extend ClassMethods base.many_to_many Tag #-- # 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.
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/glue/taggable.rb', line 252 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.
153 154 155 156 157 158 159 |
# File 'lib/glue/taggable.rb', line 153 def for tag in tag.reload tag.unlink end .clear end |
#delete_tag(name) ⇒ Object
Delete a single tag from this taggable object.
145 146 147 148 149 |
# File 'lib/glue/taggable.rb', line 145 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.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/glue/taggable.rb', line 129 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.
164 165 166 |
# File 'lib/glue/taggable.rb', line 164 def tag_names .collect { |t| t.name } end |
#tag_string(separator = Taggable.separator) ⇒ Object
Return the tag string
170 171 172 |
# File 'lib/glue/taggable.rb', line 170 def tag_string(separator = Taggable.separator) .collect { |t| t.name }.join(separator) end |
#tag_string_linked(separator = Taggable.separator) ⇒ Object
Return the linked tag string. Typically you will overrie this in your application.
177 178 179 |
# File 'lib/glue/taggable.rb', line 177 def tag_string_linked(separator = Taggable.separator) .collect { |t| %|<a href="/tags/#{t.name}">#{t.name}</a>| }.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
.
184 185 186 |
# File 'lib/glue/taggable.rb', line 184 def tagged_with?(tag_name) tag_names.include?(tag_name) end |