Module: ActsAsMongoTaggable

Defined in:
lib/acts_as_mongo_taggable.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

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 delete_tags_by_user(user)
  return false unless user
  return 0 if model_tags.blank?
  user_tags = tags_by_user(user)
  
  Tag.find(user_tags.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
  
  user_tags.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, options={})
  user = options[:user]
  arr_of_words(word_or_words).each do |word|
    word = word.downcase unless options[:case_sensitive] == true
    if user.nil? || (user && !tag_words_by_user(user).include?(word))
      model_tag = model_tags.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_tags << model_tag
        self.tag_words << word
      end
      model_tag.users << user if user
    end
  end
  save
  tags
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, options={})
  user = options[:user]
  arr_of_words(word_or_words).each do |word|
    word = word.downcase unless options[:case_sensitive] == true
    raise StandardError if (user && tag_words_by_user(user).include?(word)) || (model_tag = model_tags.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_tags << model_tag
      self.tag_words << word
    end
    model_tag.users << user if user
  end
  save!
  tags
end

#tagged_by_user?(user) ⇒ Boolean

returns the Rating object found if user has rated this project, else returns nil

Returns:

  • (Boolean)


196
197
198
# File 'lib/acts_as_mongo_taggable.rb', line 196

def tagged_by_user?(user)
  !(model_tags.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, options={})
  return 0 if model_tags.blank?
  arr_of_words(word_or_words).each do |word|
    word = word.downcase unless options[: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