mm-versionable 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,8 +14,68 @@ Note on Patches/Pull Requests
14
14
  *Commit, do not make any changes in the rakefile, version, or history. (If you want to have your own version, that is fine but bump the version in a commit by itself so I can ignore it when I pull)
15
15
  *Send me a pull requests.
16
16
 
17
- Development
18
- -----------
17
+ Usage
18
+ -----
19
+ class Thing
20
+ include MongoMapper::Document
21
+
22
+ enable_versioning :limit => 20 #:limit here defines the size of the version history that will be loaded into memory
23
+
24
+ key :name, String, :required => true
25
+ key :date, Time
26
+ end
27
+
28
+ thing = Thing.create(:name => 'Dhruva Sagar', :date => Time.now)
29
+
30
+ thing.name = 'Change Thing'
31
+ thing.save
32
+
33
+ #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
34
+ #example :
35
+ #thing.save :updater_id => "4cef9936f61aa33717000001"
36
+
37
+ thing.versions_count
38
+ #=> 2
39
+
40
+ thing.versions
41
+ #=> [#<Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>nil, "name"=>"Dhruva Sagar", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>, #<Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>nil, "name"=>"Change Thing", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>]
42
+
43
+ thing.all_versions
44
+ #=> #<Plucky::Query doc_id: "4cef96c4f61aa33621000001", sort: [["pos", -1]]>
45
+
46
+ thing.rollback(:first)
47
+ #=> #<Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: "Dhruva Sagar", date: 2010-11-26 11:15:16 UTC>
48
+
49
+ thing.rollback(:last)
50
+ #=> #<Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 0, name: "Dhruva Sagar", date: 2010-11-26 11:15:16 UTC>
51
+
52
+ thing.rollback!(:latest)
53
+ #=> #<Thing _id: BSON::ObjectId('4cef96c4f61aa33621000001'), version_message: nil, version_number: 1, name: "Change Thing", date: 2010-11-26 11:15:16 UTC>
54
+ #rollback! saves the document as well
55
+
56
+ thing.diff(:name, 0, 1)
57
+ #=> "<del class=\"differ\">Change</del><ins class=\"differ\">Dhruva</ins> <del class=\"differ\">Thing</del><ins class=\"differ\">Sagar</ins>"
58
+
59
+ thing.diff(:name, 0, 1, :ascii)
60
+ #=> "{\"Change\" >> \"Dhruva\"} {\"Thing\" >> \"Sagar\"}"
61
+
62
+ thing.current_version
63
+ #=> "\e[31mChange\e[0m\e[32mDhruva\e[0m \e[31mThing\e[0m\e[32mSagar\e[0m"
64
+
65
+ thing.version_at(:first)
66
+ #=> #<Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>nil, "name"=>"Dhruva Sagar", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>
67
+
68
+ thing.version_at(:current)
69
+ #=> #<Version _id: BSON::ObjectId('4cef986df61aa33621000004'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>1, "name"=>"Change Thing", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:22:21 UTC, pos: nil, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>
70
+
71
+ thing.version_at(:last)
72
+ #=> #<Version _id: BSON::ObjectId('4cef96c4f61aa33621000002'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>nil, "name"=>"Dhruva Sagar", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 0, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>
73
+
74
+ thing.version_at(:latest)
75
+ #=> #<Version _id: BSON::ObjectId('4cef96c4f61aa33621000003'), data: {"_id"=>BSON::ObjectId('4cef96c4f61aa33621000001'), "version_message"=>nil, "version_number"=>nil, "name"=>"Change Thing", "date"=>2010-11-26 11:15:16 UTC}, date: 2010-11-26 11:15:16 UTC, pos: 1, doc_id: "4cef96c4f61aa33621000001", message: nil, updater_id: nil>
76
+
77
+ thing.version_at(10)
78
+ #=> nil
19
79
 
20
80
  Problems or Questions?
21
81
  ----------------------
data/lib/versionable.rb CHANGED
@@ -1,2 +1,11 @@
1
+ require 'differ'
1
2
  require 'mongo_mapper'
2
3
  require 'versionable/plugins/versionable'
4
+
5
+ module VersionablePlugin
6
+ def self.included(model)
7
+ model.plugin Versionable
8
+ end
9
+ end
10
+
11
+ MongoMapper::Document.append_inclusions(VersionablePlugin)
@@ -1,13 +1,9 @@
1
1
  require 'versionable/models/version'
2
2
 
3
3
  module Versionable
4
- def self.configure(model)
5
- model.enable_versioning
6
- end
7
-
8
4
  module InstanceMethods
9
5
  def save(options={})
10
- save_version(options[:updater_id]) if self.respond_to?(:rolling_back) && !rolling_back
6
+ save_version(options.delete(:updater_id)) if self.respond_to?(:rolling_back) && !rolling_back
11
7
  super
12
8
  end
13
9
 
@@ -24,7 +20,7 @@ module Versionable
24
20
  version.updater_id = updater_id
25
21
  version.save
26
22
 
27
- self.versions.shift
23
+ self.versions.shift if self.versions.count >= @limit
28
24
  self.versions << version
29
25
 
30
26
  @versions_count = @versions_count.to_i + 1
@@ -44,7 +40,8 @@ module Versionable
44
40
  end
45
41
 
46
42
  define_method(:versions) do
47
- @versions ||= Version.all(:doc_id => self._id.to_s, :order => 'pos desc', :limit => (opts[:limit] || 10)).reverse
43
+ @limit ||= opts[:limit] || 10
44
+ @versions ||= Version.all(:doc_id => self._id.to_s, :order => 'pos desc', :limit => @limit).reverse
48
45
  end
49
46
 
50
47
  define_method(:all_versions) do
@@ -1,3 +1,3 @@
1
1
  module Versionable
2
- Version = '0.2'
2
+ Version = '0.2.1'
3
3
  end
data/test/models/user.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  class User
2
2
  include MongoMapper::Document
3
- plugin Versionable
3
+
4
+ enable_versioning :limit => 20
4
5
 
5
6
  key :fname, String
6
7
  key :lname, String
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- version: "0.2"
8
+ - 1
9
+ version: 0.2.1
9
10
  platform: ruby
10
11
  authors:
11
12
  - Dhruva Sagar