Skip to content

Commit

Permalink
Fixed & Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvasagar committed Jun 17, 2011
1 parent bdbe4e7 commit 5e9d450
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,74 @@ Usage
-----
The following example should demonstrate how to use versioning well :

<pre>
class Thing
include MongoMapper::Document
require 'versionable'
# gem 'mm-versionable', :require => 'versionable' -- Put this in your Gemfile if you're using bundler

enable_versioning :limit => 20
#:limit here defines the size of the version history that will be loaded into memory,
#By default, if not specified, the value is 10, if you wish to load all versions set it to 0
class Thing
include MongoMapper::Document

key :name, String, :required => true
key :date, Time
end
enable_versioning :limit => 20
#:limit here defines the size of the version history that will be loaded into memory,
#By default, if not specified, the value is 10, if you wish to load all versions set it to 0

thing = Thing.create(:name => 'Dhruva Sagar', :date => Time.now)
key :name, String, :required => true
key :date, Time
end

thing.name = 'Change Thing'
thing.save
thing = Thing.create(:name => 'Dhruva Sagar', :date => Time.now)

#Alternatively you can also pass in a "updater_id" to the save method which will be saved within the version, this can be used to track who made changes
#example :
#thing.save :updater_id => "4cef9936f61aa33717000001"
thing.name = 'Change Thing'
thing.save

thing.versions_count
#=> 2
#Alternatively you can also pass in a "updater_id" to the save method which will be saved within the version, this can be used to track who made changes
#example :
#thing.save :updater_id => "4cef9936f61aa33717000001"

thing.versions
#=&gt; [#&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;, #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;]
thing.versions_count
#=> 2

thing.all_versions
#=&gt; #&lt;Plucky::Query doc_id: &quot;4cef96c4f61aa33621000001&quot;, sort: [[&quot;pos&quot;, -1]]&gt;
thing.versions
#=&gt; [#&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;, #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;]

thing.rollback(:first)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: &quot;Dhruva Sagar&quot;, date: 2010-11-26 11:15:16 UTC&gt;
thing.all_versions
#=&gt; #&lt;Plucky::Query doc_id: &quot;4cef96c4f61aa33621000001&quot;, sort: [[&quot;pos&quot;, -1]]&gt;

thing.rollback(:last)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: &quot;Dhruva Sagar&quot;, date: 2010-11-26 11:15:16 UTC&gt;
thing.rollback(:first)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: &quot;Dhruva Sagar&quot;, date: 2010-11-26 11:15:16 UTC&gt;

thing.rollback!(:latest)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 1, name: &quot;Change Thing&quot;, date: 2010-11-26 11:15:16 UTC&gt;
#rollback! saves the document as well
thing.rollback(:last)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: &quot;Dhruva Sagar&quot;, date: 2010-11-26 11:15:16 UTC&gt;

thing.diff(:name, 0, 1)
#=&gt; &quot;&lt;del class=\&quot;differ\&quot;&gt;Change&lt;/del&gt;&lt;ins class=\&quot;differ\&quot;&gt;Dhruva&lt;/ins&gt; &lt;del class=\&quot;differ\&quot;&gt;Thing&lt;/del&gt;&lt;ins class=\&quot;differ\&quot;&gt;Sagar&lt;/ins&gt;&quot;
thing.rollback!(:latest)
#=&gt; #&lt;Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 1, name: &quot;Change Thing&quot;, date: 2010-11-26 11:15:16 UTC&gt;
#rollback! saves the document as well

thing.diff(:name, 0, 1, :ascii)
#=&gt; &quot;{\&quot;Change\&quot; &gt;&gt; \&quot;Dhruva\&quot;} {\&quot;Thing\&quot; &gt;&gt; \&quot;Sagar\&quot;}&quot;
thing.diff(:name, 0, 1)
#=&gt; &quot;&lt;del class=\&quot;differ\&quot;&gt;Change&lt;/del&gt;&lt;ins class=\&quot;differ\&quot;&gt;Dhruva&lt;/ins&gt; &lt;del class=\&quot;differ\&quot;&gt;Thing&lt;/del&gt;&lt;ins class=\&quot;differ\&quot;&gt;Sagar&lt;/ins&gt;&quot;

thing.diff(:name, 0, 1, :color)
#=&gt; &quot;\e[31mChange\e[0m\e[32mDhruva\e[0m \e[31mThing\e[0m\e[32mSagar\e[0m&quot;
thing.diff(:name, 0, 1, :ascii)
#=&gt; &quot;{\&quot;Change\&quot; &gt;&gt; \&quot;Dhruva\&quot;} {\&quot;Thing\&quot; &gt;&gt; \&quot;Sagar\&quot;}&quot;

thing.current_version
#=&gt; #&lt;Version _id: BSON::ObjectId('4cf03822f61aa30fd8000004'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cf03816f61aa30fd8000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 22:43:34 UTC}, date: 2010-11-26 22:43:46 UTC, pos: nil, doc_id: &quot;4cf03816f61aa30fd8000001&quot;, message: nil, updater_id: nil&gt;
thing.diff(:name, 0, 1, :color)
#=&gt; &quot;\e[31mChange\e[0m\e[32mDhruva\e[0m \e[31mThing\e[0m\e[32mSagar\e[0m&quot;

thing.version_at(:first)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;
thing.current_version
#=&gt; #&lt;Version _id: BSON::ObjectId('4cf03822f61aa30fd8000004'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cf03816f61aa30fd8000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 22:43:34 UTC}, date: 2010-11-26 22:43:46 UTC, pos: nil, doc_id: &quot;4cf03816f61aa30fd8000001&quot;, message: nil, updater_id: nil&gt;

thing.version_at(:current)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef986df61aa33621000004'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;1, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:22:21 UTC, pos: nil, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;
thing.version_at(:first)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;

thing.version_at(:last)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;
thing.version_at(:current)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef986df61aa33621000004'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;1, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:22:21 UTC, pos: nil, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;

thing.version_at(:latest)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;
thing.version_at(:last)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Dhruva Sagar&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;

thing.version_at(10)
#=> nil
</pre>
thing.version_at(:latest)
#=&gt; #&lt;Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {&quot;_id&quot;=&gt;BSON::ObjectId('4cef96c4f61aa33621000001'), &quot;version_message&quot;=&gt;nil, &quot;version_number&quot;=&gt;nil, &quot;name&quot;=&gt;&quot;Change Thing&quot;, &quot;date&quot;=&gt;2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: &quot;4cef96c4f61aa33621000001&quot;, message: nil, updater_id: nil&gt;

thing.version_at(10)
#=> nil

Problems or Questions?
----------------------
Expand Down

0 comments on commit 5e9d450

Please sign in to comment.