No commit activity in last 3 years
No release in over 3 years
Versioning Mongoid documents by means of separate collection.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 5.0.0
 Project Readme

Mongoid Versioning

Build Status Gem Version Coverage Status

Versioning of Mongoid documents. Past versions are stored in separate collection.

This gem is intended for versioning of whole documents. However, it might be possible to extend its support to embedded documents as well. Pull requests are welcome.

Installation

Add this line to your application's Gemfile:

gem 'mongoid_versioning'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid_versioning

Usage

Include the MongoidVersioning::Versioned module into your model:

class MyVersionedDocument
    include Mongoid::Document
    include MongoidVersioning::Versioned
end

Your class will then have:

field :_version, type: Integer
field :_based_on_version, type: Integer

Creating versions

To create new version of your document:

doc = MyVersionedDocument.new
doc.revise # => true
doc._version # => 1
doc._based_on_version # => nil

The #revise method validates the document and runs :revise, :save and :update callbacks. (Please note that running #revise on new document will resort to standard #save.)

A #revise! method, raising Mongoid::Errors::Validations and Mongoid::Errors::Callbacks exceptions, is also available.

Retrieving versions

To access all previous versions:

doc.previous_versions # => Mongoid::Criteria

These versions are stored in separate collection, by default named by appending .versions to name of the source collection. In the above example it is my_versioned_documents.versions.

To access latest version (as stored in the db):

doc.latest_version # => MyVersionedDocument

To retrieve all versions of a document:

doc.versions # => Array

To retrieve specific version:

doc.version(2) # => MyVersionedDocument

Removing versions

By default, past versions are never removed. This way it is possible to implement some sort of recovery mechanism of deleted documents.

It should be however trivial to implement removal of versions on destroy. For example:

after_destroy -> doc { doc.previous_versions.destroy_all }

Further Reading

See Further Thoughts on How to Track Versions with MongoDB.

Contributing

  1. Fork it ( https://github.com/tomasc/mongoid_versioning/fork )
  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