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, user, opts = {}) ⇒ Object
tags, 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, user) ⇒ Object
returns my current tag word list; raises exception if user tries to multi-tag with same word.
-
#tagged_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil.
Class Method Details
.included(receiver) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/acts_as_mongo_taggable.rb', line 178 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, user, opts = {}) ⇒ Object
tags, but silently ignores if user tries to multi-tag with same word NOTE: automatically downcases each word unless you manually specify :case_sensitive=>true
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/acts_as_mongo_taggable.rb', line 150 def tag(word_or_words, user, opts={}) arr_of_words(word_or_words).each do |word| word = word.downcase unless opts[:case_sensitive] == true unless tag_words_by_user(user).include?(word) #First add Tag/Tagging t = Tag.first(:word => word) || Tag.create!(:word => word) t.taggings << Tagging.new(:user => user, :taggable => self) t.save #Now add ModelTag/User/tag_word model_tag = .detect{|tag| tag.word == word} unless model_tag model_tag = ModelTag.new(:word => word, :tag => t) self. << model_tag self.tag_words << word end model_tag.users << user end end save end |
#tag!(word_or_words, user) ⇒ Object
returns my current tag word list; raises exception if user tries to multi-tag with same word
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/acts_as_mongo_taggable.rb', line 126 def tag!(word_or_words, user) arr_of_words(word_or_words).each do |word| raise StandardError if tag_words_by_user(user).include?(word) #First add Tag/Tagging t = Tag.first(:word => word) || Tag.create!(:word => word) t.taggings << Tagging.new(:user => user, :taggable => self) t.save! #Now add ModelTag/User/tag_word model_tag = .detect{|tag| tag.word == word} unless model_tag model_tag = ModelTag.new(:word => word, :tag => t) self. << model_tag self.tag_words << word end model_tag.users << user end save! end |
#tagged_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil
174 175 176 |
# File 'lib/acts_as_mongo_taggable.rb', line 174 def tagged_by_user?(user) !(.detect{|tag| tag.user_ids.include?(user.id)}.nil?) end |