Full-graph dirty tracking for MongoMapper
Switch branches/tags
Pull request Compare This branch is 3 commits ahead of PlasticLizard:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



mm-dirtier is a MongoMapper plugin that extends dirty tracking to the entire object graph. The default dirty tracking implementation in MongoMapper will only track dirty attributes for top level documents, and does not detect changes to hash or array keys. mm-dirtier, which currently only works with the Rails3 branch of MongoMapper, will record changes throughout your entire document hierarchy, including in place modifications to embedded array and hash keys.


mm-dirtier is available as a RubyGem:

gem install mm_dirtier

To activate the plugin, add 'mm_dirtier' to your gemfile and

include 'mm_dirtier'

when your app is initialized. mm-dirtier will take care of plugging itself into MongoMapper::Document and MongoMapper::EmbeddedDocument.


mm-dirtier was created to serve a single purpose: to enable MongoMapper to automatically persist changes to documents via MongoDB's atomic opererators. Whether or not the deep dirty tracking mm-dirtier provides has any use beyond that, I have no idea. For that reason I will simply provide a basic sample of plugin usage in this readme. If anyone conceives of a use for this plugin beyond document persistence and wants to know more, add an issue to the tracker stating as much and I'll expand the documentation.


class Procedure
  include MongoMapper::EmbeddedDocument

  key :name, String

class Diagnosis
  include MongoMapper::Document

  key :name, String

class Patient
  include MongoMapper::Document

  key :name, String
  key :nicknames, Array
  key :address, Hash

  one  :procedure
  many :procedures
  many :diagnosis_ids, Array
  many :diagnoses, :in => :diagnosis_ids

p = Patient.create :name=>"Nathan Stults"
p.changed? => false
p.nicknames << "Col. Easy Andy"
p.changed? => true
p.nicknames_changed? => true
p.nicknames_change => [[],["Col. Easy Andy"]]

p.address["city"] = "Santa Rosa"
p.address_changed? => true
p.address_change => [{},{"city"=>"Santa Rosa"}]

p.procedure.build :name => "Labotomy"
p.procedure_changed? => true
p.procedure_change => [nil,<Class ...>]
p.procedure.changed? => true
p.procedure.name_changed? => true

p.procedures_changed? => true
p.procedures_change? => [[],[<Class...]]
p.procedures[0].changed? => true
p.procedures[0].name_changed? => true
p.procedures[0].name_change => [nil,"Labotomy"]

#in_array associations are a little different than embedded associations
#because what is actually stored on the document is only the array of ids
#then that is what is tracked for changed.
p.diagnoses << Diagnosis.create(:name=>"Aspergers Syndrom")
p.diagnosis_ids_changed? => true
p.diagnosis_ids_change => [[],[BSON::ObjectId('...')]]


p.changed? => false
p.procedure.changed? => false
p.procedures[0].changed? => false

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.


Copyright © 1776 Nathan Stults. See LICENSE for details.