mongo_mapper_acts_as_versioned 0.1.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +13 -10
- data/lib/acts_as_versioned.rb +115 -91
- data/spec/acts_as_versioned_spec.rb +17 -17
- data/spec/spec_helper.rb +1 -1
- metadata +77 -43
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c49bf4f793bea61d5a8867b53f06bbb9a2da305e
|
4
|
+
data.tar.gz: 22854f1a7dad3d65dc35bb97ceecab1234c18041
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f1066c0eedbb1eb1c3b1feb3d276c4cd0a30b69d37d7424812707aaf997413025824733e8a1be03948af062e5c686d9399a4a22dffc9f103ca275ba9360486c
|
7
|
+
data.tar.gz: f6c2ec523dfcf6caa2ae7b3d35c520937ffc104009395c3bfbc82ddf2dd7903d8a2736234149b70454579f01e3039600a800eff79e79e7be09fa5449a28a4370
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# ActsAsVersioned for [MongoMapper](http://github.com/
|
1
|
+
# ActsAsVersioned for [MongoMapper](http://github.com/mongomapper/mongomapper)
|
2
2
|
|
3
|
-
Basic MongoMapper port of technoweenie's [acts_as_versioned](http://github.com/technoweenie/acts_as_versioned).
|
3
|
+
Basic MongoMapper port of technoweenie's [acts_as_versioned](http://github.com/technoweenie/acts_as_versioned).
|
4
|
+
Stores changed attributes in a single collection using a general Hash key.
|
4
5
|
|
5
|
-
*Note:* This plugin is intended for MongoMapper 0.8.6. The plugin architecture must be slightly changed for it to work on 0.9+.
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
@@ -51,17 +51,20 @@ Default ignored keys are:
|
|
51
51
|
|
52
52
|
### Ignoring additional keys
|
53
53
|
|
54
|
-
Simply add `
|
54
|
+
Simply add `do_not_version 'new_skipped_key', 'another_skipped_key'` somewhere in your model.
|
55
55
|
|
56
56
|
## Tested with
|
57
57
|
|
58
|
-
* MongoMapper 0.
|
59
|
-
* Ruby 1.9.2
|
58
|
+
* MongoMapper 0.11.1, 0.13.1, 0.14.0
|
59
|
+
* Ruby 1.9.2 up to 2.4.0
|
60
60
|
|
61
|
-
##
|
61
|
+
## Older MongoMapper versions
|
62
62
|
|
63
|
-
*
|
64
|
-
|
63
|
+
* For MongoMapper <= 0.8.6, see the master branch of this repository.
|
64
|
+
|
65
|
+
## Older embedded document versions
|
66
|
+
|
67
|
+
* See the next branch of this repository.
|
65
68
|
|
66
69
|
## Bundler note
|
67
70
|
|
@@ -69,4 +72,4 @@ Make sure to specify `:require => 'acts_as_versioned'` in your Gemfile.
|
|
69
72
|
|
70
73
|
## Copyright
|
71
74
|
|
72
|
-
|
75
|
+
See LICENSE.
|
data/lib/acts_as_versioned.rb
CHANGED
@@ -1,133 +1,157 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require "active_model"
|
3
|
+
if ActiveSupport.version >= Gem::Version.new("5.0.0")
|
4
|
+
begin
|
5
|
+
require "activemodel-serializers-xml"
|
6
|
+
rescue LoadError
|
7
|
+
warn "Couldn't load AM serializers. Things might break."
|
8
|
+
end
|
9
|
+
end
|
10
|
+
require "mongo_mapper"
|
11
|
+
|
1
12
|
module MongoMapper
|
2
13
|
module Acts
|
3
14
|
module Versioned
|
4
|
-
|
5
|
-
|
15
|
+
class DocumentVersion
|
16
|
+
include MongoMapper::Document
|
6
17
|
|
7
|
-
|
8
|
-
model.class_eval do
|
9
|
-
cattr_accessor :versioned_class_name,
|
10
|
-
:non_versioned_keys, :max_version_limit
|
18
|
+
set_collection_name "document_versions"
|
11
19
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
]
|
20
|
+
key :modified, Hash
|
21
|
+
key :version, Integer
|
22
|
+
key :entity_id, ObjectId
|
23
|
+
key :entity_type, String
|
24
|
+
timestamps!
|
18
25
|
|
19
|
-
|
20
|
-
|
26
|
+
belongs_to :entity, polymorphic: true
|
27
|
+
end
|
21
28
|
|
22
|
-
|
23
|
-
key :modified, Hash
|
24
|
-
end
|
29
|
+
extend ActiveSupport::Concern
|
25
30
|
|
26
|
-
|
27
|
-
|
28
|
-
def [](version)
|
29
|
-
detect { |doc| doc.version.to_s == version.to_s }
|
30
|
-
end
|
31
|
-
end
|
31
|
+
VERSION = '0.3.1'
|
32
|
+
CALLBACKS = [:save_version, :clear_old_versions]
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
before_save :clear_old_versions
|
36
|
-
end
|
37
|
-
end
|
34
|
+
included do
|
35
|
+
class_attribute :non_versioned_keys, :max_version_limit
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
self.max_version_limit = 0
|
38
|
+
self.non_versioned_keys = %w(
|
39
|
+
_id _type created_at updated_at
|
40
|
+
creator_id updater_id version
|
41
|
+
)
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
key :version, Integer
|
44
|
+
before_save :save_version
|
45
|
+
before_save :clear_old_versions
|
46
|
+
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
def versions
|
49
|
+
MongoMapper::Acts::Versioned::DocumentVersion.where(
|
50
|
+
entity_type: self.class.name, entity_id: self.id
|
51
|
+
)
|
52
|
+
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
def document_version(given_version)
|
55
|
+
versions.where(version: given_version).first
|
56
|
+
end
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
58
|
+
def current_document_version
|
59
|
+
document_version(version)
|
60
|
+
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
else
|
65
|
-
return false unless rev = versions[rev]
|
66
|
-
end
|
62
|
+
def save_version
|
63
|
+
if new_record? || save_version?
|
64
|
+
self.version = next_version
|
67
65
|
|
68
|
-
|
69
|
-
|
66
|
+
rev = MongoMapper::Acts::Versioned::DocumentVersion.new
|
67
|
+
rev.entity_type = self.class.name
|
68
|
+
rev.entity_id = id
|
69
|
+
rev.version = version
|
70
70
|
|
71
|
-
|
72
|
-
end
|
71
|
+
clone_attributes(self, rev)
|
73
72
|
|
74
|
-
|
75
|
-
revert_to(rev) and save_without_revision or false
|
73
|
+
rev.save
|
76
74
|
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def clear_old_versions
|
78
|
+
return if self.class.max_version_limit == 0
|
79
|
+
excess_bagage = version.to_i - self.class.max_version_limit
|
77
80
|
|
78
|
-
|
79
|
-
|
80
|
-
true
|
81
|
-
rescue
|
82
|
-
false
|
81
|
+
if excess_bagage > 0
|
82
|
+
versions.select { |v| v.version.to_i <= excess_bagage }.map(&:delete)
|
83
83
|
end
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
86
|
+
def revert_to(rev)
|
87
|
+
if rev.is_a?(MongoMapper::Acts::Versioned::DocumentVersion)
|
88
|
+
return false if rev.new_record?
|
89
|
+
else
|
90
|
+
return false unless rev = document_version(rev)
|
87
91
|
end
|
88
92
|
|
89
|
-
|
90
|
-
|
91
|
-
orig_model = orig_model.modified
|
92
|
-
elsif new_model.is_a?(self.class.versioned_class)
|
93
|
-
new_model = new_model.modified
|
94
|
-
end
|
93
|
+
clone_attributes(rev, self)
|
94
|
+
self.version = rev.version
|
95
95
|
|
96
|
-
|
97
|
-
|
98
|
-
end
|
99
|
-
end
|
96
|
+
true
|
97
|
+
end
|
100
98
|
|
101
|
-
|
102
|
-
|
103
|
-
|
99
|
+
def revert_to!(rev)
|
100
|
+
revert_to(rev) and save_without_revision or false
|
101
|
+
end
|
104
102
|
|
105
|
-
|
106
|
-
|
103
|
+
def save_without_revision
|
104
|
+
save_without_revision!
|
105
|
+
true
|
106
|
+
rescue
|
107
|
+
false
|
108
|
+
end
|
109
|
+
|
110
|
+
def save_without_revision!
|
111
|
+
without_revision { save! }
|
112
|
+
end
|
113
|
+
|
114
|
+
def clone_attributes(orig_model, new_model)
|
115
|
+
if orig_model.is_a?(MongoMapper::Acts::Versioned::DocumentVersion)
|
116
|
+
orig_model = orig_model.modified
|
117
|
+
elsif new_model.is_a?(MongoMapper::Acts::Versioned::DocumentVersion)
|
118
|
+
new_model = new_model.modified
|
107
119
|
end
|
108
120
|
|
109
|
-
|
121
|
+
self.class.versioned_keys.each do |attribute|
|
122
|
+
new_model[attribute] = orig_model[attribute]
|
110
123
|
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def save_version?
|
127
|
+
(self.class.versioned_keys & changed).any?
|
128
|
+
end
|
111
129
|
|
112
|
-
|
130
|
+
def without_revision(&block)
|
131
|
+
self.class.without_revision(&block)
|
132
|
+
end
|
113
133
|
|
114
|
-
|
115
|
-
|
116
|
-
1 : versions.map(&:version).max.next
|
117
|
-
end
|
118
|
-
end # InstanceMethods
|
134
|
+
def empty_callback
|
135
|
+
end
|
119
136
|
|
120
|
-
|
121
|
-
def versioned_class
|
122
|
-
const_get versioned_class_name
|
123
|
-
end
|
137
|
+
protected
|
124
138
|
|
139
|
+
def next_version
|
140
|
+
new_record? || versions.count == 0 ?
|
141
|
+
1 : versions.fields(:version).map(&:version).max.next
|
142
|
+
end
|
143
|
+
|
144
|
+
module ClassMethods
|
125
145
|
def versioned_keys
|
126
146
|
keys.keys - non_versioned_keys
|
127
147
|
end
|
128
148
|
|
129
149
|
def do_not_version(*args)
|
130
|
-
self.non_versioned_keys
|
150
|
+
self.non_versioned_keys |= args
|
151
|
+
end
|
152
|
+
|
153
|
+
def max_versions(amount)
|
154
|
+
self.max_version_limit = amount
|
131
155
|
end
|
132
156
|
|
133
157
|
def without_revision
|
@@ -19,17 +19,12 @@ describe MongoMapper::Acts::Versioned do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
it 'should set the correct properties on the version class' do
|
23
|
-
Landmark.versioned_class.should == Landmark::Version
|
24
|
-
Sublandmark.versioned_class.should == Landmark::Version
|
25
|
-
end
|
26
|
-
|
27
22
|
it 'should save a versioned copy' do
|
28
23
|
l = Landmark.create(:title => 'title')
|
29
|
-
l.new_record?.should
|
24
|
+
l.new_record?.should be_falsy
|
30
25
|
l.versions.size.should == 1
|
31
26
|
l.version.should == 1
|
32
|
-
l.versions.first.should be_a(
|
27
|
+
l.versions.first.should be_a(MongoMapper::Acts::Versioned::DocumentVersion)
|
33
28
|
end
|
34
29
|
|
35
30
|
it 'should clear old versions when a limit is set' do
|
@@ -80,7 +75,7 @@ describe MongoMapper::Acts::Versioned do
|
|
80
75
|
l.versions.size.should == 10
|
81
76
|
l.title.should == 'title10'
|
82
77
|
|
83
|
-
l.revert_to!(7).should
|
78
|
+
l.revert_to!(7).should be_truthy
|
84
79
|
l = l.reload
|
85
80
|
l.version.should == 7
|
86
81
|
l.versions.size.should == 10
|
@@ -99,7 +94,7 @@ describe MongoMapper::Acts::Versioned do
|
|
99
94
|
l.versions.size.should == 10
|
100
95
|
l.title.should == 'title10'
|
101
96
|
|
102
|
-
l.revert_to!(l.
|
97
|
+
l.revert_to!(l.document_version(7)).should be_truthy
|
103
98
|
l = l.reload
|
104
99
|
l.version.should == 7
|
105
100
|
l.versions.size.should == 10
|
@@ -114,7 +109,7 @@ describe MongoMapper::Acts::Versioned do
|
|
114
109
|
end
|
115
110
|
|
116
111
|
l_version = l.reload.versions.last
|
117
|
-
l_version.
|
112
|
+
l_version.entity.should == l.reload
|
118
113
|
end
|
119
114
|
|
120
115
|
it 'should not create new versions for skipped keys' do
|
@@ -157,20 +152,25 @@ describe MongoMapper::Acts::Versioned do
|
|
157
152
|
|
158
153
|
it 'should store changes in a hash' do
|
159
154
|
l = Landmark.create(:title => 'title')
|
160
|
-
l.
|
155
|
+
l.document_version(1).modified.should == {'title' => 'title'}
|
161
156
|
|
162
157
|
l.update_attributes(:title => 'changed title', :depth => 1)
|
163
|
-
l.reload.
|
158
|
+
l.reload.document_version(2).modified.should == {'title' => 'changed title'}
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should save when a version was created' do
|
162
|
+
l = Landmark.create(:title => 'title')
|
163
|
+
l.document_version(1).created_at.should be_instance_of(Time)
|
164
164
|
end
|
165
165
|
|
166
166
|
it 'should save a versioned class with sci' do
|
167
167
|
s = Sublandmark.create!(:title => 'first title')
|
168
|
-
s.new_record?.should
|
168
|
+
s.new_record?.should be_falsy
|
169
169
|
s.version.should == 1
|
170
170
|
|
171
171
|
s.versions.size.should == 1
|
172
|
-
s.versions.first.should be_a(
|
173
|
-
s.versions.first.
|
172
|
+
s.versions.first.should be_a(MongoMapper::Acts::Versioned::DocumentVersion)
|
173
|
+
s.versions.first.entity_type.should == "Sublandmark"
|
174
174
|
end
|
175
175
|
|
176
176
|
it 'should rollback with sci' do
|
@@ -184,7 +184,7 @@ describe MongoMapper::Acts::Versioned do
|
|
184
184
|
l.version.should == 5
|
185
185
|
l.versions.size.should == 5
|
186
186
|
l.title.should == 'other title5'
|
187
|
-
l.revert_to!(3).should
|
187
|
+
l.revert_to!(3).should be_truthy
|
188
188
|
l = l.reload
|
189
189
|
l.version.should == 3
|
190
190
|
l.versions.size.should == 5
|
@@ -201,7 +201,7 @@ describe MongoMapper::Acts::Versioned do
|
|
201
201
|
s.version.should == 5
|
202
202
|
s.versions.size.should == 5
|
203
203
|
s.title.should == 'title5'
|
204
|
-
s.revert_to!(3).should
|
204
|
+
s.revert_to!(3).should be_truthy
|
205
205
|
s = s.reload
|
206
206
|
s.version.should == 3
|
207
207
|
s.versions.size.should == 5
|
data/spec/spec_helper.rb
CHANGED
@@ -8,7 +8,7 @@ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
|
|
8
8
|
MongoMapper.database = 'test'
|
9
9
|
MongoMapper.database.collections.each {|c| c.drop_indexes }
|
10
10
|
|
11
|
-
|
11
|
+
RSpec.configure do |config|
|
12
12
|
config.after :each do
|
13
13
|
MongoMapper.database.collections.each do |collection|
|
14
14
|
collection.remove unless collection.name =~ /(.*\.)?system\..*/
|
metadata
CHANGED
@@ -1,71 +1,105 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper_acts_as_versioned
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Gigamo
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
date: 2017-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongo_mapper
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
18
35
|
prerelease: false
|
19
|
-
|
20
|
-
|
21
|
-
requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
22
38
|
- - ">="
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
25
48
|
type: :development
|
26
|
-
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
27
69
|
description: Basic MongoMapper port of technoweenie's acts_as_versioned
|
28
|
-
email:
|
29
|
-
-
|
70
|
+
email:
|
71
|
+
- kdwinter@protonmail.com
|
30
72
|
executables: []
|
31
|
-
|
32
73
|
extensions: []
|
33
|
-
|
34
74
|
extra_rdoc_files: []
|
35
|
-
|
36
|
-
|
75
|
+
files:
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
37
78
|
- lib/acts_as_versioned.rb
|
38
79
|
- spec/acts_as_versioned_spec.rb
|
39
80
|
- spec/spec_helper.rb
|
40
|
-
- LICENSE
|
41
|
-
- README.md
|
42
|
-
has_rdoc: true
|
43
81
|
homepage: http://github.com/gigamo/mongo_mapper_acts_as_versioned
|
44
|
-
licenses:
|
45
|
-
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
46
85
|
post_install_message:
|
47
86
|
rdoc_options: []
|
48
|
-
|
49
|
-
require_paths:
|
87
|
+
require_paths:
|
50
88
|
- lib
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
|
53
|
-
requirements:
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
54
91
|
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
-
|
59
|
-
requirements:
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
60
96
|
- - ">="
|
61
|
-
- !ruby/object:Gem::Version
|
97
|
+
- !ruby/object:Gem::Version
|
62
98
|
version: 1.3.6
|
63
99
|
requirements: []
|
64
|
-
|
65
100
|
rubyforge_project: mongo_mapper_acts_as_versioned
|
66
|
-
rubygems_version:
|
101
|
+
rubygems_version: 2.6.11
|
67
102
|
signing_key:
|
68
|
-
specification_version:
|
103
|
+
specification_version: 4
|
69
104
|
summary: Basic MongoMapper port of technoweenie's acts_as_versioned
|
70
105
|
test_files: []
|
71
|
-
|