Module: ActsAsMongoTaggable
- Defined in:
- lib/acts_as_mongo_taggable.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Class Method Summary collapse
Instance Method Summary collapse
- #arr_of_words(words) ⇒ Object
- #delete_tags_by_user(user) ⇒ Object
-
#tag(word_or_words, options = {}) ⇒ Object
tags, with optional user, but silently ignores if user tries to multi-tag with same word NOTE: automatically downcases each word unless you manually specify :case_sensitive=>true.
-
#tag!(word_or_words, options = {}) ⇒ Object
returns my current tag word list; with optional user, raises exception if user tries to multi-tag with same word when user specified or if duplicate tag.
-
#tagged_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil.
-
#untag(word_or_words, opts = {}) ⇒ Object
removes tag and all taggings from object NOTE: automatically downcases each word unless you manually specify :case_sensitive=>true.
Class Method Details
.included(receiver) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/acts_as_mongo_taggable.rb', line 219 def self.included(receiver) receiver.class_eval do key :tag_words, Array, :index => true many :model_tags ensure_index 'model_tags.word' ensure_index 'model_tags.tagging_count' end receiver.extend ClassMethods receiver.send :include, InstanceMethods Tag.register_taggable_type receiver end |
Instance Method Details
#arr_of_words(words) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/acts_as_mongo_taggable.rb', line 116 def arr_of_words(words) raise "Passed an invalid data type to tag()" unless words.is_a?(String) || words.is_a?(Array) if words.is_a?(String) words.squish.split(',').map{|w| w.squish} else words.map{|w| w.squish} end end |
#delete_tags_by_user(user) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/acts_as_mongo_taggable.rb', line 97 def (user) return false unless user return 0 if .blank? = (user) Tag.find(.map(&:tag_id)).each do |tag| user_taggings = tag.taggings.select{|tagging| tagging.user_id == user.id && tagging.taggable_type == self.class.name && tagging.taggable_id == self.id} user_taggings.each{|tagging| tag.taggings.delete tagging} tag.save_or_destroy end .each do |tag| tag.users.delete user destroy_if_empty(tag) end save reload end |
#tag(word_or_words, options = {}) ⇒ Object
tags, with optional user, but silently ignores if user tries to multi-tag with same word NOTE: automatically downcases each word unless you manually specify :case_sensitive=>true
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/acts_as_mongo_taggable.rb', line 154 def tag(word_or_words, ={}) user = [:user] arr_of_words(word_or_words).each do |word| word = word.downcase unless [:case_sensitive] == true if user.nil? || (user && !tag_words_by_user(user).include?(word)) model_tag = .detect{|tag| tag.word == word} if user || !model_tag #First add Tag/Tagging t = Tag.first(:word => word) || Tag.create!(:word => word) t.taggings << Tagging.new(:user => user, :taggable => self) t.save end #Now add ModelTag/User/tag_word unless model_tag model_tag = ModelTag.new(:word => word, :tag => t) self. << model_tag self.tag_words << word end model_tag.users << user if user end end save end |
#tag!(word_or_words, options = {}) ⇒ Object
returns my current tag word list; with optional user, raises exception if user tries to multi-tag with same word when user specified or if duplicate tag
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/acts_as_mongo_taggable.rb', line 127 def tag!(word_or_words, ={}) user = [:user] arr_of_words(word_or_words).each do |word| word = word.downcase unless [:case_sensitive] == true raise StandardError if (user && tag_words_by_user(user).include?(word)) || (model_tag = .detect{|tag| tag.word == word}) if user || !model_tag #First add Tag/Tagging t = Tag.first(:word => word) || Tag.create!(:word => word) t.taggings << Tagging.new(:user => user, :taggable => self) t.save end #Now add ModelTag/User/tag_word unless model_tag model_tag = ModelTag.new(:word => word, :tag => t) self. << model_tag self.tag_words << word end model_tag.users << user if user end save! end |
#tagged_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil
196 197 198 |
# File 'lib/acts_as_mongo_taggable.rb', line 196 def tagged_by_user?(user) !(.detect{|tag| tag.user_ids.include?(user.id)}.nil?) end |
#untag(word_or_words, opts = {}) ⇒ Object
removes tag and all taggings from object NOTE: automatically downcases each word unless you manually specify :case_sensitive=>true
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/acts_as_mongo_taggable.rb', line 202 def untag(word_or_words, ={}) return 0 if .blank? arr_of_words(word_or_words).each do |word| word = word.downcase unless [:case_sensitive] == true tag = Tag.first(:word => word) tagging_for_deletion = t.taggings.select{|tagging| tagging.taggable_type == self.class.name && tagging.taggable_id == self.id}.first tag.taggings.delete tagging_for_deletion tag.save_or_destroy end save reload end |