mongoid_revisions 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +18 -0
- data/lib/mongoid_revisions/version.rb +3 -0
- data/lib/mongoid_revisions.rb +40 -0
- data/mongoid_revisions.gemspec +24 -0
- data/spec/.rspec +1 -0
- data/spec/mongoid/models/project.rb +7 -0
- data/spec/mongoid_revisions/revisions_spec.rb +83 -0
- data/spec/spec_helper.rb +21 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Mongoid Revisions
|
2
|
+
|
3
|
+
Add support for revisions to your Mongoid documents
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "mongoid_revisions"
|
11
|
+
```
|
12
|
+
|
13
|
+
**Requires Ruby 1.9.2 or later and Mongoid 2.4.2 or later.**
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To add support for revisions to a Mongoid documents, include the module to your Model:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class Comment
|
21
|
+
include Mongoid::Document
|
22
|
+
include Mongoid::Revisions
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
This gem adds to your Mongoid document the following attributes:
|
27
|
+
|
28
|
+
- revision
|
29
|
+
- tag
|
30
|
+
- token
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
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
|
+
|
36
|
+
This gem is created by Emiliano Della Casa and is under the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => ['spec']
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
#require "bundler/gem_tasks"
|
15
|
+
#require 'rspec/core/rake_task'
|
16
|
+
|
17
|
+
#RSpec::Core::RakeTask.new(:spec)
|
18
|
+
#task default: :spec
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "mongoid_revisions/version"
|
2
|
+
|
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
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mongoid_revisions/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mongoid_revisions"
|
7
|
+
s.version = MongoidRevisions::VERSION
|
8
|
+
s.authors = ["Emiliano Della Casa"]
|
9
|
+
s.email = ["e.dellacasa@engim.eu"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Add support for revisions to your Mongoid documents}
|
12
|
+
s.description = %q{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}
|
13
|
+
|
14
|
+
s.rubyforge_project = "mongoid_revisions"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.8.0'
|
22
|
+
s.add_development_dependency 'mongoid', '~> 2.4.2'
|
23
|
+
s.add_development_dependency 'bson_ext', '~> 1.5.2'
|
24
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
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
|
31
|
+
|
32
|
+
|
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
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
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
|
+
|
83
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
require 'mongoid'
|
5
|
+
models_folder = File.join(File.dirname(__FILE__), 'mongoid/models')
|
6
|
+
|
7
|
+
Mongoid.configure do |config|
|
8
|
+
name = 'revisions_mongo_test'
|
9
|
+
host = 'localhost'
|
10
|
+
config.master = Mongo::Connection.new.db(name)
|
11
|
+
config.autocreate_indexes = true
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'mongoid_revisions'
|
15
|
+
|
16
|
+
Dir[ File.join(models_folder, '*.rb') ].each { |file|
|
17
|
+
require file
|
18
|
+
file_name = File.basename(file).sub('.rb', '')
|
19
|
+
klass = file_name.classify.constantize
|
20
|
+
klass.collection.drop
|
21
|
+
}
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_revisions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Emiliano Della Casa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-08 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &80569250 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.8.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *80569250
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongoid
|
27
|
+
requirement: &80568200 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.4.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *80568200
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bson_ext
|
38
|
+
requirement: &80567950 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.5.2
|
44
|
+
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:
|
50
|
+
- e.dellacasa@engim.eu
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/mongoid_revisions.rb
|
60
|
+
- lib/mongoid_revisions/version.rb
|
61
|
+
- mongoid_revisions.gemspec
|
62
|
+
- spec/.rspec
|
63
|
+
- spec/mongoid/models/project.rb
|
64
|
+
- spec/mongoid_revisions/revisions_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project: mongoid_revisions
|
86
|
+
rubygems_version: 1.8.11
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
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
|