mongoid_revisions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,12 +25,47 @@ To add support for revisions to a Mongoid documents, include the module to your
25
25
 
26
26
  This gem adds to your Mongoid document the following attributes:
27
27
 
28
- - revision
28
+ - revision (read only)
29
29
  - tag
30
- - token
30
+ - token (read only)
31
+
32
+ and the following methods:
33
+
34
+ - revisions
35
+ - tag_version
36
+ - revise
37
+
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
+
40
+ Simply call the revise method
41
+
42
+ ```ruby
43
+ last_comment_revision=@comment.revise
44
+ ```
45
+
46
+ to create a new revision for a document. Please note that the original object will be unchanged!
47
+
48
+ To access all revisitons for a givent document, use
49
+
50
+ ```ruby
51
+ @comment.revisions
52
+ ```
53
+
54
+ Lastly, to modify a revision with a given tag, use
55
+
56
+ ```ruby
57
+ @comment.revise.tag_version("Beta stage")
58
+ ```
59
+
60
+ to change the tag attribute of the document and save it in a single call.
61
+
62
+ ## Future changes (sort of TODO list)
63
+
64
+ - Recursive revision to linked documents
65
+ - Branching to create a new document at revision a 0 and with a token different from the original
31
66
 
32
67
  ## Development
33
68
 
34
69
  Questions or problems? Please post them on the [issue tracker](https://github.com/emilianodellacasa/mongoid_revisions/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
35
70
 
36
- This gem is created by Emiliano Della Casa and is under the MIT License.
71
+ This gem is created by Emiliano Della Casa and is under the MIT License and it is distributed by courtesy of [Engim srl](http://www.engim.eu/en).
@@ -1,3 +1,3 @@
1
1
  module MongoidRevisions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,40 +1,68 @@
1
1
  require "mongoid_revisions/version"
2
2
 
3
3
  module Mongoid
4
- module Revisions
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- field :revision, :type => Integer, :default => 1
9
- field :tag, :type => String, :default => "1.0.0"
10
- field :token, :type => String
11
-
12
-
13
- set_callback :create, :before, :create_unique_token
14
- end
15
-
16
- # CREATE A UNIQUE ID FOR THE DOCUMENT BUT ONLY IF TOKEN IS EMPTY
17
- def create_unique_token
18
- self.token=SecureRandom.hex(16) if self.token.nil?
19
- end
20
-
21
- # RETURN ALL REVISIONS FOR THIS DOCUMENT
22
- def revisions
23
- self.class.where(:token=>self.token) #.order_by([[:revision,:asc]])
24
- end
25
-
26
- # ASSIGN A NEW TAG TO THIS REVISION
27
- def tag_revision(tag)
28
- self.tag=tag
29
- self.save
30
- end
31
-
32
- # CREATE A NEW REVISION FOR THE DOCUMENT
33
- def revise
34
- new_revision = self.class.create self.attributes.except("_id")
35
- new_revision.revision = (self.revision || 1) + 1
36
- new_revision.tag = "#{new_revision.revision}.0.0"
37
- new_revision.save
38
- end
4
+ module Revisions
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ field :revision, :type => Integer, :default => 0
9
+ field :tag, :type => String, :default => "0.0.0"
10
+ field :token, :type => String
11
+
12
+ set_callback :create, :before, :create_unique_token
13
+ end
14
+
15
+ # BLOCK ACCESS TO REVISION ATTRIBUTE
16
+ def revision=(rev)
17
+ # DOES NOTHING
18
+ end
19
+
20
+ def token=(tok)
21
+ end
22
+
23
+ # RETURN ALL REVISIONS FOR THIS DOCUMENT
24
+ def revisions
25
+ self.class.where(:token=>self.token).order_by([[:revision,:asc]])
26
+ end
27
+
28
+ # ASSIGN A NEW TAG TO THIS REVISION
29
+ def tag_revision(tag)
30
+ self.tag=tag
31
+ self.save
32
+ end
33
+
34
+ # CREATE A NEW REVISION FOR THE DOCUMENT
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|
42
+ metadata = self.class.reflect_on_association(relation[0])
43
+ metadata.class_name.constantize.where(metadata.foreign_key.to_sym=>self.id).each do |child|
44
+ new_child = metadata.class_name.constantize.create child.attributes.except("_id")
45
+ new_child.revision = child.revision+1
46
+ new_child.tag = "#{new_child.revision}.0.0"
47
+ new_child[metadata.foreign_key.to_sym]=new_revision.id
48
+ new_child.save
49
+ end
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def _revision=(rev)
56
+ self[:revision]=rev
57
+ end
58
+
59
+ def _token=(tok)
60
+ self[:token]=tok
61
+ end
62
+
63
+ # CREATE A UNIQUE ID FOR THE DOCUMENT BUT ONLY IF TOKEN IS EMPTY
64
+ def create_unique_token
65
+ self._token=SecureRandom.hex(16) if self.token.nil?
66
+ end
39
67
  end
40
68
  end
@@ -0,0 +1,9 @@
1
+ class Milestone
2
+ include Mongoid::Document
3
+ include Mongoid::Revisions
4
+
5
+ field :description
6
+
7
+ belongs_to :project
8
+
9
+ end
@@ -4,4 +4,6 @@ class Project
4
4
 
5
5
  field :name
6
6
 
7
+ has_many :milestones
8
+
7
9
  end
@@ -1,83 +1,110 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Mongoid::Revisions do
4
- before :all do
5
- Project.delete_all
6
- @project = Project.create!(:name=>"My first project")
7
- end
8
-
9
- context "just created" do
10
- it "is at revision 1" do
11
- @project.revision.should==1
12
- end
13
-
14
- it "has a default tag" do
15
- @project.tag.should=="1.0.0"
16
- end
17
-
18
- it "has a unique token" do
19
- @project.token.should_not be_nil
20
- end
21
-
22
- it "has just 1 revision" do
23
- @project.revisions.count.should==1
24
- end
25
-
26
- it "can have a different tag" do
27
- @project.tag_revision("Alpha Stage")
28
- @project.tag.should=="Alpha Stage"
29
- end
30
- end
4
+ before :all do
5
+ Project.delete_all
6
+ @project = Project.create!(:name=>"My first project")
7
+ @project.milestones.create!(:description=>"First Milestone")
8
+ end
9
+
10
+ context "just created" do
11
+ it "is at revision 0" do
12
+ @project.revision.should==0
13
+ end
14
+
15
+ it "has a default tag" do
16
+ @project.tag.should=="0.0.0"
17
+ end
18
+
19
+ it "has a unique token" do
20
+ @project.token.should_not be_nil
21
+ end
22
+
23
+ it "has just 1 revision" do
24
+ @project.revisions.count.should==1
25
+ end
26
+
27
+ it "can have a different tag" do
28
+ @project.tag_revision("Alpha Stage")
29
+ @project.tag.should=="Alpha Stage"
30
+ end
31
+
32
+ it "has a single milestone" do
33
+ @project.milestones.count==1
34
+ end
35
+
36
+ it "does not allow modification to its revision" do
37
+ @project.revision=56
38
+ @project.revision.should==0
39
+ end
40
+
41
+ it "does not allow modifications to its token" do
42
+ @project.token="ciccio"
43
+ @project.token.should_not=="ciccio"
44
+ end
45
+ end
31
46
 
32
47
 
33
- describe "when I do not want a new revision" do
34
- before :all do
35
- @project.save
36
- end
37
-
38
- it "has 1 revisions" do
39
- @project.revisions.count.should==1
40
- end
41
-
42
- it "it is at revision 1" do
43
- @project.revision.should==1
44
- end
45
- end
46
-
47
- describe "when I want a new revision" do
48
- before :all do
49
- @project.revise
50
- end
51
-
52
- it "has 2 revisions" do
53
- @project.revisions.count.should==2
54
- end
55
-
56
- it "it is at revision 1" do
57
- @project.revision.should==1
58
- end
59
-
60
- it "last revision is at revision 2" do
61
- @project.revisions.last.revision.should==2
62
- end
63
-
64
- it "last revision has the default tag" do
65
- @project.revisions.last.tag.should=="2.0.0"
66
- end
67
-
68
- it "has 2 revisions" do
69
- @project.revisions.count.should==2
48
+ describe "when I do not want a new revision" do
49
+ before :all do
50
+ @project.save
51
+ end
52
+
53
+ it "has 1 revisions" do
54
+ @project.revisions.count.should==1
55
+ end
56
+
57
+ it "it is at revision 0" do
58
+ @project.revision.should==0
59
+ end
60
+
61
+ it "has a single milestone" do
62
+ @project.milestones.count==1
63
+ end
64
+ end
65
+
66
+ describe "when I want a new revision" do
67
+ before :all do
68
+ @project.revise
69
+ end
70
+
71
+ it "has 2 revisions" do
72
+ @project.revisions.count.should==2
73
+ end
74
+
75
+ it "it is at revision 0" do
76
+ @project.revision.should==0
77
+ end
78
+
79
+ it "last revision is at revision 1" do
80
+ @project.revisions.last.revision.should==1
81
+ end
82
+
83
+ it "last revision has the default tag" do
84
+ @project.revisions.last.tag.should=="1.0.0"
85
+ end
86
+
87
+ it "has 2 revisions" do
88
+ @project.revisions.count.should==2
89
+ end
90
+
91
+ it "creates a new milestone" do
92
+ Milestone.count.should==2
93
+ end
94
+
95
+ it "last revision has a single milestone" do
96
+ #puts @project.revisions.last.inspect
97
+ @project.revisions.last.milestones.count.should==1
70
98
  end
71
99
  end
72
100
 
73
- describe "adding a random number of revisions" do
74
- it "should give me the right number of revisions" do
75
- count = rand(10)
76
- count.times do
77
- @project.revise
78
- end
79
- @project.revisions.count.should==(count+2)
80
- end
81
- end
82
-
101
+ describe "adding a random number of revisions" do
102
+ it "should give me the right number of revisions" do
103
+ count = rand(10)
104
+ count.times do
105
+ @project.revise
106
+ end
107
+ @project.revisions.count.should==(count+2)
108
+ end
109
+ end
83
110
  end
metadata CHANGED
@@ -1,57 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongoid_revisions
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Emiliano Della Casa
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-08 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2012-02-13 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: rspec
16
- requirement: &80569250 !ruby/object:Gem::Requirement
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
17
19
  none: false
18
- requirements:
20
+ requirements:
19
21
  - - ~>
20
- - !ruby/object:Gem::Version
22
+ - !ruby/object:Gem::Version
21
23
  version: 2.8.0
22
24
  type: :development
23
- prerelease: false
24
- version_requirements: *80569250
25
- - !ruby/object:Gem::Dependency
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
26
27
  name: mongoid
27
- requirement: &80568200 !ruby/object:Gem::Requirement
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
28
30
  none: false
29
- requirements:
31
+ requirements:
30
32
  - - ~>
31
- - !ruby/object:Gem::Version
33
+ - !ruby/object:Gem::Version
32
34
  version: 2.4.2
33
35
  type: :development
34
- prerelease: false
35
- version_requirements: *80568200
36
- - !ruby/object:Gem::Dependency
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
37
38
  name: bson_ext
38
- requirement: &80567950 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
39
41
  none: false
40
- requirements:
42
+ requirements:
41
43
  - - ~>
42
- - !ruby/object:Gem::Version
44
+ - !ruby/object:Gem::Version
43
45
  version: 1.5.2
44
46
  type: :development
45
- prerelease: false
46
- version_requirements: *80567950
47
- description: Add support for revisions to your Mongoid documents by creating a new
48
- version of a document every time you change it and replicating all of its associations
49
- email:
47
+ version_requirements: *id003
48
+ 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
+ email:
50
50
  - e.dellacasa@engim.eu
51
51
  executables: []
52
+
52
53
  extensions: []
54
+
53
55
  extra_rdoc_files: []
54
- files:
56
+
57
+ files:
55
58
  - .gitignore
56
59
  - Gemfile
57
60
  - README.md
@@ -60,34 +63,36 @@ files:
60
63
  - lib/mongoid_revisions/version.rb
61
64
  - mongoid_revisions.gemspec
62
65
  - spec/.rspec
66
+ - spec/mongoid/models/milestone.rb
63
67
  - spec/mongoid/models/project.rb
64
68
  - spec/mongoid_revisions/revisions_spec.rb
65
69
  - spec/spec_helper.rb
66
- homepage: ''
70
+ homepage: ""
67
71
  licenses: []
72
+
68
73
  post_install_message:
69
74
  rdoc_options: []
70
- require_paths:
75
+
76
+ require_paths:
71
77
  - lib
72
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
73
79
  none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
85
  none: false
80
- requirements:
81
- - - ! '>='
82
- - !ruby/object:Gem::Version
83
- version: '0'
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
84
90
  requirements: []
91
+
85
92
  rubyforge_project: mongoid_revisions
86
- rubygems_version: 1.8.11
93
+ rubygems_version: 1.8.16
87
94
  signing_key:
88
95
  specification_version: 3
89
96
  summary: Add support for revisions to your Mongoid documents
90
- test_files:
91
- - spec/mongoid/models/project.rb
92
- - spec/mongoid_revisions/revisions_spec.rb
93
- - spec/spec_helper.rb
97
+ test_files: []
98
+