Module: Mongoid::Document::Taggable::ClassMethods

Defined in:
lib/mongoid-simple-tags.rb

Instance Method Summary collapse

Instance Method Details

#all_tags(scope = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongoid-simple-tags.rb', line 32

def all_tags(scope = {})
  map = %Q{
    function() {
      if(this.tags){
        this.tags.forEach(function(tag){
          emit(tag, 1)
        });
      }
    }
  }

  reduce = %Q{
    function(key, values) {
      var tag_count = 0 ;
      values.forEach(function(value) {
        tag_count += value;
      });
      return tag_count;
    }
  }

  tags = self
  tags = tags.where(scope) if scope.present?

  results = tags.map_reduce(map, reduce).out(inline: true)
  results.to_a.map!{ |item| { :name => item['_id'], :count => item['value'].to_i } }
end

#scoped_tags(scope = {}) ⇒ Object



60
61
62
63
# File 'lib/mongoid-simple-tags.rb', line 60

def scoped_tags(scope = {})
  warn "[DEPRECATION] `scoped_tags` is deprecated.  Please use `all_tags` instead."
  all_tags(scope)
end

#tag_listObject



75
76
77
# File 'lib/mongoid-simple-tags.rb', line 75

def tag_list
  self.all_tags.collect{|tag| tag[:name]}
end

#tagged_with(tags) ⇒ Object



65
66
67
68
# File 'lib/mongoid-simple-tags.rb', line 65

def tagged_with(tags)
  tags = [tags] unless tags.is_a? Array
  criteria.in(:tags => tags)
end

#tagged_with_all(tags) ⇒ Object



70
71
72
73
# File 'lib/mongoid-simple-tags.rb', line 70

def tagged_with_all(tags)
  tags = [tags] unless tags.is_a? Array
  criteria.all(:tags => tags)
end