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

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.tags_to_names(the_tags, separator = Taggable.separator)
  if the_tags.is_a? Array
    names = the_tags 
  elsif the_tags.is_a? String
    names = the_tags.split(separator)
  end
  
  names = names.flatten.uniq.compact
  
  return names
end

Instance Method Details

#delete_all_tagsObject Also known as: clear_tags

Delete all tags from this taggable object.


153
154
155
156
157
158
159
# File 'lib/glue/taggable.rb', line 153

def delete_all_tags
  for tag in tags
    tag.reload
    tag.unlink
  end
  tags.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 = (tags.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(the_tags, options = {})
  options = {
    :clear => true
  }.merge(options)
    
  delete_all_tags() if options[:clear]
  
  for name in Taggable.tags_to_names(the_tags)
    the_tag = Tag.find_or_create_by_name(name)
    the_tag.tag(self)
  end    
end

#tag_namesObject

Return the names of the tags.


164
165
166
# File 'lib/glue/taggable.rb', line 164

def tag_names
  tags.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)
  tags.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)
  tags.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.

Returns:

  • (Boolean)

184
185
186
# File 'lib/glue/taggable.rb', line 184

def tagged_with?(tag_name)
  tag_names.include?(tag_name)
end