Vidibus::Versioning
Vidibus::Versioning provides advanced versioning for Mongoid models including support for future and editable versions.
This gem is part of Vidibus, an open source toolset for building distributed (video) applications.
Installation
Add gem 'vidibus-versioning'
to your Gemfile. Then call bundle install
on your console.
Usage
Applying versioning to your model is easy. An example:
class Article
include Mongoid::Document
include Vidibus::Uuid::Mongoid
include Vidibus::Versioning::Mongoid
field :title, type: String
field :text, type: String
versioned :title, :text, editing_time: 300
end
Versioned attributes
Including the versioning engine by adding include Vidibus::Versioning::Mongoid
will set all fields of your model as
versioned ones, except those contained in <Class>.unversioned_attributes
, which are _id
, _type
, uuid
,
updated_at
, created_at
, and version_number
.
An optional versioned
call lets you specify the versioned attributes precisely by providing a list. For example, to
set the title as only attribute to be versioned, call versioned :title
.
Combined versioning
versioned
also takes options to tweak versioning behaviour. By calling versioned editing_time: 300
you set a
timespan for the version to accept changes so all changes within 300 seconds will be treated as one version.
That behaviour is especially useful if your model's UI allows changing attributes separately, like in-place editing.
Migrating
The basic methods for migrating a versioned object – an article in this case – are:
article.migrate!(32)
article.undo!
article.redo!
Version editing
There is also a method version
that loads an exisiting version of the record or instantiates a new one:
article.version(3)
article.version(:previous)
article.version(:next)
article.version(:new)
To apply a version on the current article itself (without persisting it), call version
with a bang!:
article.version!(3)
To find out if a version exists, call:
article.version?(1)
article.version?(2)
article.version?(3)
It is even possible to apply versioned attributes directly by adding them to the version
call:
article.version(3, title: 'Wicked!')
You may treat the article object with an applied version like the article itself. All changes will
be applied to the loaded version instead of the current instance. This is useful for creating future versions
that can be scheduled by Vidibus::VersionScheduler.
A workflow example:
article = Article.create(title: 'Old stuff')
future_article = article.version(:new)
future_article.updated_at = 1.day.since
future_article.title = 'New stuff'
future_article.save
Version objects
All versions of your models are stored in a separate model: Vidibus::Versioning::Version
. To access the
version object of an article's version, call article.version_object
:
article.version(3).version_object
article.version_object
TODO
- Handle embedded documents
- Handle related documents
Copyright
Copyright (c) 2011-2015 Andre Pankratz. See LICENSE for details.