module Mongoid::TaggableWithContext::AggregationStrategy::RealTime::ClassMethods

Public Instance Methods

aggregation_collection_for(context) click to toggle source
# 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) click to toggle source

Collection name for storing results of tag count aggregation

# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 17
def aggregation_database_collection_for(context)
  (@aggregation_database_collection ||= {})[context] ||= Mongo::Collection.new(self.collection.database, aggregation_collection_for(context))
end
recalculate_all_context_tag_weights!() click to toggle source
# 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) click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 41
      def recalculate_tag_weights!(context)
        db_field = self.class.tag_options_for(context)[:db_field]

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

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

        self.class.map_reduce(map, reduce).out(replace: aggregation_collection_for(context)).time
      end
tag_name_attribute() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 11
def tag_name_attribute
  "_id"
end
tags_autocomplete(context, criteria, options={}) click to toggle source

adapted from github.com/jesuisbonbon/mongoid_taggable/commit/42feddd24dedd66b2b6776f9694d1b5b8bf6903d

# 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={}) click to toggle source
# 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={}) click to toggle source

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

# 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