Module: Mongoid::Tracking::ClassMethods

Defined in:
lib/mongoid/tracking.rb

Instance Method Summary collapse

Instance Method Details

#create_tracked_fields(name) ⇒ Object



75
76
77
# 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) ⇒ Object

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.



98
99
100
101
102
# 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) ⇒ Object

Returns the internal representation of the tracked field name



81
82
83
# File 'lib/mongoid/tracking.rb', line 81

def internal_track_name(name)
  "#{name}_data".to_sym
end

#set_tracking_field(name) ⇒ Object

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



87
88
89
90
91
92
# 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) ⇒ Object

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.



68
69
70
71
72
73
# 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) ⇒ Object

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



105
106
107
# File 'lib/mongoid/tracking.rb', line 105

def update_aggregates(name)
  aggregate_klass.track name
end