mongoid-history 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/mongoid/history/trackable.rb +31 -30
- data/mongoid-history.gemspec +5 -5
- data/spec/integration/integration_spec.rb +23 -12
- metadata +6 -5
data/Rakefile
CHANGED
@@ -19,8 +19,8 @@ Jeweler::Tasks.new do |gem|
|
|
19
19
|
gem.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.
|
20
20
|
|
21
21
|
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.}
|
22
|
-
gem.email = "
|
23
|
-
gem.authors = ["Aaron Qian"]
|
22
|
+
gem.email = "aq1018@gmail.com"
|
23
|
+
gem.authors = ["Aaron Qian", "Justin Grimes"]
|
24
24
|
end
|
25
25
|
Jeweler::RubygemsDotOrgTasks.new
|
26
26
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.8
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Mongoid::History
|
2
2
|
module Trackable
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
4
|
+
|
5
5
|
module ClassMethods
|
6
6
|
def track_history(options={})
|
7
7
|
model_name = self.name.tableize.singularize.to_sym
|
@@ -15,7 +15,7 @@ module Mongoid::History
|
|
15
15
|
}
|
16
16
|
|
17
17
|
options = default_options.merge(options)
|
18
|
-
|
18
|
+
|
19
19
|
# normalize except fields
|
20
20
|
# manually ensure _id, id, version will not be tracked in history
|
21
21
|
options[:except] = [options[:except]] unless options[:except].is_a? Array
|
@@ -24,48 +24,48 @@ module Mongoid::History
|
|
24
24
|
options[:except] += [:_id, :id]
|
25
25
|
options[:except] = options[:except].map(&:to_s).flatten.compact.uniq
|
26
26
|
options[:except].map(&:to_s)
|
27
|
-
|
27
|
+
|
28
28
|
# normalize fields to track to either :all or an array of strings
|
29
29
|
if options[:on] != :all
|
30
30
|
options[:on] = [options[:on]] unless options[:on].is_a? Array
|
31
31
|
options[:on] = options[:on].map(&:to_s).flatten.uniq
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
field options[:version_field].to_sym, :type => Integer
|
35
35
|
referenced_in options[:modifier_field].to_sym, :class_name => Mongoid::History.modifer_class_name
|
36
|
-
|
36
|
+
|
37
37
|
include InstanceMethods
|
38
38
|
extend SingletonMethods
|
39
|
-
|
39
|
+
|
40
40
|
delegate :history_trackable_options, :to => 'self.class'
|
41
41
|
delegate :track_history?, :to => 'self.class'
|
42
42
|
|
43
43
|
before_update :track_update
|
44
44
|
before_create :track_create if options[:track_create]
|
45
|
-
|
45
|
+
|
46
46
|
Mongoid::History.trackable_classes ||= []
|
47
47
|
Mongoid::History.trackable_classes << self
|
48
48
|
Mongoid::History.trackable_class_options ||= {}
|
49
49
|
Mongoid::History.trackable_class_options[model_name] = options
|
50
50
|
Thread.current[:mongoid_history_trackable_enabled] = true
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
def track_history?
|
54
54
|
!!Thread.current[:mongoid_history_trackable_enabled]
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def disable_tracking(&block)
|
58
58
|
Thread.current[:mongoid_history_trackable_enabled] = false
|
59
59
|
yield
|
60
60
|
Thread.current[:mongoid_history_trackable_enabled] = true
|
61
61
|
end
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
module InstanceMethods
|
65
65
|
def history_tracks
|
66
66
|
@history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => triverse_association_chain)
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
# undo :from => 1, :to => 5
|
70
70
|
# undo 4
|
71
71
|
# undo :last => 10
|
@@ -79,7 +79,7 @@ module Mongoid::History
|
|
79
79
|
end
|
80
80
|
save!
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
def redo!(modifier, options_or_version=nil)
|
84
84
|
versions = get_versions_criteria(options_or_version).to_a
|
85
85
|
versions.sort!{|v1, v2| v1.version <=> v2.version}
|
@@ -90,7 +90,7 @@ module Mongoid::History
|
|
90
90
|
end
|
91
91
|
save!
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
private
|
95
95
|
def get_versions_criteria(options_or_version)
|
96
96
|
if options_or_version.is_a? Hash
|
@@ -112,29 +112,30 @@ module Mongoid::History
|
|
112
112
|
end
|
113
113
|
versions.desc(:version)
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
def should_track_update?
|
117
117
|
track_history? && !modified_attributes_for_update.blank?
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def triverse_association_chain(node=self)
|
121
121
|
list = node._parent ? triverse_association_chain(node._parent) : []
|
122
122
|
list << { 'name' => node.class.name, 'id' => node.id }
|
123
123
|
list
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
def modified_attributes_for_update
|
127
127
|
@modified_attributes_for_update ||= if history_trackable_options[:on] == :all
|
128
|
-
changes
|
128
|
+
changes.reject do |k, v|
|
129
|
+
history_trackable_options[:except].include?(k)
|
130
|
+
end
|
129
131
|
else
|
130
132
|
changes.reject do |k, v|
|
131
133
|
!history_trackable_options[:on].include?(k)
|
132
|
-
end.reject do |k, v|
|
133
|
-
history_trackable_options[:except].include?(k)
|
134
134
|
end
|
135
|
+
|
135
136
|
end
|
136
137
|
end
|
137
|
-
|
138
|
+
|
138
139
|
def modified_attributes_for_create
|
139
140
|
@modified_attributes_for_create ||= attributes.inject({}) do |h, pair|
|
140
141
|
k,v = pair
|
@@ -147,19 +148,19 @@ module Mongoid::History
|
|
147
148
|
|
148
149
|
def history_tracker_attributes
|
149
150
|
return @history_tracker_attributes if @history_tracker_attributes
|
150
|
-
|
151
|
+
|
151
152
|
@history_tracker_attributes = {
|
152
153
|
:association_chain => triverse_association_chain,
|
153
154
|
:scope => history_trackable_options[:scope],
|
154
155
|
:modifier => send(history_trackable_options[:modifier_field])
|
155
156
|
}
|
156
|
-
|
157
|
+
|
157
158
|
original, modified = transform_changes((new_record? ? modified_attributes_for_create : modified_attributes_for_update))
|
158
159
|
@history_tracker_attributes[:original] = original
|
159
160
|
@history_tracker_attributes[:modified] = modified
|
160
161
|
@history_tracker_attributes
|
161
162
|
end
|
162
|
-
|
163
|
+
|
163
164
|
def track_update
|
164
165
|
return unless should_track_update?
|
165
166
|
current_version = (self.send(history_trackable_options[:version_field]) || 0 ) + 1
|
@@ -167,7 +168,7 @@ module Mongoid::History
|
|
167
168
|
Mongoid::History.tracker_class.create!(history_tracker_attributes.merge(:version => current_version))
|
168
169
|
clear_memoization
|
169
170
|
end
|
170
|
-
|
171
|
+
|
171
172
|
def track_create
|
172
173
|
return unless track_history?
|
173
174
|
current_version = (self.send(history_trackable_options[:version_field]) || 0 ) + 1
|
@@ -175,14 +176,14 @@ module Mongoid::History
|
|
175
176
|
Mongoid::History.tracker_class.create!(history_tracker_attributes.merge(:version => current_version))
|
176
177
|
clear_memoization
|
177
178
|
end
|
178
|
-
|
179
|
+
|
179
180
|
def clear_memoization
|
180
181
|
@history_tracker_attributes = nil
|
181
182
|
@modified_attributes_for_create = nil
|
182
183
|
@modified_attributes_for_update = nil
|
183
184
|
@history_tracks = nil
|
184
185
|
end
|
185
|
-
|
186
|
+
|
186
187
|
def transform_changes(changes)
|
187
188
|
original = {}
|
188
189
|
modified = {}
|
@@ -191,17 +192,17 @@ module Mongoid::History
|
|
191
192
|
original[k] = o if o
|
192
193
|
modified[k] = m if m
|
193
194
|
end
|
194
|
-
|
195
|
+
|
195
196
|
return original.easy_diff modified
|
196
197
|
end
|
197
|
-
|
198
|
+
|
198
199
|
end
|
199
|
-
|
200
|
+
|
200
201
|
module SingletonMethods
|
201
202
|
def history_trackable_options
|
202
203
|
@history_trackable_options ||= Mongoid::History.trackable_class_options[self.name.tableize.singularize.to_sym]
|
203
204
|
end
|
204
205
|
end
|
205
|
-
|
206
|
+
|
206
207
|
end
|
207
208
|
end
|
data/mongoid-history.gemspec
CHANGED
@@ -5,15 +5,15 @@
|
|
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.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Aaron Qian"]
|
12
|
-
s.date = %q{2011-
|
11
|
+
s.authors = ["Aaron Qian", "Justin Grimes"]
|
12
|
+
s.date = %q{2011-04-02}
|
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.}
|
16
|
-
s.email = %q{
|
16
|
+
s.email = %q{aq1018@gmail.com}
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE.txt",
|
19
19
|
"README.rdoc"
|
@@ -40,7 +40,7 @@ Gem::Specification.new do |s|
|
|
40
40
|
s.homepage = %q{http://github.com/aq1018/mongoid-history}
|
41
41
|
s.licenses = ["MIT"]
|
42
42
|
s.require_paths = ["lib"]
|
43
|
-
s.rubygems_version = %q{1.
|
43
|
+
s.rubygems_version = %q{1.5.2}
|
44
44
|
s.summary = %q{history tracking, auditing, undo, redo for mongoid}
|
45
45
|
s.test_files = [
|
46
46
|
"spec/integration/integration_spec.rb",
|
@@ -14,6 +14,7 @@ describe Mongoid::History do
|
|
14
14
|
field :title
|
15
15
|
field :body
|
16
16
|
field :rating
|
17
|
+
|
17
18
|
embeds_many :comments
|
18
19
|
track_history :on => [:title, :body]
|
19
20
|
end
|
@@ -32,15 +33,18 @@ describe Mongoid::History do
|
|
32
33
|
class User
|
33
34
|
include Mongoid::Document
|
34
35
|
include Mongoid::Timestamps
|
36
|
+
include Mongoid::History::Trackable
|
35
37
|
|
38
|
+
field :email
|
36
39
|
field :name
|
40
|
+
track_history :except => [:email]
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
40
44
|
before :each do
|
41
|
-
@user = User.create(:name => "Aaron")
|
42
|
-
@another_user = User.create(:name => "Another Guy")
|
43
|
-
@post = Post.create(:title => "Test", :body => "Post", :modifier => @user)
|
45
|
+
@user = User.create(:name => "Aaron", :email => "aaron@randomemail.com")
|
46
|
+
@another_user = User.create(:name => "Another Guy", :email => "anotherguy@randomemail.com")
|
47
|
+
@post = Post.create(:title => "Test", :body => "Post", :modifier => @user, :views => 100)
|
44
48
|
@comment = @post.comments.create(:title => "test", :body => "comment", :modifier => @user)
|
45
49
|
end
|
46
50
|
|
@@ -63,15 +67,15 @@ describe Mongoid::History do
|
|
63
67
|
@comment.history_tracks.first.modifier.should == @user
|
64
68
|
end
|
65
69
|
|
66
|
-
it "should
|
70
|
+
it "should assign version" do
|
67
71
|
@comment.history_tracks.first.version.should == 1
|
68
72
|
end
|
69
73
|
|
70
|
-
it "should
|
74
|
+
it "should assign scope" do
|
71
75
|
@comment.history_tracks.first.scope == "Post"
|
72
76
|
end
|
73
77
|
|
74
|
-
it "should
|
78
|
+
it "should assign association_chain" do
|
75
79
|
@comment.history_tracks.first.association_chain = [{:id => @post.id, :name => "Post"}, {:id => @comment.id, :name => "Comment"}]
|
76
80
|
end
|
77
81
|
end
|
@@ -108,29 +112,36 @@ describe Mongoid::History do
|
|
108
112
|
@post.history_tracks.first.modifier.should == @user
|
109
113
|
end
|
110
114
|
|
111
|
-
it "should
|
115
|
+
it "should assign version on history tracks" do
|
112
116
|
@post.update_attributes(:title => "Another Test")
|
113
117
|
@post.history_tracks.first.version.should == 1
|
114
118
|
end
|
115
119
|
|
116
|
-
it "should
|
120
|
+
it "should assign version on post" do
|
117
121
|
@post.update_attributes(:title => "Another Test")
|
118
122
|
@post.version.should == 1
|
119
123
|
end
|
120
124
|
|
121
|
-
it "should
|
125
|
+
it "should assign scope" do
|
122
126
|
@post.update_attributes(:title => "Another Test")
|
123
127
|
@post.history_tracks.first.scope == "Post"
|
124
128
|
end
|
125
129
|
|
126
|
-
it "should
|
130
|
+
it "should assign association_chain" do
|
127
131
|
@post.update_attributes(:title => "Another Test")
|
128
132
|
@post.history_tracks.first.association_chain = [{:id => @post.id, :name => "Post"}]
|
129
133
|
end
|
134
|
+
|
135
|
+
it "should exclude defined options" do
|
136
|
+
@user.update_attributes(:name => "Aaron2", :email => "aaronsnewemail@randomemail.com")
|
137
|
+
@user.history_tracks.first.modified.should == {
|
138
|
+
"name" => "Aaron2"
|
139
|
+
}
|
140
|
+
end
|
130
141
|
end
|
131
142
|
|
132
143
|
describe "on update non-embedded twice" do
|
133
|
-
it "should
|
144
|
+
it "should assign version on post" do
|
134
145
|
@post.update_attributes(:title => "Test2")
|
135
146
|
@post.update_attributes(:title => "Test3")
|
136
147
|
@post.version.should == 2
|
@@ -173,7 +184,7 @@ describe Mongoid::History do
|
|
173
184
|
end
|
174
185
|
|
175
186
|
describe "on update embedded" do
|
176
|
-
it "should
|
187
|
+
it "should assign version on comment" do
|
177
188
|
@comment.update_attributes(:title => "Test2")
|
178
189
|
@comment.version.should == 2 # first track generated on creation
|
179
190
|
end
|
metadata
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
name: mongoid-history
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.8
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Aaron Qian
|
9
|
+
- Justin Grimes
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
13
|
|
13
|
-
date: 2011-
|
14
|
+
date: 2011-04-02 00:00:00 -07:00
|
14
15
|
default_executable:
|
15
16
|
dependencies:
|
16
17
|
- !ruby/object:Gem::Dependency
|
@@ -127,7 +128,7 @@ description: |-
|
|
127
128
|
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.
|
128
129
|
|
129
130
|
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.
|
130
|
-
email:
|
131
|
+
email: aq1018@gmail.com
|
131
132
|
executables: []
|
132
133
|
|
133
134
|
extensions: []
|
@@ -167,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
168
|
requirements:
|
168
169
|
- - ">="
|
169
170
|
- !ruby/object:Gem::Version
|
170
|
-
hash:
|
171
|
+
hash: 143233499188101224
|
171
172
|
segments:
|
172
173
|
- 0
|
173
174
|
version: "0"
|
@@ -180,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
181
|
requirements: []
|
181
182
|
|
182
183
|
rubyforge_project:
|
183
|
-
rubygems_version: 1.
|
184
|
+
rubygems_version: 1.5.2
|
184
185
|
signing_key:
|
185
186
|
specification_version: 3
|
186
187
|
summary: history tracking, auditing, undo, redo for mongoid
|