mongoid-history 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ 0.3.3 (1/4/2013)
2
+ ----------------
3
+
4
+ * [#42](https://github.com/aq1018/mongoid-history/issues/42) Fix: corrected creation of association chain when using nested embedded documents - [@matekb](https://github.com/matekb).
5
+ * [#56](https://github.com/aq1018/mongoid-history/issues/56) Fix: now possible to undo setting (creating) attributes that was previously unset - [@matekb](https://github.com/matekb).
6
+ * [#49](https://github.com/aq1018/mongoid-history/issues/49) Fix: now correctly undo/redo localized fields - [@edejong](https://github.com/edejong).
7
+
8
+
1
9
  0.3.2 (1/24/2013)
2
10
  -----------------
3
11
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -140,7 +140,7 @@ module Mongoid::History
140
140
  # we're assured, through the object creation, it'll exist. Whereas we're not guarenteed
141
141
  # the child to parent (embedded_in, belongs_to) relation will be defined
142
142
  if node._parent
143
- meta = _parent.relations.values.select do |relation|
143
+ meta = node._parent.relations.values.select do |relation|
144
144
  relation.class_name == node.class.to_s
145
145
  end.first
146
146
  end
@@ -52,7 +52,10 @@ module Mongoid::History
52
52
  undo_hash.easy_merge!(original)
53
53
  modifier_field = trackable.history_trackable_options[:modifier_field]
54
54
  undo_hash[modifier_field] = modifier
55
- undo_hash
55
+ (modified.keys - undo_hash.keys).each do |k|
56
+ undo_hash[k] = nil
57
+ end
58
+ localize_keys(undo_hash)
56
59
  end
57
60
 
58
61
  def redo_attr(modifier)
@@ -60,7 +63,7 @@ module Mongoid::History
60
63
  redo_hash.easy_merge!(modified)
61
64
  modifier_field = trackable.history_trackable_options[:modifier_field]
62
65
  redo_hash[modifier_field] = modifier
63
- redo_hash
66
+ localize_keys(redo_hash)
64
67
  end
65
68
 
66
69
  def trackable_root
@@ -97,7 +100,7 @@ private
97
100
 
98
101
  def create_standalone
99
102
  class_name = association_chain.first["name"]
100
- restored = class_name.constantize.new(modified)
103
+ restored = class_name.constantize.new(localize_keys(modified))
101
104
  restored.id = modified["_id"]
102
105
  restored.save!
103
106
  end
@@ -105,9 +108,9 @@ private
105
108
  def create_on_parent
106
109
  name = association_chain.last["name"]
107
110
  if embeds_one?(trackable_parent, name)
108
- trackable_parent.send("create_#{name}!", modified)
111
+ trackable_parent.send("create_#{name}!", localize_keys(modified))
109
112
  elsif embeds_many?(trackable_parent, name)
110
- trackable_parent.send(name).create!(modified)
113
+ trackable_parent.send(name).create!(localize_keys(modified))
111
114
  else
112
115
  raise "This should never happen. Please report bug!"
113
116
  end
@@ -155,5 +158,13 @@ private
155
158
  documents
156
159
  end
157
160
 
161
+ def localize_keys(hash)
162
+ klass = association_chain.first["name"].constantize
163
+ klass.localized_fields.keys.each do |name|
164
+ hash["#{name}_translations"] = hash.delete(name) if hash[name].present?
165
+ end if klass.respond_to?(:localized_fields)
166
+ hash
167
+ end
168
+
158
169
  end
159
170
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mongoid-history"
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
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 = "2013-01-24"
12
+ s.date = "2013-04-01"
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 = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "mongoid-history.gemspec",
36
36
  "spec/integration/integration_spec.rb",
37
37
  "spec/integration/multi_relation_spec.rb",
38
+ "spec/integration/nested_embedded_documents_spec.rb",
38
39
  "spec/spec_helper.rb",
39
40
  "spec/support/database_cleaner.rb",
40
41
  "spec/support/mongoid.rb",
@@ -45,7 +46,7 @@ Gem::Specification.new do |s|
45
46
  s.homepage = "http://github.com/aq1018/mongoid-history"
46
47
  s.licenses = ["MIT"]
47
48
  s.require_paths = ["lib"]
48
- s.rubygems_version = "1.8.24"
49
+ s.rubygems_version = "1.8.25"
49
50
  s.summary = "history tracking, auditing, undo, redo for mongoid"
50
51
 
51
52
  if s.respond_to? :specification_version then
@@ -199,6 +199,15 @@ describe Mongoid::History do
199
199
  @user.reload.name.should == name
200
200
  end
201
201
 
202
+ it "should undo non-existing field changes" do
203
+ post = Post.create(:modifier => @user, :views => 100)
204
+ post.reload.title.should == nil
205
+ post.update_attributes(:title => "Aaron2")
206
+ post.reload.title.should == "Aaron2"
207
+ post.history_tracks.first.undo! nil
208
+ post.reload.title.should == nil
209
+ end
210
+
202
211
  it "should track array changes" do
203
212
  aliases = @user.aliases
204
213
  @user.update_attributes(:aliases => [ 'bob', 'joe' ])
@@ -536,6 +545,7 @@ describe Mongoid::History do
536
545
  @comment.undo! @user, :last => 2
537
546
  @comment.title.should == "Test2"
538
547
  end
548
+
539
549
  end
540
550
 
541
551
  describe "redo" do
@@ -565,5 +575,36 @@ describe Mongoid::History do
565
575
 
566
576
  end
567
577
  end
578
+
579
+ describe "localized fields" do
580
+ before :each do
581
+ class Sausage
582
+ include Mongoid::Document
583
+ include Mongoid::History::Trackable
584
+
585
+ field :flavour, localize: true
586
+ track_history :on => [:flavour], :track_destroy => true
587
+ end
588
+ end
589
+ it "should correctly undo and redo" do
590
+ if Sausage.respond_to?(:localized_fields)
591
+ sausage = Sausage.create(flavour_translations: { 'en' => "Apple", 'nl' => 'Appel' } )
592
+ sausage.update_attributes(:flavour => "Guinness")
593
+
594
+ track = sausage.history_tracks.last
595
+
596
+ track.undo! @user
597
+ sausage.reload.flavour.should == "Apple"
598
+
599
+ track.redo! @user
600
+ sausage.reload.flavour.should == "Guinness"
601
+
602
+ sausage.destroy
603
+ sausage.history_tracks.last.action.should == "destroy"
604
+ sausage.history_tracks.last.undo! @user
605
+ sausage.reload.flavour.should == "Guinness"
606
+ end
607
+ end
608
+ end
568
609
  end
569
610
  end
@@ -0,0 +1,75 @@
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
+ embeds_many :embones
12
+
13
+ track_history :on => :all, # 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
+ :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
16
+ :track_create => false, # track document creation, default is false
17
+ :track_update => true, # track document updates, default is true
18
+ :track_destroy => false # track document destruction, default is false
19
+ end
20
+
21
+ class Embone
22
+ include Mongoid::Document
23
+ include Mongoid::History::Trackable
24
+
25
+ field :name
26
+ embeds_many :embtwos
27
+ embedded_in :model
28
+
29
+ track_history :on => :all, # track title and body fields only, default is :all
30
+ :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
31
+ :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
32
+ :track_create => false, # track document creation, default is false
33
+ :track_update => true, # track document updates, default is true
34
+ :track_destroy => false, # track document destruction, default is false
35
+ :scope => :model
36
+ end
37
+
38
+ class Embtwo
39
+ include Mongoid::Document
40
+ include Mongoid::History::Trackable
41
+
42
+ field :name
43
+ embedded_in :embone
44
+
45
+ track_history :on => :all, # track title and body fields only, default is :all
46
+ :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
47
+ :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
48
+ :track_create => false, # track document creation, default is false
49
+ :track_update => true, # track document updates, default is true
50
+ :track_destroy => false, # track document destruction, default is false
51
+ :scope => :model
52
+ end
53
+
54
+ class User
55
+ include Mongoid::Document
56
+ has_many :models, :dependent => :destroy, inverse_of: :user
57
+ end
58
+ end
59
+
60
+ it "should be able to track history for nested embedded documents" do
61
+ user = User.new
62
+ user.save
63
+
64
+ model = Model.new({name: "m1name"})
65
+ model.user = user
66
+ model.save!
67
+ embedded1 = model.embones.create({name: "e1name"})
68
+ embedded2 = embedded1.embtwos.create({name: "e2name"})
69
+
70
+ embedded2.name = "a new name"
71
+ embedded2.save
72
+
73
+ model.history_tracks.first.undo! user
74
+ end
75
+ 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.2
4
+ version: 0.3.3
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: 2013-01-24 00:00:00.000000000 Z
13
+ date: 2013-04-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: easy_diff
@@ -97,6 +97,7 @@ files:
97
97
  - mongoid-history.gemspec
98
98
  - spec/integration/integration_spec.rb
99
99
  - spec/integration/multi_relation_spec.rb
100
+ - spec/integration/nested_embedded_documents_spec.rb
100
101
  - spec/spec_helper.rb
101
102
  - spec/support/database_cleaner.rb
102
103
  - spec/support/mongoid.rb
@@ -118,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  segments:
120
121
  - 0
121
- hash: -3042283399996028740
122
+ hash: 1291370256221815219
122
123
  required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  none: false
124
125
  requirements:
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  requirements: []
129
130
  rubyforge_project:
130
- rubygems_version: 1.8.24
131
+ rubygems_version: 1.8.25
131
132
  signing_key:
132
133
  specification_version: 3
133
134
  summary: history tracking, auditing, undo, redo for mongoid