module Mongoid::TaggableWithContext::AggregationStrategy::RealTime

Protected Instance Methods

get_conditions(context, tag) click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 74
def get_conditions(context, tag)
  {self.class.tag_name_attribute.to_sym => tag}
end
update_tags_aggregation(context, old_tags=[], new_tags=[]) click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 78
def update_tags_aggregation(context, old_tags=[], new_tags=[])
  coll = self.class.aggregation_database_collection_for(context)

  old_tags ||= []
  new_tags ||= []
  unchanged_tags  = old_tags & new_tags
  tags_removed    = old_tags - unchanged_tags
  tags_added      = new_tags - unchanged_tags


  tags_removed.each do |tag|
    coll.update_many(get_conditions(context, tag), {'$inc' => {value: -1}}, upsert: true)
  end
  tags_added.each do |tag|
    coll.update_many(get_conditions(context, tag), {'$inc' => {value: 1}}, upsert: true)
  end
end
update_tags_aggregations_on_destroy() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 106
def update_tags_aggregations_on_destroy
  self.class.tag_database_fields.each do |field|
    old_tags = send field
    new_tags = []
    update_tags_aggregation(field, old_tags, new_tags)
  end
end
update_tags_aggregations_on_save() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb, line 96
def update_tags_aggregations_on_save
  indifferent_changes = HashWithIndifferentAccess.new changes
  self.class.tag_database_fields.each do |field|
    next if indifferent_changes[field].nil?

    old_tags, new_tags = indifferent_changes[field]
    update_tags_aggregation(field, old_tags, new_tags)
  end
end