class ActsAsTaggableOnMongoid::Taggable::TaggedWithQuery::Base

A base class with shared code for match queries.

Attributes

options[R]
tag_definition[R]
tag_list[R]
taggable_model[R]

Public Class Methods

new(tag_definition, tag_list, options) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 13
def initialize(tag_definition, tag_list, options)
  @tag_definition = tag_definition
  @tag_list       = tag_list
  @options        = options
end

Private Instance Methods

build_ids_from(count_selector) click to toggle source

Any is relatively simple, but All and exclude are a bit more complicated. To make the code simpler I'm treating all of them the same. We build an aggregation of all of the matching taggables whose key_name is in the list of tags with the count of the matching key names. We then filter on that count.

  • All - count == tag_list count

  • Any - count > 0

  • Exclude - count > 0 (but anything that isn't in that count)

# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 29
def build_ids_from(count_selector)
  where_query = tagging_query.where(:tag_name.in => tag_list)
  build_ids_from_query(where_query, count_selector)
end
build_ids_from_query(where_query, count_selector) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 38
def build_ids_from_query(where_query, count_selector)
  pipeline = where_query.
      group(_id: { taggable_id: "$taggable_id", tag_name: "$tag_name" }).
      group(_id: "$_id.taggable_id", :count.sum => 1).
      pipeline.
      concat(count_selector.to_pipeline)

  tag_definition.taggings_table.collection.aggregate(pipeline).to_a.map { |counts| counts[:_id] }
end
build_tagless_ids_from(count_selector) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 34
def build_tagless_ids_from(count_selector)
  build_ids_from_query(tagging_query, count_selector)
end
tagging_query() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 48
def tagging_query
  context       = options[:on]
  tagging_query = tag_definition.taggings_table.where(taggable_type: tag_definition.owner.name)
  tagging_query = tagging_query.where(:context.in => context) if context.present?

  time_constraints tagging_query
end
time_constraints(tagging_query) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagged_with_query/base.rb, line 56
def time_constraints(tagging_query)
  start_at      = options[:start_at]
  end_at        = options[:end_at]

  tagging_query = tagging_query.where(:created_at.gte => start_at) if start_at.present?
  tagging_query = tagging_query.where(:created_at.lt => end_at) if end_at.present?

  tagging_query
end