mongoid-history 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +28 -11
- data/VERSION +1 -1
- data/lib/mongoid-history.rb +2 -0
- data/lib/mongoid/history.rb +1 -0
- data/lib/mongoid/history/sweeper.rb +28 -0
- data/lib/mongoid/history/tracker.rb +6 -0
- data/mongoid-history.gemspec +3 -2
- data/spec/integration/integration_spec.rb +8 -8
- metadata +26 -25
data/README.rdoc
CHANGED
@@ -18,12 +18,29 @@ In your Gemfile:
|
|
18
18
|
|
19
19
|
Here is a quick example on how to use this plugin. For more details, please look at spec/integration/integration_spec.rb. It offers more detailed examples on how to use Mongoid::History.
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
=== Create a History Tracker
|
22
|
+
|
23
|
+
Create a new class to track histories. All histories are stored in this tracker. The name of the class can be anything you like. The only requirement is that it includes `Mongoid::History::Tracker`
|
24
|
+
|
25
|
+
# app/models/history_tracker.rb
|
23
26
|
class HistoryTracker
|
24
27
|
include Mongoid::History::Tracker
|
25
28
|
end
|
26
29
|
|
30
|
+
=== Set Tracker Class Name
|
31
|
+
|
32
|
+
You should manually set the tracker class name to make sure your tracker can be found and loaded properly. You can skip this step if you manually require your tracker before using any trackables. If you don't know what I'm talking about, then you should just follow the example below.
|
33
|
+
|
34
|
+
Here is an example of setting the tracker class name using a rails initializer
|
35
|
+
|
36
|
+
# config/initializers/mongoid-history.rb
|
37
|
+
# initializer for mongoid-history
|
38
|
+
# assuming HistoryTracker is your tracker class
|
39
|
+
Mongoid::History.tracker_class_name = :history_tracker
|
40
|
+
|
41
|
+
|
42
|
+
=== Create Trackable classes and objects
|
43
|
+
|
27
44
|
class Post
|
28
45
|
include Mongoid::Document
|
29
46
|
include Mongoid::Timestamps
|
@@ -62,7 +79,7 @@ Here is a quick example on how to use this plugin. For more details, please look
|
|
62
79
|
track_history :on => [:title, :body], :scope => :post, :track_create => true, :track_destroy => true
|
63
80
|
end
|
64
81
|
|
65
|
-
# The modifier can be specified as well
|
82
|
+
# The modifier can be specified as well
|
66
83
|
class User
|
67
84
|
include Mongoid::Document
|
68
85
|
include Mongoid::Timestamps
|
@@ -80,30 +97,30 @@ Here is a quick example on how to use this plugin. For more details, please look
|
|
80
97
|
|
81
98
|
track = comment.history_tracks.last
|
82
99
|
|
83
|
-
track.undo! # comment title should be "Test"
|
100
|
+
track.undo! user # comment title should be "Test"
|
84
101
|
|
85
|
-
track.redo! # comment title should be "Test 2"
|
102
|
+
track.redo! user # comment title should be "Test 2"
|
86
103
|
|
87
104
|
# undo last change
|
88
|
-
comment.undo!
|
105
|
+
comment.undo! user
|
89
106
|
|
90
107
|
# undo versions 1 - 4
|
91
|
-
comment.undo!
|
108
|
+
comment.undo! user, :from => 4, :to => 1
|
92
109
|
|
93
110
|
# undo last 3 versions
|
94
|
-
comment.undo!
|
111
|
+
comment.undo! user, :last => 3
|
95
112
|
|
96
113
|
# redo versions 1 - 4
|
97
|
-
comment.redo!
|
114
|
+
comment.redo! user, :from => 1, :to => 4
|
98
115
|
|
99
116
|
# redo last 3 versions
|
100
|
-
comment.redo!
|
117
|
+
comment.redo! user, :last => 3
|
101
118
|
|
102
119
|
# delete post
|
103
120
|
post.destroy
|
104
121
|
|
105
122
|
# undelete post
|
106
|
-
post.undo!
|
123
|
+
post.undo! user
|
107
124
|
|
108
125
|
# disable tracking for comments within a block
|
109
126
|
Comment.disable_tracking do
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/mongoid-history.rb
CHANGED
@@ -3,7 +3,9 @@ require 'easy_diff'
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history')
|
4
4
|
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/tracker')
|
5
5
|
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/trackable')
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/sweeper')
|
6
7
|
|
7
8
|
Mongoid::History.modifier_class_name = "User"
|
8
9
|
Mongoid::History.trackable_classes = []
|
9
10
|
Mongoid::History.trackable_class_options = {}
|
11
|
+
Mongoid::History.current_user_method ||= :current_user
|
data/lib/mongoid/history.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid::History
|
2
|
+
class Sweeper < Mongoid::Observer
|
3
|
+
attr_accessor :controller
|
4
|
+
|
5
|
+
def self.observed_classes
|
6
|
+
[Mongoid::History.tracker_class]
|
7
|
+
end
|
8
|
+
|
9
|
+
def before(controller)
|
10
|
+
self.controller = controller
|
11
|
+
true # before method from sweeper should always return true
|
12
|
+
end
|
13
|
+
|
14
|
+
def after(controller)
|
15
|
+
self.controller = controller
|
16
|
+
# Clean up, so that the controller can be collected after this request
|
17
|
+
self.controller = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def before_create(track)
|
21
|
+
track.modifier ||= current_user
|
22
|
+
end
|
23
|
+
|
24
|
+
def current_user
|
25
|
+
controller.send Mongoid::History.current_user_method if controller.respond_to?(Mongoid::History.current_user_method, true)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -15,6 +15,12 @@ module Mongoid::History
|
|
15
15
|
referenced_in :modifier, :class_name => Mongoid::History.modifier_class_name
|
16
16
|
|
17
17
|
Mongoid::History.tracker_class_name = self.name.tableize.singularize.to_sym
|
18
|
+
|
19
|
+
if defined?(ActionController) and defined?(ActionController::Base)
|
20
|
+
ActionController::Base.class_eval do
|
21
|
+
around_filter Mongoid::History::Sweeper.instance
|
22
|
+
end
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
20
26
|
def undo!(modifier)
|
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.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Aaron Qian}, %q{Justin Grimes}]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-11-21}
|
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.}
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"VERSION",
|
30
30
|
"lib/mongoid-history.rb",
|
31
31
|
"lib/mongoid/history.rb",
|
32
|
+
"lib/mongoid/history/sweeper.rb",
|
32
33
|
"lib/mongoid/history/trackable.rb",
|
33
34
|
"lib/mongoid/history/tracker.rb",
|
34
35
|
"mongoid-history.gemspec",
|
@@ -71,15 +71,15 @@ describe Mongoid::History do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should assign scope" do
|
74
|
-
@comment.history_tracks.first.scope == "
|
74
|
+
@comment.history_tracks.first.scope.should == "post"
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should assign method" do
|
78
|
-
@comment.history_tracks.first.action == "create"
|
78
|
+
@comment.history_tracks.first.action.should == "create"
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should assign association_chain" do
|
82
|
-
@comment.history_tracks.first.association_chain
|
82
|
+
@comment.history_tracks.first.association_chain.should == [{'id' => @post.id, 'name' => "Post"}, {'id' => @comment.id, 'name' => "Comment"}]
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -92,12 +92,12 @@ describe Mongoid::History do
|
|
92
92
|
|
93
93
|
it "should assign destroy on track record" do
|
94
94
|
@post.destroy
|
95
|
-
@post.history_tracks.last.action == "destroy"
|
95
|
+
@post.history_tracks.last.action.should == "destroy"
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should return affected attributes from track record" do
|
99
99
|
@post.destroy
|
100
|
-
@post.history_tracks.last.affected["title"] == "Test"
|
100
|
+
@post.history_tracks.last.affected["title"].should == "Test"
|
101
101
|
end
|
102
102
|
|
103
103
|
end
|
@@ -151,12 +151,12 @@ describe Mongoid::History do
|
|
151
151
|
|
152
152
|
it "should assign scope" do
|
153
153
|
@post.update_attributes(:title => "Another Test")
|
154
|
-
@post.history_tracks.first.scope == "
|
154
|
+
@post.history_tracks.first.scope.should == "post"
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should assign association_chain" do
|
158
158
|
@post.update_attributes(:title => "Another Test")
|
159
|
-
@post.history_tracks.first.association_chain
|
159
|
+
@post.history_tracks.first.association_chain.should == [{'id' => @post.id, 'name' => "Post"}]
|
160
160
|
end
|
161
161
|
|
162
162
|
it "should exclude defined options" do
|
@@ -284,7 +284,7 @@ describe Mongoid::History do
|
|
284
284
|
@track = @post.history_tracks.where(:version => 1).first
|
285
285
|
@track.undo!(@user)
|
286
286
|
@track.redo!(@user)
|
287
|
-
Post.where(:_id => @post.id).first == nil
|
287
|
+
Post.where(:_id => @post.id).first.should == nil
|
288
288
|
end
|
289
289
|
end
|
290
290
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-history
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-21 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: easy_diff
|
17
|
-
requirement: &
|
17
|
+
requirement: &30606000 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *30606000
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: mongoid
|
28
|
-
requirement: &
|
28
|
+
requirement: &30605480 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '2.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *30605480
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bson_ext
|
39
|
-
requirement: &
|
39
|
+
requirement: &30604960 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 1.3.1
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *30604960
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec
|
50
|
-
requirement: &
|
50
|
+
requirement: &30604460 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ~>
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: 2.3.0
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *30604460
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: yard
|
61
|
-
requirement: &
|
61
|
+
requirement: &30603900 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ~>
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: 0.6.0
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *30603900
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: bundler
|
72
|
-
requirement: &
|
72
|
+
requirement: &30603360 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ~>
|
@@ -77,10 +77,10 @@ dependencies:
|
|
77
77
|
version: 1.0.0
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *30603360
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: jeweler
|
83
|
-
requirement: &
|
83
|
+
requirement: &30602760 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
86
86
|
- - ~>
|
@@ -88,10 +88,10 @@ dependencies:
|
|
88
88
|
version: 1.5.2
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
|
-
version_requirements: *
|
91
|
+
version_requirements: *30602760
|
92
92
|
- !ruby/object:Gem::Dependency
|
93
93
|
name: rcov
|
94
|
-
requirement: &
|
94
|
+
requirement: &30602200 !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
97
97
|
- - ! '>='
|
@@ -99,10 +99,10 @@ dependencies:
|
|
99
99
|
version: '0'
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
|
-
version_requirements: *
|
102
|
+
version_requirements: *30602200
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: reek
|
105
|
-
requirement: &
|
105
|
+
requirement: &30601580 !ruby/object:Gem::Requirement
|
106
106
|
none: false
|
107
107
|
requirements:
|
108
108
|
- - ~>
|
@@ -110,10 +110,10 @@ dependencies:
|
|
110
110
|
version: 1.2.8
|
111
111
|
type: :development
|
112
112
|
prerelease: false
|
113
|
-
version_requirements: *
|
113
|
+
version_requirements: *30601580
|
114
114
|
- !ruby/object:Gem::Dependency
|
115
115
|
name: roodi
|
116
|
-
requirement: &
|
116
|
+
requirement: &30600920 !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
119
119
|
- - ~>
|
@@ -121,10 +121,10 @@ dependencies:
|
|
121
121
|
version: 2.1.0
|
122
122
|
type: :development
|
123
123
|
prerelease: false
|
124
|
-
version_requirements: *
|
124
|
+
version_requirements: *30600920
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: database_cleaner
|
127
|
-
requirement: &
|
127
|
+
requirement: &30600320 !ruby/object:Gem::Requirement
|
128
128
|
none: false
|
129
129
|
requirements:
|
130
130
|
- - ! '>='
|
@@ -132,7 +132,7 @@ dependencies:
|
|
132
132
|
version: '0'
|
133
133
|
type: :development
|
134
134
|
prerelease: false
|
135
|
-
version_requirements: *
|
135
|
+
version_requirements: *30600320
|
136
136
|
description: ! "In frustration of Mongoid::Versioning, I created this plugin for tracking
|
137
137
|
historical changes for any document, including embedded ones. It achieves this by
|
138
138
|
storing all history tracks in a single collection that you define. (See Usage for
|
@@ -162,6 +162,7 @@ files:
|
|
162
162
|
- VERSION
|
163
163
|
- lib/mongoid-history.rb
|
164
164
|
- lib/mongoid/history.rb
|
165
|
+
- lib/mongoid/history/sweeper.rb
|
165
166
|
- lib/mongoid/history/trackable.rb
|
166
167
|
- lib/mongoid/history/tracker.rb
|
167
168
|
- mongoid-history.gemspec
|
@@ -184,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
185
|
version: '0'
|
185
186
|
segments:
|
186
187
|
- 0
|
187
|
-
hash:
|
188
|
+
hash: 3716774476393405527
|
188
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
190
|
none: false
|
190
191
|
requirements:
|