mongoid-history 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,7 +1,13 @@
1
- 0.3.1
2
- -----
1
+ 0.3.2 (1/24/2013)
2
+ -----------------
3
+
4
+ * [#54](https://github.com/aq1018/mongoid-history/pull/54) Use an index instead of the `$elemMatch` selector in `history_tracks` - [@vecio](https://github.com/vecio).
5
+ * [#11](https://github.com/aq1018/mongoid-history/issues/11) Added `:modifier_field_inverse_of` on `track_history` that defines the `:inverse_of` relationship of the modifier class - [@matekb](https://github.com/matekb), [@dblock](https://github.com/dblock).
6
+
7
+ 0.3.1 (11/16/2012)
8
+ ------------------
3
9
 
4
- * [#45](https://github.com/aq1018/mongoid-history/pull/45) Fix: intermittent hash ordering issue with history_tracks - [@getaroom](https://github.com/getaroom).
10
+ * [#45](https://github.com/aq1018/mongoid-history/pull/45) Fix: intermittent hash ordering issue with `history_tracks` - [@getaroom](https://github.com/getaroom).
5
11
  * [#50](https://github.com/aq1018/mongoid-history/pull/50) Fix: tracking of array changes, undo and redo of field changes on non-embedded objects - [@dblock](https://github.com/dblock).
6
12
 
7
13
  0.3.0 (8/21/2012)
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "easy_diff"
4
- gem "mongoid", "~> 3.0.4"
4
+ gem "mongoid", "~> 3.0"
5
+ gem "activesupport"
5
6
 
6
7
  group :test do
7
8
  gem "rspec", "~> 2.11.0"
data/README.md CHANGED
@@ -89,7 +89,8 @@ class Post
89
89
 
90
90
  # telling Mongoid::History how you want to track changes
91
91
  track_history :on => [:title, :body], # track title and body fields only, default is :all
92
- :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
92
+ :modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier
93
+ :modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set
93
94
  :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
94
95
  :track_create => false, # track document creation, default is false
95
96
  :track_update => true, # track document updates, default is true
@@ -179,5 +180,6 @@ Copyright
179
180
  ---------
180
181
 
181
182
  Copyright (c) 2011-2012 Aaron Qian. MIT License.
183
+
182
184
  See [LICENSE.txt](https://github.com/aq1018/mongoid-history/blob/master/LICENSE.txt) for further details.
183
185
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -34,7 +34,10 @@ module Mongoid::History
34
34
  end
35
35
 
36
36
  field options[:version_field].to_sym, :type => Integer
37
- belongs_to options[:modifier_field].to_sym, :class_name => Mongoid::History.modifier_class_name
37
+
38
+ belongs_to_modifier_options = { :class_name => Mongoid::History.modifier_class_name }
39
+ belongs_to_modifier_options[:inverse_of] = options[:modifier_field_inverse_of] if options.has_key?(:modifier_field_inverse_of)
40
+ belongs_to options[:modifier_field].to_sym, belongs_to_modifier_options
38
41
 
39
42
  include MyInstanceMethods
40
43
  extend SingletonMethods
@@ -71,7 +74,7 @@ module Mongoid::History
71
74
 
72
75
  module MyInstanceMethods
73
76
  def history_tracks
74
- @history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => { "$elemMatch" => association_hash })
77
+ @history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => association_hash)
75
78
  end
76
79
 
77
80
  # undo :from => 1, :to => 5
@@ -82,7 +85,6 @@ module Mongoid::History
82
85
  versions.sort!{|v1, v2| v2.version <=> v1.version}
83
86
 
84
87
  versions.each do |v|
85
- undo_attr = v.undo_attr(modifier)
86
88
  self.attributes = v.undo_attr(modifier)
87
89
  end
88
90
  save!
@@ -146,7 +148,7 @@ module Mongoid::History
146
148
  # if root node has no meta, and should use class name instead
147
149
  name = meta ? meta.key.to_s : node.class.name
148
150
 
149
- { 'name' => name, 'id' => node.id}
151
+ ActiveSupport::OrderedHash['name', name, 'id', node.id]
150
152
  end
151
153
 
152
154
  def modified_attributes_for_update
@@ -13,7 +13,10 @@ module Mongoid::History
13
13
  field :version, :type => Integer
14
14
  field :action, :type => String
15
15
  field :scope, :type => String
16
- belongs_to :modifier, :class_name => Mongoid::History.modifier_class_name
16
+ belongs_to :modifier, :class_name => Mongoid::History.modifier_class_name
17
+
18
+ index(:scope => 1)
19
+ index(:association_chain => 1)
17
20
 
18
21
  Mongoid::History.tracker_class_name = self.name.tableize.singularize.to_sym
19
22
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mongoid-history"
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
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", "Justin Grimes"]
12
- s.date = "2012-11-16"
12
+ s.date = "2013-01-24"
13
13
  s.description = "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.\n\n 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."
14
14
  s.email = ["aq1018@gmail.com", "justin.mgrimes@gmail.com"]
15
15
  s.extra_rdoc_files = [
@@ -34,9 +34,11 @@ Gem::Specification.new do |s|
34
34
  "lib/mongoid/history/tracker.rb",
35
35
  "mongoid-history.gemspec",
36
36
  "spec/integration/integration_spec.rb",
37
+ "spec/integration/multi_relation_spec.rb",
37
38
  "spec/spec_helper.rb",
38
39
  "spec/support/database_cleaner.rb",
39
40
  "spec/support/mongoid.rb",
41
+ "spec/support/mongoid_history.rb",
40
42
  "spec/trackable_spec.rb",
41
43
  "spec/tracker_spec.rb"
42
44
  ]
@@ -51,14 +53,17 @@ Gem::Specification.new do |s|
51
53
 
52
54
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
55
  s.add_runtime_dependency(%q<easy_diff>, [">= 0"])
54
- s.add_runtime_dependency(%q<mongoid>, ["~> 3.0.4"])
56
+ s.add_runtime_dependency(%q<mongoid>, ["~> 3.0"])
57
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
55
58
  else
56
59
  s.add_dependency(%q<easy_diff>, [">= 0"])
57
- s.add_dependency(%q<mongoid>, ["~> 3.0.4"])
60
+ s.add_dependency(%q<mongoid>, ["~> 3.0"])
61
+ s.add_dependency(%q<activesupport>, [">= 0"])
58
62
  end
59
63
  else
60
64
  s.add_dependency(%q<easy_diff>, [">= 0"])
61
- s.add_dependency(%q<mongoid>, ["~> 3.0.4"])
65
+ s.add_dependency(%q<mongoid>, ["~> 3.0"])
66
+ s.add_dependency(%q<activesupport>, [">= 0"])
62
67
  end
63
68
  end
64
69
 
@@ -1,11 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History do
4
- before :all do
5
- class HistoryTracker
6
- include Mongoid::History::Tracker
7
- end
8
-
4
+ before :each do
9
5
  class Post
10
6
  include Mongoid::Document
11
7
  include Mongoid::Timestamps
@@ -66,9 +62,7 @@ describe Mongoid::History do
66
62
  field :title
67
63
  track_history :on => [:title], :scope => :post, :track_create => true, :track_destroy => true, :modifier_field => :updated_by
68
64
  end
69
- end
70
65
 
71
- before :each do
72
66
  @user = User.create(:name => "Aaron", :email => "aaron@randomemail.com", :aliases => [ 'bob' ])
73
67
  @another_user = User.create(:name => "Another Guy", :email => "anotherguy@randomemail.com")
74
68
  @post = Post.create(:title => "Test", :body => "Post", :modifier => @user, :views => 100)
@@ -118,7 +112,7 @@ describe Mongoid::History do
118
112
  it "should have two history track records in post" do
119
113
  lambda {
120
114
  @post.destroy
121
- }.should change(HistoryTracker, :count).by(1)
115
+ }.should change(Tracker, :count).by(1)
122
116
  end
123
117
 
124
118
  it "should assign destroy on track record" do
@@ -136,13 +130,13 @@ describe Mongoid::History do
136
130
  it "should create a history track if changed attributes match tracked attributes" do
137
131
  lambda {
138
132
  @post.update_attributes(:title => "Another Test")
139
- }.should change(HistoryTracker, :count).by(1)
133
+ }.should change(Tracker, :count).by(1)
140
134
  end
141
135
 
142
136
  it "should not create a history track if changed attributes do not match tracked attributes" do
143
137
  lambda {
144
138
  @post.update_attributes(:rating => "untracked")
145
- }.should change(HistoryTracker, :count).by(0)
139
+ }.should change(Tracker, :count).by(0)
146
140
  end
147
141
 
148
142
  it "should assign modified fields" do
@@ -232,7 +226,7 @@ describe Mongoid::History do
232
226
  lambda {
233
227
  @post.update_attributes(:title => "Test2")
234
228
  @post.update_attributes(:title => "Test3")
235
- }.should change(HistoryTracker, :count).by(2)
229
+ }.should change(Tracker, :count).by(2)
236
230
  end
237
231
 
238
232
  it "should create a history track of version 2" do
@@ -394,10 +388,6 @@ describe Mongoid::History do
394
388
  @tag_bar = @post.tags.create(:title => "bar")
395
389
  end
396
390
 
397
- after(:each) do
398
- Thread.current[:mongoid_history_sweeper_controller] = nil
399
- end
400
-
401
391
  it "should have cascaded the creation callbacks and set timestamps" do
402
392
  @tag_foo.created_at.should_not be_nil
403
393
  @tag_foo.updated_at.should_not be_nil
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::History::Tracker do
4
+ before :each do
5
+ class Model
6
+ include Mongoid::Document
7
+ include Mongoid::History::Trackable
8
+
9
+ field :name, type: String
10
+ belongs_to :user, inverse_of: :models
11
+ has_and_belongs_to_many :external_users, class_name: "User", inverse_of: :external_models
12
+
13
+ track_history :on => :name, # track title and body fields only, default is :all
14
+ :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
15
+ :modifier_field_inverse_of => nil, # no inverse modifier relationship
16
+ :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
17
+ :track_create => false, # track document creation, default is false
18
+ :track_update => true, # track document updates, default is true
19
+ :track_destroy => false # track document destruction, default is false
20
+ end
21
+
22
+ class User
23
+ include Mongoid::Document
24
+ has_many :models, :dependent => :destroy, inverse_of: :user
25
+ has_and_belongs_to_many :external_model, class_name: "Model", inverse_of: :external_users
26
+ end
27
+ end
28
+
29
+ it "should be possible to undo when having multiple relations to modifier class" do
30
+ user = User.new
31
+ user.save
32
+
33
+ model = Model.new
34
+ model.name = "Foo"
35
+ model.user = user
36
+ model.save!
37
+
38
+ model.name = "Bar"
39
+ model.save!
40
+
41
+ model.undo! user
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ RSpec.configure do |config|
2
+ config.before :each do
3
+ class Tracker
4
+ include Mongoid::History::Tracker
5
+ end
6
+ Mongoid::History.tracker_class_name = "Tracker"
7
+ Mongoid::History.modifier_class_name = "User"
8
+ end
9
+ config.after :each do
10
+ Mongoid::History.tracker_class_name = nil
11
+ Mongoid::History.trackable_class_options = nil
12
+ Thread.current[:mongoid_history_sweeper_controller] = nil
13
+ end
14
+ end
15
+
@@ -8,10 +8,6 @@ describe Mongoid::History::Trackable do
8
8
  end
9
9
  end
10
10
 
11
- after :each do
12
- Mongoid::History.trackable_class_options = nil
13
- end
14
-
15
11
  it "should have #track_history" do
16
12
  MyModel.should respond_to :track_history
17
13
  end
@@ -42,10 +38,6 @@ describe Mongoid::History::Trackable do
42
38
  }
43
39
  end
44
40
 
45
- after :each do
46
- Mongoid::History.trackable_class_options = nil
47
- end
48
-
49
41
  it "should have default options" do
50
42
  Mongoid::History.trackable_class_options[:my_model].should == @expected_option
51
43
  end
data/spec/tracker_spec.rb CHANGED
@@ -1,17 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Mongoid::History::Tracker do
4
- before :each do
4
+ it "should set tracker_class_name when included" do
5
5
  class MyTracker
6
6
  include Mongoid::History::Tracker
7
7
  end
8
- end
9
-
10
- after :each do
11
- Mongoid::History.tracker_class_name = nil
12
- end
13
-
14
- it "should set tracker_class_name when included" do
15
8
  Mongoid::History.tracker_class_name.should == :my_tracker
16
9
  end
17
10
  end
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.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-16 00:00:00.000000000 Z
13
+ date: 2013-01-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: easy_diff
@@ -35,7 +35,7 @@ dependencies:
35
35
  requirements:
36
36
  - - ~>
37
37
  - !ruby/object:Gem::Version
38
- version: 3.0.4
38
+ version: '3.0'
39
39
  type: :runtime
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,23 @@ dependencies:
43
43
  requirements:
44
44
  - - ~>
45
45
  - !ruby/object:Gem::Version
46
- version: 3.0.4
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  description: ! "In frustration of Mongoid::Versioning, I created this plugin for tracking
48
64
  historical changes for any document, including embedded ones. It achieves this by
49
65
  storing all history tracks in a single collection that you define. (See Usage for
@@ -80,9 +96,11 @@ files:
80
96
  - lib/mongoid/history/tracker.rb
81
97
  - mongoid-history.gemspec
82
98
  - spec/integration/integration_spec.rb
99
+ - spec/integration/multi_relation_spec.rb
83
100
  - spec/spec_helper.rb
84
101
  - spec/support/database_cleaner.rb
85
102
  - spec/support/mongoid.rb
103
+ - spec/support/mongoid_history.rb
86
104
  - spec/trackable_spec.rb
87
105
  - spec/tracker_spec.rb
88
106
  homepage: http://github.com/aq1018/mongoid-history
@@ -100,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
118
  version: '0'
101
119
  segments:
102
120
  - 0
103
- hash: -3144305441896243530
121
+ hash: -3042283399996028740
104
122
  required_rubygems_version: !ruby/object:Gem::Requirement
105
123
  none: false
106
124
  requirements: