Mongoid Activity Tracker

Build Status Gem Version Coverage Status

Module to help with tracking activity in your application.


Add this line to your application's Gemfile:

gem 'mongoid_activity_tracker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid_activity_tracker


Setup a class that will record the activity:

class MyTracker
  include MongoidActivityTracker::Tracker

Track the activity:

res = MyTracker.track(current_user, :create) # returns the current_user
res.actor_cache # => { to_s: … }
res.actor_cache_object.to_s # the 'actor_cache_object' wraps the actor_cache hash into an OpenStruct
res.action # => :create


Along the action, it is possible to track any number of related documents.

First, configure the tracker class using the .tracks macro:

class MyTracker
  include MongoidActivityTracker::Tracker
  tracks :subject

Then, specify the subject when tracking the activity:

res = MyTracker.track(current_user, :create, subject: my_subject)

res.subject # => my_subject
res.subject_cache # => { to_s: … }
res.subject_cache_object.to_s # …

By default, the :cache_methods parameter is set to store the result of :to_s method. This is valuable for example when displaying activity overview (no need for additional query for the related document), or should the related document no longer exist. The list of methods to be cached can be customised as follows:

class MyTracker
  include MongoidActivityTracker::Tracker
  tracks :actor, cache_methods: [:to_s, :first_name, :last_name]
  tracks :subject, cache_methods: [:to_s, :some_other_method]

The tracker class can be also subclassed:

class MyTrackerWithTarget < MyTracker
  tracks :target


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request