vidibus-versioning 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -12
- data/lib/vidibus/versioning/_version.rb +1 -1
- data/lib/vidibus/versioning/mongoid.rb +19 -5
- metadata +27 -4
data/README.md
CHANGED
@@ -60,32 +60,36 @@ article.redo! # restores next version
|
|
60
60
|
There is also a method `version` that loads an exisiting version of the record or instantiates a new one:
|
61
61
|
|
62
62
|
```ruby
|
63
|
-
article.version(3)
|
64
|
-
article.version(:previous)
|
65
|
-
article.version(:next)
|
66
|
-
article.version(:new)
|
63
|
+
article.version(3) # returns version 3 of the article
|
64
|
+
article.version(:previous) # returns the previous version of the article
|
65
|
+
article.version(:next) # returns the next version of the article
|
66
|
+
article.version(:new) # returns a new version of the article
|
67
67
|
```
|
68
68
|
|
69
69
|
To apply a version on the current article itself, call `version` with a bang!:
|
70
70
|
|
71
71
|
```ruby
|
72
|
-
|
72
|
+
article.version!(3) # applies version 3 to the article and returns nil
|
73
73
|
```
|
74
74
|
|
75
|
-
It is even possible to apply versioned attributes directly by adding
|
75
|
+
It is even possible to apply versioned attributes directly by adding them to the `version` call:
|
76
76
|
|
77
77
|
```ruby
|
78
|
-
|
78
|
+
article.version(3, :title => "Wicked!") # returns version 3 with a new title applied
|
79
79
|
```
|
80
80
|
|
81
81
|
You may treat the article object with an applied version like the article itself. All changes will
|
82
|
-
be applied to the loaded version instead of the current instance.
|
82
|
+
be applied to the loaded version instead of the current instance. This is useful for creating future versions
|
83
|
+
that can be scheduled by [Vidibus::VersionScheduler](https://github.com/vidibus/vidibus-version_scheduler).
|
84
|
+
|
85
|
+
A workflow example:
|
83
86
|
|
84
87
|
```ruby
|
85
|
-
article.
|
86
|
-
|
87
|
-
|
88
|
-
|
88
|
+
article = Article.create(:title => "Old shit")
|
89
|
+
future_article = article.version(:new) # initialize a new version
|
90
|
+
future_article.updated_at = 1.day.since # set a date in the future
|
91
|
+
future_article.title = "New shit" # set the new title
|
92
|
+
future_article.save # save the version
|
89
93
|
```
|
90
94
|
|
91
95
|
|
@@ -10,13 +10,14 @@ module Vidibus
|
|
10
10
|
has_many :versions, :as => :versioned, :class_name => "Vidibus::Versioning::Version", :dependent => :destroy
|
11
11
|
|
12
12
|
field :version_number, :type => Integer, :default => 1
|
13
|
+
field :version_updated_at, :type => Time
|
13
14
|
|
14
15
|
after_initialize :original_attributes
|
15
16
|
before_update :reset_version_cache
|
16
17
|
|
17
18
|
mattr_accessor :versioned_attributes, :unversioned_attributes, :versioning_options
|
18
19
|
self.versioned_attributes = []
|
19
|
-
self.unversioned_attributes = %w[_id _type uuid updated_at created_at version_number]
|
20
|
+
self.unversioned_attributes = %w[_id _type uuid updated_at created_at version_number version_updated_at]
|
20
21
|
self.versioning_options = {}
|
21
22
|
|
22
23
|
# Returns the attributes that should be versioned.
|
@@ -40,7 +41,6 @@ module Vidibus
|
|
40
41
|
#
|
41
42
|
def versioned(*args)
|
42
43
|
options = args.extract_options!
|
43
|
-
|
44
44
|
self.versioned_attributes = args.map {|a| a.to_s} if args.any?
|
45
45
|
self.versioning_options = options if options.any?
|
46
46
|
end
|
@@ -130,6 +130,11 @@ module Vidibus
|
|
130
130
|
version_obj and version_obj.new_record?
|
131
131
|
end
|
132
132
|
|
133
|
+
# Returns the time when versioned attributes have been updated.
|
134
|
+
def version_updated_at
|
135
|
+
read_attribute(:version_updated_at) || updated_at
|
136
|
+
end
|
137
|
+
|
133
138
|
protected
|
134
139
|
|
135
140
|
# Applies version on self. Returns nil
|
@@ -141,7 +146,10 @@ module Vidibus
|
|
141
146
|
|
142
147
|
self.attributes = version_attributes
|
143
148
|
self.version_number = version_cache.wanted_version_number
|
144
|
-
|
149
|
+
if time = version_obj.created_at
|
150
|
+
self.updated_at = time
|
151
|
+
self.version_updated_at = time
|
152
|
+
end
|
145
153
|
nil
|
146
154
|
end
|
147
155
|
|
@@ -157,10 +165,15 @@ module Vidibus
|
|
157
165
|
end
|
158
166
|
|
159
167
|
# Returns original attributes with attributes of version object and wanted attributes merged in.
|
168
|
+
# TODO: Only return attributes that are present on the object. They may have changed.
|
160
169
|
def version_attributes
|
161
170
|
# TODO: Setting the following line will cause DelayedJob to loop endlessly. The same should happen if an embedded document is defined as versioned_attribute!
|
162
171
|
# original_attributes.merge(version_obj.versioned_attributes).merge(version_cache.wanted_attributes.stringify_keys!)
|
163
|
-
|
172
|
+
|
173
|
+
Hash[*self.class.fields.keys.zip([]).flatten]. # ensure nil fields are included as well
|
174
|
+
merge(version_obj.versioned_attributes). # add version's attributes
|
175
|
+
merge(version_cache.wanted_attributes.stringify_keys!). # add options provided with #version call
|
176
|
+
except(self.class.unversioned_attributes) # remove unversioned attributes; they may have changed
|
164
177
|
end
|
165
178
|
|
166
179
|
# Sets instance variables used for versioning.
|
@@ -211,7 +224,7 @@ module Vidibus
|
|
211
224
|
obj
|
212
225
|
else
|
213
226
|
editing_time = self.class.versioning_options[:editing_time]
|
214
|
-
if !editing_time or
|
227
|
+
if !editing_time or version_updated_at <= (Time.now-editing_time.to_i) or version_updated_at > Time.now # allow future versions
|
215
228
|
versions.build(:created_at => updated_at_was)
|
216
229
|
end
|
217
230
|
end
|
@@ -261,6 +274,7 @@ module Vidibus
|
|
261
274
|
elsif version_obj
|
262
275
|
version_obj.update_attributes!(:versioned_attributes => original_attributes)
|
263
276
|
self.version_number = version_obj.number + 1
|
277
|
+
self.version_updated_at = Time.now
|
264
278
|
end
|
265
279
|
nil
|
266
280
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vidibus-versioning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Andre Pankratz
|
@@ -21,9 +22,11 @@ dependencies:
|
|
21
22
|
name: mongoid
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ~>
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
27
30
|
segments:
|
28
31
|
- 2
|
29
32
|
version: "2"
|
@@ -33,9 +36,11 @@ dependencies:
|
|
33
36
|
name: vidibus-core_extensions
|
34
37
|
prerelease: false
|
35
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
36
40
|
requirements:
|
37
41
|
- - ">="
|
38
42
|
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
39
44
|
segments:
|
40
45
|
- 0
|
41
46
|
version: "0"
|
@@ -45,9 +50,11 @@ dependencies:
|
|
45
50
|
name: vidibus-uuid
|
46
51
|
prerelease: false
|
47
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
48
54
|
requirements:
|
49
55
|
- - ">="
|
50
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
51
58
|
segments:
|
52
59
|
- 0
|
53
60
|
version: "0"
|
@@ -57,9 +64,11 @@ dependencies:
|
|
57
64
|
name: bundler
|
58
65
|
prerelease: false
|
59
66
|
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
60
68
|
requirements:
|
61
69
|
- - ">="
|
62
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 23
|
63
72
|
segments:
|
64
73
|
- 1
|
65
74
|
- 0
|
@@ -71,9 +80,11 @@ dependencies:
|
|
71
80
|
name: rake
|
72
81
|
prerelease: false
|
73
82
|
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
74
84
|
requirements:
|
75
85
|
- - ">="
|
76
86
|
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
77
88
|
segments:
|
78
89
|
- 0
|
79
90
|
version: "0"
|
@@ -83,9 +94,11 @@ dependencies:
|
|
83
94
|
name: rdoc
|
84
95
|
prerelease: false
|
85
96
|
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
86
98
|
requirements:
|
87
99
|
- - ">="
|
88
100
|
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
89
102
|
segments:
|
90
103
|
- 0
|
91
104
|
version: "0"
|
@@ -95,9 +108,11 @@ dependencies:
|
|
95
108
|
name: rcov
|
96
109
|
prerelease: false
|
97
110
|
requirement: &id007 !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
98
112
|
requirements:
|
99
113
|
- - ">="
|
100
114
|
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
101
116
|
segments:
|
102
117
|
- 0
|
103
118
|
version: "0"
|
@@ -107,9 +122,11 @@ dependencies:
|
|
107
122
|
name: rspec
|
108
123
|
prerelease: false
|
109
124
|
requirement: &id008 !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
110
126
|
requirements:
|
111
127
|
- - ~>
|
112
128
|
- !ruby/object:Gem::Version
|
129
|
+
hash: 7
|
113
130
|
segments:
|
114
131
|
- 2
|
115
132
|
version: "2"
|
@@ -119,9 +136,11 @@ dependencies:
|
|
119
136
|
name: rr
|
120
137
|
prerelease: false
|
121
138
|
requirement: &id009 !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
122
140
|
requirements:
|
123
141
|
- - ">="
|
124
142
|
- !ruby/object:Gem::Version
|
143
|
+
hash: 3
|
125
144
|
segments:
|
126
145
|
- 0
|
127
146
|
version: "0"
|
@@ -155,16 +174,20 @@ rdoc_options: []
|
|
155
174
|
require_paths:
|
156
175
|
- lib
|
157
176
|
required_ruby_version: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
158
178
|
requirements:
|
159
179
|
- - ">="
|
160
180
|
- !ruby/object:Gem::Version
|
181
|
+
hash: 3
|
161
182
|
segments:
|
162
183
|
- 0
|
163
184
|
version: "0"
|
164
185
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
none: false
|
165
187
|
requirements:
|
166
188
|
- - ">="
|
167
189
|
- !ruby/object:Gem::Version
|
190
|
+
hash: 23
|
168
191
|
segments:
|
169
192
|
- 1
|
170
193
|
- 3
|
@@ -173,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
196
|
requirements: []
|
174
197
|
|
175
198
|
rubyforge_project: vidibus-versioning
|
176
|
-
rubygems_version: 1.
|
199
|
+
rubygems_version: 1.6.2
|
177
200
|
signing_key:
|
178
201
|
specification_version: 3
|
179
202
|
summary: Advanced versioning for Mongoid models
|