module Mongoid::Tracking::ClassMethods

Public Instance Methods

create_tracked_fields(name) click to toggle source
# File lib/mongoid/tracking.rb, line 75
def create_tracked_fields(name)
  field "#{name}_data".to_sym, type: Hash, default: {}
end
create_tracking_accessors(name) click to toggle source

Creates the tracking field accessor and also disables the original ones from Mongoid. Hidding here the original accessors for the Mongoid fields ensures they doesn't get dirty, so Mongoid does not overwrite old data.

# File lib/mongoid/tracking.rb, line 98
def create_tracking_accessors(name)
  define_method(name) do |*aggr|
    Tracker.new(self, name, aggr)
  end
end
internal_track_name(name) click to toggle source

Returns the internal representation of the tracked field name

# File lib/mongoid/tracking.rb, line 81
def internal_track_name(name)
  "#{name}_data".to_sym
end
set_tracking_field(name) click to toggle source

Configures the internal fields for tracking. Additionally also creates an index for the internal tracking field.

# File lib/mongoid/tracking.rb, line 87
def set_tracking_field(name)
  # DONT make an index for this field. MongoDB indexes have limited
  # size and seems that this is not a good target for indexing.
  # index internal_track_name(name)
  tracked_fields << name
end
track(name) click to toggle source
Adds analytics tracking for +name+. Adds a +'name'_data+ mongoid
field as a Hash for tracking this information. Additionaly, hiddes

 the field, so that the user can not mangle with the original one.

This is necessary so that Mongoid does not "dirty" the field
potentially overwriting the original data.
# File lib/mongoid/tracking.rb, line 68
def track(name)
  set_tracking_field(name.to_sym)
  create_tracking_accessors(name.to_sym)
  create_tracked_fields(name)
  update_aggregates(name.to_sym) if aggregated?
end
update_aggregates(name) click to toggle source

Updates the aggregated class for it to include a new tracking field

# File lib/mongoid/tracking.rb, line 105
def update_aggregates(name)
  aggregate_klass.track name
end