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

Methods included from Og::EntityMixin

#assign_properties, #delete, #force_save!, #insert, #og_clone, #og_quote, #properties_to_hash, #reload, #save, #saved?, #transaction, #update, #update_by_sql, #update_properties

Class Method Details

.included(base) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/glue/taggable.rb', line 218

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.



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/glue/taggable.rb', line 232

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.



140
141
142
143
144
145
# File 'lib/glue/taggable.rb', line 140

def delete_all_tags
  for tag in tags
    tag.unlink
  end
  tags.clear
end

#delete_tag(name) ⇒ Object

Delete a single tag from this taggable object.



132
133
134
135
136
# File 'lib/glue/taggable.rb', line 132

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.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/glue/taggable.rb', line 116

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.



150
151
152
# File 'lib/glue/taggable.rb', line 150

def tag_names
  tags.collect { |t| t.name }
end

#tag_string(separator = Taggable.separator) ⇒ Object

Return the tag string



156
157
158
# File 'lib/glue/taggable.rb', line 156

def tag_string(separator = Taggable.separator)
  tags.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.

Returns:

  • (Boolean)


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

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