mongoid-history 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -0
- data/VERSION +1 -1
- data/lib/mongoid/history.rb +1 -0
- data/lib/mongoid/history/trackable.rb +43 -1
- data/lib/mongoid/history/tracker.rb +11 -3
- data/mongoid-history.gemspec +2 -2
- data/spec/integration/integration_spec.rb +57 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -82,7 +82,20 @@ Here is a quick example on how to use this plugin. For more details, please look
|
|
82
82
|
|
83
83
|
track.redo! # comment title should be "Test 2"
|
84
84
|
|
85
|
+
# undo last change
|
86
|
+
comment.undo! @user
|
85
87
|
|
88
|
+
# undo versions 1 - 4
|
89
|
+
comment.undo! @user, :from => 1, :to => 4
|
90
|
+
|
91
|
+
# undo last 3 versions
|
92
|
+
comment.undo! @user, :last => 3
|
93
|
+
|
94
|
+
# redo versions 1 - 4
|
95
|
+
comment.redo! @user, :from => 1, :to => 4
|
96
|
+
|
97
|
+
# redo last 3 versions
|
98
|
+
comment.redo! @user, :last => 3
|
86
99
|
|
87
100
|
== Contributing to mongoid-history
|
88
101
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
data/lib/mongoid/history.rb
CHANGED
@@ -42,7 +42,6 @@ module Mongoid::History
|
|
42
42
|
|
43
43
|
before_update :track_update
|
44
44
|
before_create :track_create if options[:track_create]
|
45
|
-
|
46
45
|
|
47
46
|
Mongoid::History.trackable_classes ||= []
|
48
47
|
Mongoid::History.trackable_classes << self
|
@@ -67,7 +66,50 @@ module Mongoid::History
|
|
67
66
|
@history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => triverse_association_chain)
|
68
67
|
end
|
69
68
|
|
69
|
+
# undo :from => 1, :to => 5
|
70
|
+
# undo 4
|
71
|
+
# undo :last => 10
|
72
|
+
def undo!(modifier, options_or_version=nil)
|
73
|
+
versions = get_versions_criteria(options_or_version).to_a
|
74
|
+
versions.sort!{|v1, v2| v2.version <=> v1.version}
|
75
|
+
|
76
|
+
versions.each do |v|
|
77
|
+
undo_attr = v.undo_attr(modifier)
|
78
|
+
self.attributes = v.undo_attr(modifier)
|
79
|
+
end
|
80
|
+
save!
|
81
|
+
end
|
82
|
+
|
83
|
+
def redo!(modifier, options_or_version=nil)
|
84
|
+
versions = get_versions_criteria(options_or_version).to_a
|
85
|
+
versions.sort!{|v1, v2| v1.version <=> v2.version}
|
86
|
+
|
87
|
+
versions.each do |v|
|
88
|
+
redo_attr = v.redo_attr(modifier)
|
89
|
+
self.attributes = redo_attr
|
90
|
+
end
|
91
|
+
save!
|
92
|
+
end
|
93
|
+
|
70
94
|
private
|
95
|
+
def get_versions_criteria(options_or_version)
|
96
|
+
if options_or_version.is_a? Hash
|
97
|
+
options = options_or_version
|
98
|
+
if options[:from] && options[:to]
|
99
|
+
versions = history_tracks.where(:version.in => (options[:from] .. options[:to]).to_a)
|
100
|
+
elsif options[:last]
|
101
|
+
versions = history_tracks.limit(options[:last])
|
102
|
+
else
|
103
|
+
raise "Invalid options, please specify (:from / :to) keys or :last key."
|
104
|
+
end
|
105
|
+
else
|
106
|
+
version = options_or_version || self.attributes[history_trackable_options[:version_field]]
|
107
|
+
version = [ version ].flatten
|
108
|
+
versions = history_tracks.where(:version.in => version)
|
109
|
+
end
|
110
|
+
versions.desc(:version)
|
111
|
+
end
|
112
|
+
|
71
113
|
def should_track_update?
|
72
114
|
track_history? && !modified_attributes_for_update.blank?
|
73
115
|
end
|
@@ -21,19 +21,27 @@ module Mongoid::History
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def undo!(modifier)
|
24
|
+
trackable.update_attributes!(undo_attr(modifier))
|
25
|
+
end
|
26
|
+
|
27
|
+
def redo!(modifier)
|
28
|
+
trackable.update_attributes!(redo_attr(modifier))
|
29
|
+
end
|
30
|
+
|
31
|
+
def undo_attr(modifier)
|
24
32
|
undo_hash = affected.easy_unmerge(modified)
|
25
33
|
undo_hash.easy_merge!(original)
|
26
34
|
modifier_field = trackable.history_trackable_options[:modifier_field]
|
27
35
|
undo_hash[modifier_field] = modifier
|
28
|
-
|
36
|
+
undo_hash
|
29
37
|
end
|
30
38
|
|
31
|
-
def
|
39
|
+
def redo_attr(modifier)
|
32
40
|
redo_hash = affected.easy_unmerge(original)
|
33
41
|
redo_hash.easy_merge!(modified)
|
34
42
|
modifier_field = trackable.history_trackable_options[:modifier_field]
|
35
43
|
redo_hash[modifier_field] = modifier
|
36
|
-
|
44
|
+
redo_hash
|
37
45
|
end
|
38
46
|
|
39
47
|
def trackable_root
|
data/mongoid-history.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid-history}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Aaron Qian"]
|
12
|
-
s.date = %q{2011-03-
|
12
|
+
s.date = %q{2011-03-16}
|
13
13
|
s.description = %q{In frustration of Mongoid::Versioning, I created this plugin for tracking historical changes for any document, including embedded ones. It achieves this by storing all history tracks in a single collection that you define. (See Usage for more details) Embedded documents are referenced by storing an association path, which is an array of document_name and document_id fields starting from the top most parent document and down to the embedded document that should track history.
|
14
14
|
|
15
15
|
This plugin implements multi-user undo, which allows users to undo any history change in any order. Undoing a document also creates a new history track. This is great for auditing and preventing vandalism, but it is probably not suitable for use cases such as a wiki.}
|
@@ -274,6 +274,63 @@ describe Mongoid::History do
|
|
274
274
|
@comment.title.should == @comment2.title
|
275
275
|
end
|
276
276
|
end
|
277
|
+
|
278
|
+
describe "trackables" do
|
279
|
+
before :each do
|
280
|
+
@comment.update_attributes(:title => "Test2") # version == 2
|
281
|
+
@comment.update_attributes(:title => "Test3") # version == 3
|
282
|
+
@comment.update_attributes(:title => "Test4") # version == 4
|
283
|
+
end
|
284
|
+
|
285
|
+
describe "undo" do
|
286
|
+
it "should recognize :from, :to options" do
|
287
|
+
@comment.undo! @user, :from => 2, :to => 4
|
288
|
+
@comment.title.should == "test"
|
289
|
+
end
|
290
|
+
|
291
|
+
it "should recognize parameter as version number" do
|
292
|
+
@comment.undo! @user, 3
|
293
|
+
|
294
|
+
@comment.title.should == "Test2"
|
295
|
+
end
|
296
|
+
|
297
|
+
it "should undo last version when no parameter is specified" do
|
298
|
+
@comment.undo! @user
|
299
|
+
@comment.title.should == "Test3"
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should recognize :last options" do
|
303
|
+
@comment.undo! @user, :last => 2
|
304
|
+
@comment.title.should == "Test2"
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
describe "redo" do
|
309
|
+
before :each do
|
310
|
+
@comment.update_attributes(:title => "Test5")
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should recognize :from, :to options" do
|
314
|
+
@comment.redo! @user, :from => 2, :to => 4
|
315
|
+
@comment.title.should == "Test4"
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should recognize parameter as version number" do
|
319
|
+
@comment.redo! @user, 2
|
320
|
+
@comment.title.should == "Test2"
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should redo last version when no parameter is specified" do
|
324
|
+
@comment.redo! @user
|
325
|
+
@comment.title.should == "Test5"
|
326
|
+
end
|
327
|
+
|
328
|
+
it "should recognize :last options" do
|
329
|
+
@comment.redo! @user, :last => 1
|
330
|
+
@comment.title.should == "Test5"
|
331
|
+
end
|
277
332
|
|
333
|
+
end
|
334
|
+
end
|
278
335
|
end
|
279
336
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid-history
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Aaron Qian
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-16 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -167,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
167
|
requirements:
|
168
168
|
- - ">="
|
169
169
|
- !ruby/object:Gem::Version
|
170
|
-
hash:
|
170
|
+
hash: 1896069804740488378
|
171
171
|
segments:
|
172
172
|
- 0
|
173
173
|
version: "0"
|