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

Defined in:
lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb

Instance Method Summary collapse

Instance Method Details

#all_tags(scope = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 61

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

  reduce = %{
    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: 1)
  results.to_a.map! { |item| { name: item['_id'], count: item['value'].to_i } }
end

#scoped_tags(scope = {}) ⇒ Object



37
38
39
40
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 37

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

#tag_listObject



57
58
59
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 57

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

#tagged_with(tags) ⇒ Object



42
43
44
45
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 42

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

#tagged_with_all(tags) ⇒ Object



52
53
54
55
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 52

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

#tagged_without(tags) ⇒ Object



47
48
49
50
# File 'lib/workarea/ext/freedom_patches/mongoid_simple_tags.rb', line 47

def tagged_without(tags)
  tags = [tags] unless tags.is_a? Array
  criteria.nin(tags: tags)
end