mongoid_revisions 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 1.9.2
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Mongoid Revisions
1
+ # Mongoid Revisions [![Build Status](https://secure.travis-ci.org/emilianodellacasa/mongoid_revisions.png)](https://secure.travis-ci.org/emilianodellacasa/mongoid_revisions.png)
2
2
 
3
3
  Add support for revisions to your Mongoid documents
4
4
 
@@ -23,42 +23,61 @@ To add support for revisions to a Mongoid documents, include the module to your
23
23
  end
24
24
  ```
25
25
 
26
- This gem adds to your Mongoid document the following attributes:
26
+ ### Added Attributes
27
27
 
28
28
  - revision (read only)
29
29
  - tag
30
30
  - token (read only)
31
31
 
32
- and the following methods:
32
+ ### Added Methods
33
33
 
34
34
  - revisions
35
35
  - tag_version
36
36
  - revise
37
+ - branch
37
38
 
38
- Token is a randomly generated string that is common to all revisions of the same document and it is wirte protected to preserve the functionality of this library.
39
+ ### Token Field
40
+
41
+ Token is a randomly generated string that is common to all revisions of the same document and it is write protected to preserve the functionality of this library.
42
+
43
+ An index is automatically added to this field but to apply it you will have to issue the following command
44
+
45
+ ```ruby
46
+ rake db:mongoid:create_indexes
47
+ ```
48
+
49
+ like all mongoid indexes.
50
+
51
+ ### Create a new revision
39
52
 
40
53
  Simply call the revise method
41
54
 
42
55
  ```ruby
43
- last_comment_revision=@comment.revise
56
+ last_comment_revision = @comment.revise
44
57
  ```
45
58
 
46
- to create a new revision for a document. Please note that the original object will be unchanged!
59
+ Please note that the original object will be unchanged!
60
+
61
+ ### Create a new branch
47
62
 
48
63
  To create a new branch of a given document, use
49
64
 
50
65
  ```ruby
51
- new_comment=@comment.branch
66
+ new_comment = @comment.branch
52
67
  ```
53
68
 
54
69
  and the returned document will have a different token and will be at revision 0.
55
70
 
56
- To access all revisitons for a givent document, use
71
+ ### Get all document's revisions
72
+
73
+ To access all revisions for a given document, use
57
74
 
58
75
  ```ruby
59
76
  @comment.revisions
60
77
  ```
61
78
 
79
+ ### Tag a revision
80
+
62
81
  Lastly, to modify a revision with a given tag, use
63
82
 
64
83
  ```ruby
@@ -67,10 +86,49 @@ Lastly, to modify a revision with a given tag, use
67
86
 
68
87
  to change the tag attribute of the document and save it in a single call.
69
88
 
89
+ ### Access a document by a particular revision or tag
90
+
91
+ You can access a document by its revision or tag by using the following functions:
92
+
93
+ ```ruby
94
+ @comment.at_revision(2)
95
+ ```
96
+
97
+ ```ruby
98
+ @comment.tagged("Beta stage")
99
+ ```
100
+
101
+ ### Navigate thru revisions
102
+
103
+ You can navigate thru all revisions of a document by using the 'next' and 'previous' functions:
104
+
105
+ ```ruby
106
+ @comment.next
107
+ ```
108
+
109
+ ```ruby
110
+ @comment.previous
111
+ ```
112
+
113
+ ## Relations Gotcha
114
+
115
+ At present time, not all relation types supported by Mongoid get cloned by this gem.
116
+
117
+ Here follow a list of all relation types with the indication in they are supported or not:
118
+
119
+ - has_one : SUPPORTED
120
+ - has_many : SUPPORTED
121
+ - belongs_to : UNSUPPORTED (Doesn't make any sense IMHO, but it will be possible to support it)
122
+ - has_and_belongs_to_many : SUPPORTED (only the relation between the two models gets cloned)
123
+ - embeds_many : UNSUPPORTED
124
+ - embeds_one : UNSUPPORTED
125
+
70
126
  ## Future changes (sort of TODO list)
71
127
 
72
- - Check if linked relations cloning work for all relation's tipologies
73
128
  - Recursive revision to linked documents
129
+ - Add search method for tag
130
+ - Add support for cloning of "embeds_many" and "embeds_one" relations
131
+ - Add relation blacklist for all relations you don't want to clone
74
132
 
75
133
  ## Development
76
134
 
@@ -1,3 +1,3 @@
1
1
  module MongoidRevisions
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -9,6 +9,8 @@ module Mongoid
9
9
  field :tag, :type => String, :default => "0.0.0"
10
10
  field :token, :type => String
11
11
 
12
+ index :token
13
+
12
14
  set_callback :create, :before, :create_unique_token
13
15
  end
14
16
 
@@ -17,7 +19,9 @@ module Mongoid
17
19
  # DOES NOTHING
18
20
  end
19
21
 
22
+ # BLOCK ACCESS TO TOKEN ATTRIBUTE
20
23
  def token=(tok)
24
+ # DOES NOTHING
21
25
  end
22
26
 
23
27
  # RETURN ALL REVISIONS FOR THIS DOCUMENT
@@ -25,6 +29,26 @@ module Mongoid
25
29
  self.class.where(:token=>self.token).order_by([[:revision,:asc]])
26
30
  end
27
31
 
32
+ # RETURN A SPECIFIC REVISION
33
+ def at_revision(revision)
34
+ self.class.where(:token=>self.token,:revision=>revision.to_i).first
35
+ end
36
+
37
+ # RETURN A SPECIFIC REVISION WITH A SPECIFIC TAG
38
+ def tagged(tag)
39
+ self.class.where(:token=>self.token,:tag=>tag).first
40
+ end
41
+
42
+ # RETURN NEXT REVISION
43
+ def next
44
+ at_revision(self.revision+1)
45
+ end
46
+
47
+ # RETURN PREVIOUS REVISION
48
+ def previous
49
+ at_revision(self.revision-1)
50
+ end
51
+
28
52
  # ASSIGN A NEW TAG TO THIS REVISION
29
53
  def tag_revision(tag)
30
54
  self.tag=tag
@@ -50,14 +74,18 @@ module Mongoid
50
74
  new.tag = "#{new.revision}.0.0"
51
75
  new.save
52
76
  self.relations.each do |relation|
53
- metadata = self.class.reflect_on_association(relation[0])
54
- metadata.class_name.constantize.where(metadata.foreign_key.to_sym=>self.id).each do |child|
55
- new_child = metadata.class_name.constantize.create child.attributes.except("_id")
56
- new_child.revision = child.revision+1
57
- new_child.tag = "#{new_child.revision}.0.0"
58
- new_child[metadata.foreign_key.to_sym]=new.id
59
- new_child.save
60
- end
77
+ metadata=relation[1]
78
+ if metadata[:relation]==Mongoid::Relations::Embedded::Many or metadata[:relation]==Mongoid::Relations::Embedded::One
79
+ # TODO
80
+ elsif metadata[:relation]!=Mongoid::Relations::Referenced::In
81
+ metadata.class_name.constantize.where(metadata.foreign_key.to_sym=>self.id).each do |child|
82
+ new_child = metadata.class_name.constantize.create child.attributes.except("_id")
83
+ new_child.revision = child.revision+1
84
+ new_child.tag = "#{new_child.revision}.0.0"
85
+ new_child[metadata.foreign_key.to_sym]=new.id
86
+ new_child.save
87
+ end
88
+ end
61
89
  end
62
90
  new
63
91
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_development_dependency "rake"
21
22
  s.add_development_dependency 'rspec', '~> 2.8.0'
22
23
  s.add_development_dependency 'mongoid', '~> 2.4.2'
23
24
  s.add_development_dependency 'bson_ext', '~> 1.5.2'
@@ -0,0 +1,9 @@
1
+ class Configuration
2
+ include Mongoid::Document
3
+ include Mongoid::Revisions
4
+
5
+ field :directory_name
6
+
7
+ belongs_to :project
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class Note
2
+ include Mongoid::Document
3
+ include Mongoid::Revisions
4
+
5
+ field :text
6
+
7
+ embedded_in :project
8
+
9
+ end
@@ -5,5 +5,9 @@ class Project
5
5
  field :name
6
6
 
7
7
  has_many :milestones
8
-
8
+ belongs_to :user
9
+ has_one :configuration
10
+ embeds_many :notes
11
+ embeds_one :version
12
+ has_and_belongs_to_many :teams
9
13
  end
@@ -0,0 +1,9 @@
1
+ class Team
2
+ include Mongoid::Document
3
+ include Mongoid::Revisions
4
+
5
+ field :name
6
+
7
+ has_and_belongs_to_many :projects
8
+
9
+ end
@@ -0,0 +1,8 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :username
5
+
6
+ has_many :projects
7
+
8
+ end
@@ -0,0 +1,9 @@
1
+ class Version
2
+ include Mongoid::Document
3
+ include Mongoid::Revisions
4
+
5
+ field :description
6
+
7
+ embedded_in :project
8
+
9
+ end
@@ -3,8 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Mongoid::Revisions do
4
4
  before :all do
5
5
  Project.delete_all
6
- @project = Project.create!(:name=>"My first project")
6
+ @user = User.create!(:username=>"teddy")
7
+ @project = Project.create!(:name=>"My first project",:user_id=>@user.id)
7
8
  @project.milestones.create!(:description=>"First Milestone")
9
+ @project.create_configuration(:directory_name=>"/opt/project")
10
+ @project.notes.create!(:text=>"This is a note")
11
+ @project.create_version(:description=>"1.0.0")
12
+ @project.teams.create(:name=>"Team A")
8
13
  end
9
14
 
10
15
  context "just created" do
@@ -33,6 +38,18 @@ describe Mongoid::Revisions do
33
38
  @project.milestones.count==1
34
39
  end
35
40
 
41
+ it "has a single note" do
42
+ @project.notes.count==1
43
+ end
44
+
45
+ it "has a single team" do
46
+ @project.teams.count==1
47
+ end
48
+
49
+ it "has a single configuration" do
50
+ @project.configuration.should_not be_nil
51
+ end
52
+
36
53
  it "does not allow modification to its revision" do
37
54
  @project.revision=56
38
55
  @project.revision.should==0
@@ -42,6 +59,10 @@ describe Mongoid::Revisions do
42
59
  @project.token="ciccio"
43
60
  @project.token.should_not=="ciccio"
44
61
  end
62
+
63
+ it "should have a index on token" do
64
+ Project.index_options.count.should==1
65
+ end
45
66
  end
46
67
 
47
68
 
@@ -61,6 +82,14 @@ describe Mongoid::Revisions do
61
82
  it "has a single milestone" do
62
83
  @project.milestones.count==1
63
84
  end
85
+
86
+ it "has a single note" do
87
+ @project.notes.count==1
88
+ end
89
+
90
+ it "has a single version" do
91
+ @project.version.should_not be_nil
92
+ end
64
93
  end
65
94
 
66
95
  describe "when I want a new revision" do
@@ -99,6 +128,64 @@ describe Mongoid::Revisions do
99
128
  it "last revision has a single milestone" do
100
129
  @project.revisions.last.milestones.count.should==1
101
130
  end
131
+
132
+ it "last revision has a single team" do
133
+ @project.revisions.last.teams.count.should==1
134
+ end
135
+
136
+ it "original revision has a single team" do
137
+ @project.teams.count.should==1
138
+ end
139
+
140
+ it "original project has a single note" do
141
+ @project.notes.count.should==1
142
+ end
143
+
144
+ it "last revision has a single note" do
145
+ pending # EMBEDDED RELATIONS NOT SUPPORTED (YET)
146
+ @project.revisions.last.notes.count.should==1
147
+ end
148
+
149
+ it "last revision has a single version" do
150
+ pending # EMBEDDED RELATIONS NOT SUPPORTED (YET)
151
+ @project.revisions.last.version.should_not be_nil
152
+ end
153
+
154
+ it "does not create a new User" do
155
+ User.count.should==1
156
+ end
157
+
158
+ it "should create a new configuration" do
159
+ Configuration.count.should==2
160
+ end
161
+
162
+ it "last revision has a single configuration" do
163
+ @project.revisions.last.configuration.should_not be_nil
164
+ end
165
+
166
+ it "should allow me to access a particular revision" do
167
+ @project.at_revision(1).revision.should==1
168
+ end
169
+
170
+ it "should allow me to access a particular revision using its tag" do
171
+ @project.tagged("1.0.0").tag.should=="1.0.0"
172
+ end
173
+
174
+ it "should allow me to navigate to the next revision" do
175
+ @project.next.revision.should==@project.revision+1
176
+ end
177
+
178
+ it "should allow me to navigate to the previous revision" do
179
+ @new_revised_project.previous.revision.should==@project.revision
180
+ end
181
+
182
+ it "should me give me nil if there is no next revision" do
183
+ @new_revised_project.next.should==nil
184
+ end
185
+
186
+ it "should me give me nil if there is no previous revision" do
187
+ @project.previous.should==nil
188
+ end
102
189
  end
103
190
 
104
191
  describe "adding a random number of revisions" do
data/spec/spec_helper.rb CHANGED
@@ -17,5 +17,5 @@ Dir[ File.join(models_folder, '*.rb') ].each { |file|
17
17
  require file
18
18
  file_name = File.basename(file).sub('.rb', '')
19
19
  klass = file_name.classify.constantize
20
- klass.collection.drop
20
+ klass.collection.drop unless klass.embedded?
21
21
  }
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.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Emiliano Della Casa
@@ -10,41 +10,52 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-02-13 00:00:00 Z
13
+ date: 2012-02-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rspec
16
+ name: rake
17
17
  prerelease: false
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
19
30
  none: false
20
31
  requirements:
21
32
  - - ~>
22
33
  - !ruby/object:Gem::Version
23
34
  version: 2.8.0
24
35
  type: :development
25
- version_requirements: *id001
36
+ version_requirements: *id002
26
37
  - !ruby/object:Gem::Dependency
27
38
  name: mongoid
28
39
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
30
41
  none: false
31
42
  requirements:
32
43
  - - ~>
33
44
  - !ruby/object:Gem::Version
34
45
  version: 2.4.2
35
46
  type: :development
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: bson_ext
39
50
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirement: &id004 !ruby/object:Gem::Requirement
41
52
  none: false
42
53
  requirements:
43
54
  - - ~>
44
55
  - !ruby/object:Gem::Version
45
56
  version: 1.5.2
46
57
  type: :development
47
- version_requirements: *id003
58
+ version_requirements: *id004
48
59
  description: Add support for revisions to your Mongoid documents by creating a new version of a document every time you change it and replicating all of its associations
49
60
  email:
50
61
  - e.dellacasa@engim.eu
@@ -56,6 +67,7 @@ extra_rdoc_files: []
56
67
 
57
68
  files:
58
69
  - .gitignore
70
+ - .travis.yml
59
71
  - Gemfile
60
72
  - README.md
61
73
  - Rakefile
@@ -63,8 +75,13 @@ files:
63
75
  - lib/mongoid_revisions/version.rb
64
76
  - mongoid_revisions.gemspec
65
77
  - spec/.rspec
78
+ - spec/mongoid/models/configuration.rb
66
79
  - spec/mongoid/models/milestone.rb
80
+ - spec/mongoid/models/note.rb
67
81
  - spec/mongoid/models/project.rb
82
+ - spec/mongoid/models/team.rb
83
+ - spec/mongoid/models/user.rb
84
+ - spec/mongoid/models/version.rb
68
85
  - spec/mongoid_revisions/revisions_spec.rb
69
86
  - spec/spec_helper.rb
70
87
  homepage: ""