mongoid_revisions 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -43,7 +43,15 @@ Simply call the revise method
43
43
  last_comment_revision=@comment.revise
44
44
  ```
45
45
 
46
- to create a new revision for a document. Please note that the original object will be unchanged!
46
+ to create a new revision for a document. Please note that the original object will be unchanged!
47
+
48
+ To create a new branch of a given document, use
49
+
50
+ ```ruby
51
+ new_comment=@comment.branch
52
+ ```
53
+
54
+ and the returned document will have a different token and will be at revision 0.
47
55
 
48
56
  To access all revisitons for a givent document, use
49
57
 
@@ -61,8 +69,8 @@ to change the tag attribute of the document and save it in a single call.
61
69
 
62
70
  ## Future changes (sort of TODO list)
63
71
 
72
+ - Check if linked relations cloning work for all relation's tipologies
64
73
  - Recursive revision to linked documents
65
- - Branching to create a new document at revision a 0 and with a token different from the original
66
74
 
67
75
  ## Development
68
76
 
@@ -1,3 +1,3 @@
1
1
  module MongoidRevisions
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -33,24 +33,34 @@ module Mongoid
33
33
 
34
34
  # CREATE A NEW REVISION FOR THE DOCUMENT
35
35
  def revise
36
- new_revision = self.class.create self.attributes.except("_id")
37
- new_revision._token = self.token
38
- new_revision._revision = (self.revision || 1) + 1
39
- new_revision.tag = "#{new_revision.revision}.0.0"
40
- new_revision.save
41
- self.relations.each do |relation|
36
+ self._revise_or_branch((self.revision || 1) + 1,self.token)
37
+ end
38
+
39
+ # CREATE A NEW BRANCH
40
+ def branch
41
+ self._revise_or_branch(0)
42
+ end
43
+
44
+ protected
45
+
46
+ def _revise_or_branch(revision,token=nil)
47
+ new = self.class.create self.attributes.except("_id")
48
+ new._token = token
49
+ new._revision = revision
50
+ new.tag = "#{new.revision}.0.0"
51
+ new.save
52
+ self.relations.each do |relation|
42
53
  metadata = self.class.reflect_on_association(relation[0])
43
54
  metadata.class_name.constantize.where(metadata.foreign_key.to_sym=>self.id).each do |child|
44
55
  new_child = metadata.class_name.constantize.create child.attributes.except("_id")
45
56
  new_child.revision = child.revision+1
46
57
  new_child.tag = "#{new_child.revision}.0.0"
47
- new_child[metadata.foreign_key.to_sym]=new_revision.id
58
+ new_child[metadata.foreign_key.to_sym]=new.id
48
59
  new_child.save
49
60
  end
50
61
  end
51
- end
52
-
53
- protected
62
+ new
63
+ end
54
64
 
55
65
  def _revision=(rev)
56
66
  self[:revision]=rev
@@ -65,7 +65,7 @@ describe Mongoid::Revisions do
65
65
 
66
66
  describe "when I want a new revision" do
67
67
  before :all do
68
- @project.revise
68
+ @new_revised_project = @project.revise
69
69
  end
70
70
 
71
71
  it "has 2 revisions" do
@@ -80,6 +80,10 @@ describe Mongoid::Revisions do
80
80
  @project.revisions.last.revision.should==1
81
81
  end
82
82
 
83
+ it "returned object should be last revision" do
84
+ @project.revisions.last.should==@new_revised_project
85
+ end
86
+
83
87
  it "last revision has the default tag" do
84
88
  @project.revisions.last.tag.should=="1.0.0"
85
89
  end
@@ -93,7 +97,6 @@ describe Mongoid::Revisions do
93
97
  end
94
98
 
95
99
  it "last revision has a single milestone" do
96
- #puts @project.revisions.last.inspect
97
100
  @project.revisions.last.milestones.count.should==1
98
101
  end
99
102
  end
@@ -107,4 +110,26 @@ describe Mongoid::Revisions do
107
110
  @project.revisions.count.should==(count+2)
108
111
  end
109
112
  end
113
+
114
+ describe "when I want a new branch" do
115
+ before :all do
116
+ @new_project = @project.branch
117
+ end
118
+
119
+ it "should be at revision 0" do
120
+ @new_project.revision.should==0
121
+ end
122
+
123
+ it "should change token" do
124
+ @new_project.token.should_not==@project.token
125
+ end
126
+
127
+ it "should have 1 milestone" do
128
+ @new_project.milestones.count.should==1
129
+ end
130
+
131
+ it "should have 1 revision" do
132
+ @new_project.revisions.count.should==1
133
+ end
134
+ end
110
135
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_revisions
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Emiliano Della Casa