markbates/mongoid_delorean — pull request #5 by GUI (2013-12-18 06:40:38+00:00)
Title: Fix saving versions when safe mode is enabled

When the [safe mode option](http://mongoid.org/en/mongoid/docs/tips.html#safe_mode) is enabled for Mongoid, all new records fail to save when Mongoid Delorean is also enabled (all but one of the tests fail as soon as safe mode is enabled).

The save being triggered in the before_save callback was leading 2 save attempts for each record - once during the before_save and then once again during the normal save process. This second, normal save was then failing because a record already existed in Mongo. When safe mode is not enabled, Mongo's second save was silently failing, but with safe mode turned on, this error means each save action will result in a failure.

Since the save was only needed to update the version number for existing records, this is solved by performing a separate atomic update to that version field during the callback. This only happens for updates, since this secondary save isn't needed for new records at all.