Module: Mongoid::TaggableWithContext::AggregationStrategy::RealTime::ClassMethods

Defined in:
lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb

Instance Method Summary collapse

Instance Method Details

#aggregation_collection_for(context) ⇒ Object



21
22
23
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 21

def aggregation_collection_for(context)
  "#{collection_name}_#{context}_aggregation"
end

#aggregation_database_collection_for(context) ⇒ Object

Collection name for storing results of tag count aggregation



17
18
19
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 17

def aggregation_database_collection_for(context)
  (@aggregation_database_collection ||= {})[context] ||= Moped::Collection.new(self.collection.database, aggregation_collection_for(context))
end

#recalculate_all_context_tag_weights!Object



35
36
37
38
39
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 35

def recalculate_all_context_tag_weights!
  tag_contexts.each do |context|
    recalculate_tag_weights!(context)
  end
end

#recalculate_tag_weights!(context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 41

def recalculate_tag_weights!(context)
  field = tag_options_for(context)[:array_field]

  map = <<-END
    function() {
      if (!this.#{field})return;
      for (index in this.#{field})
        emit(this.#{field}[index], 1);
    }
  END

  reduce = <<-END
    function(key, values) {
      var count = 0;
      for (index in values) count += values[index];
      return count;
    }
  END

  self.map_reduce(map, reduce).out(replace: aggregation_collection_for(context)).time
end

#tag_name_attributeObject



11
12
13
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 11

def tag_name_attribute
  "_id"
end

#tags_autocomplete(context, criteria, options = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 64

def tags_autocomplete(context, criteria, options={})
  result = aggregation_database_collection_for(context).find({tag_name_attribute.to_sym => /^#{criteria}/})
  result = result.sort(value: -1) if options[:sort_by_count] == true
  result = result.limit(options[:max]) if options[:max] > 0
  result.to_a.map{ |r| [r[tag_name_attribute], r["value"]] }
end

#tags_for(context, conditions = {}) ⇒ Object



25
26
27
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 25

def tags_for(context, conditions={})
  aggregation_database_collection_for(context).find({:value => {"$gt" => 0 }}).sort(tag_name_attribute.to_sym => 1).to_a.map{ |t| t[tag_name_attribute] }
end

#tags_with_weight_for(context, conditions = {}) ⇒ Object

retrieve the list of tag with weight(count), this is useful for creating tag clouds



31
32
33
# File 'lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb', line 31

def tags_with_weight_for(context, conditions={})
  aggregation_database_collection_for(context).find({:value => {"$gt" => 0 }}).sort(tag_name_attribute.to_sym => 1).to_a.map{ |t| [t[tag_name_attribute], t["value"].to_i] }
end