Module: Mongoid::Tracking::Aggregates::ClassMethods
- Defined in:
- lib/mongoid/tracking/aggregates.rb
Instance Method Summary collapse
-
#aggregate(name, &block) ⇒ Object
Defines an aggregate token to an already tracked model.
-
#aggregated? ⇒ Boolean
Return true if this model has aggregated data.
Instance Method Details
#aggregate(name, &block) ⇒ Object
Defines an aggregate token to an already tracked model. It defines a new mongoid model named after the original model.
Example:
class Page include Mongoid::Document include Mongoid::Tracking track :visits aggregate :browsers do |b| b.split(" ").first end end
A new model is defined as class PageAggregates
This model has the following structure:
belongs_to :page field :ns, :type => String field :key, :type => String index [:page_id, :ns, :key], :unique => true track :[original_parent_tracking_data] track :...
:ns is the “namespace”. It’s the name you put along the “aggregate :browsers” in the original model definition.
:key is your aggregation key. This is the value you are required to return in the “aggregate” block.
With the above structure, you can always query aggregates directly using Mongoid this way:
TestModelAggregates.where(:ns => "browsers", :key => "explorer").first
But you are encouraged to use Trackoid methods whenever possible.
60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/tracking/aggregates.rb', line 60 def aggregate(name, &block) raise Errors::AggregationAlreadyDefined.new(self.name, name) if aggregate_fields.has_key? name raise Errors::AggregationNameDeprecated.new(name) if DEPRECATED_TOKENS.include? name.to_s define_aggregate_model if aggregate_klass.nil? has_many internal_accessor_name(name), class_name: aggregate_klass.to_s add_aggregate_field(name, block) create_aggregation_accessors(name) end |
#aggregated? ⇒ Boolean
Return true if this model has aggregated data.
71 72 73 |
# File 'lib/mongoid/tracking/aggregates.rb', line 71 def aggregated? !aggregate_klass.nil? end |