Module: ActsAsMongoTaggable::InstanceMethods

Defined in:
lib/acts_as_mongo_taggable.rb

Instance Method Summary collapse

Instance Method Details

#_tagsObject



53
54
55
# File 'lib/acts_as_mongo_taggable.rb', line 53

def _tags
  Tag.find(:all, tag_ids)
end

#delete_all_tagsObject



44
45
46
47
48
49
50
51
# File 'lib/acts_as_mongo_taggable.rb', line 44

def delete_all_tags
  Tag.find(tag_ids).each do |tag|
    model_taggings = tag.taggings.select{|tagging| tagging.taggable_type == self.class.name && tagging.taggable_id == self.id}
    model_taggings.each{|tagging| tag.taggings.delete tagging}
    tag.save_or_destroy
  end
  update_attributes({ :model_tags => [], :tag_words => [] })
end

#model_tag(phrase, opts = {}) ⇒ Object



87
88
89
90
# File 'lib/acts_as_mongo_taggable.rb', line 87

def model_tag(phrase, opts = {})
  phrase = phrase.downcase unless opts[:case_sensitive]
  model_tags.detect{|tag| tag.word == phrase}
end

#tag_idsObject



92
93
94
# File 'lib/acts_as_mongo_taggable.rb', line 92

def tag_ids
  model_tags.map(&:tag_id)
end

#tag_words_by_user(user) ⇒ Object



72
73
74
# File 'lib/acts_as_mongo_taggable.rb', line 72

def tag_words_by_user(user)
  tags_by_user(user).map(&:word)
end

#tags(limit = nil) ⇒ Object

returns only the tag words, sorted by frequency; optionally can be limited



81
82
83
84
85
# File 'lib/acts_as_mongo_taggable.rb', line 81

def tags(limit=nil)
  array = tags_with_counts
  limit ||= array.size
  array[0,limit].map{|t| t[0]}
end

#tags_by_user(user) ⇒ Object



76
77
78
# File 'lib/acts_as_mongo_taggable.rb', line 76

def tags_by_user(user)
  model_tags.select{|tag| tag.user_ids.include? user.id}
end

#tags_with_countsObject

returns array of tags and counts:

[["matt", 3], ["bob", 2], ["bill", 1], ["joe", 1], ["frank", 1]]


59
60
61
62
# File 'lib/acts_as_mongo_taggable.rb', line 59

def tags_with_counts
  counts = model_tags.map{|tag| [tag.word, tag.tagging_count]}
  counts.sort{|a, b| b[1] <=> a[1]}
end

#tags_with_user_idsObject

returns an array of ids and user_ids



65
66
67
68
69
70
# File 'lib/acts_as_mongo_taggable.rb', line 65

def tags_with_user_ids
  model_tags.inject([]) do |arr, tag|
    tag.user_ids.each{|user_id| arr << [tag.id, user_id]}
    arr
  end
end