mongoid-undo 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed489fc3104be499bffb28a4288760cdb72eb730
4
- data.tar.gz: f0e4fe0ce599a464d4323d4dde5b39cfdfa46708
3
+ metadata.gz: 7b8f85628210268bcf1b94ccb01bcf7b1192cef9
4
+ data.tar.gz: df8975a7f1030b9bda9528ee468eaa286c8068b8
5
5
  SHA512:
6
- metadata.gz: c2074f5ef14a2793e1a0a9c4af36fe0c736ac64e0e7761425b0b0cbda44e540db9464a00e76588b4d42d1221af2ea9f79886961afe603b2e3f0747a5cd4453a7
7
- data.tar.gz: 0575fc9e3b68fc138af9c9036f7887507b9ea663f13ee38d3a8051c8d948120a368fc900d66308fc87e4553a3a33f615484eb548b51dec091931f5b5c3798c9a
6
+ metadata.gz: d5b44e701abc7cf4ab90cd0d89c830fe2aba2b9eaabc7249e0cf24538459e4eae1e4f16bfac9169a410563a6dc5b96d67c2f425ed84a9ab69dc1d38d2930ba0d
7
+ data.tar.gz: 6423d0605e2b8841920d38d736c487083bcd16bb85fb74254405b47b57f9e7f8d2dfdd306a8a50feb9e81077a341cba49330d4b258c3b08238f7b4a87202100d
data/README.md CHANGED
@@ -51,7 +51,12 @@ document.persisted? #=> true
51
51
  ```ruby
52
52
  document = Document.create(name: 'foo')
53
53
 
54
+ document.undoable? # => false
55
+ document.save
56
+ document.undoable? # => false
57
+
54
58
  document.update_attributes(name: 'bar')
59
+ document.undoable? # => true
55
60
  document.name #=> 'bar'
56
61
 
57
62
  document.undo
data/lib/mongoid/undo.rb CHANGED
@@ -18,11 +18,17 @@ module Mongoid
18
18
 
19
19
  define_method name do
20
20
  collection.find(atomic_selector).update('$set' => { action: action })
21
+ version = self.instance_variable_get(:@version)
21
22
  reload
23
+ self.instance_variable_set :@version, version unless version.nil?
22
24
  end
23
25
  set_callback action, :after, name
24
26
  end
25
27
 
28
+ after_find do
29
+ @version = read_attribute(:version)
30
+ end
31
+
26
32
  define_model_callbacks :undo, :redo
27
33
  end
28
34
 
@@ -38,6 +44,16 @@ module Mongoid
38
44
  end
39
45
  alias_method :redo, :undo
40
46
 
47
+ def undoable?
48
+ case action
49
+ when :create, :destroy
50
+ true
51
+ when :update
52
+ read_attribute(:version).to_i > @version
53
+ end
54
+ end
55
+ alias_method :redoable?, :undoable?
56
+
41
57
  private
42
58
  def retrieve
43
59
  update_attributes(versions.last.versioned_attributes.except('version', 'updated_at'))
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Undo
3
- VERSION = '0.10.1'
3
+ VERSION = '0.11.0'
4
4
  end
5
5
  end
data/test/undo_test.rb CHANGED
@@ -7,19 +7,12 @@ class Document
7
7
  field :name, type: String
8
8
  end
9
9
 
10
- class Localized
11
- include Mongoid::Document
12
- include Mongoid::Undo
13
-
10
+ class Localized < Document
14
11
  field :language, localize: true, type: String
15
12
  end
16
13
 
17
- class Timestamped
18
- include Mongoid::Document
19
- include Mongoid::Undo
14
+ class Timestamped < Document
20
15
  include Mongoid::Timestamps
21
-
22
- field :name, type: String
23
16
  end
24
17
 
25
18
  class UndoTest < Minitest::Unit::TestCase
@@ -28,36 +21,46 @@ class UndoTest < Minitest::Unit::TestCase
28
21
  assert_equal :create, document.action
29
22
  assert_equal 1, document.version
30
23
  assert document.persisted?
24
+ assert document.undoable?
31
25
 
32
26
  document.undo
33
27
  assert_equal :create, document.action
34
28
  assert_equal 1, document.version
35
29
  assert_not document.persisted?
30
+ assert document.undoable?
36
31
 
37
32
  document.redo
38
33
  assert_equal :create, document.action
39
34
  assert_equal 1, document.version
40
35
  assert document.persisted?
36
+ assert document.undoable?
41
37
  end
42
38
 
43
39
 
44
40
  def test_update
45
41
  document = Document.create(name: 'foo')
46
42
 
43
+ document.save
44
+ assert_not document.undoable?
45
+
47
46
  document.update_attributes name: 'bar'
48
47
  assert_equal :update, document.action
49
48
  assert_equal 2, document.version
50
49
  assert_equal 'bar', document.name
50
+ assert document.undoable?
51
51
 
52
52
  document.undo
53
53
  assert_equal :update, document.action
54
54
  assert_equal 3, document.version
55
55
  assert_equal 'foo', document.name
56
+ assert document.undoable?
57
+
56
58
 
57
59
  document.redo
58
60
  assert_equal :update, document.action
59
61
  assert_equal 4, document.version
60
62
  assert_equal 'bar', document.name
63
+ assert document.undoable?
61
64
  end
62
65
 
63
66
 
@@ -67,14 +70,17 @@ class UndoTest < Minitest::Unit::TestCase
67
70
  document.destroy
68
71
  assert_equal :destroy, document.action
69
72
  assert_not document.persisted?
73
+ assert document.undoable?
70
74
 
71
75
  document.undo
72
76
  assert_equal :destroy, document.action
73
77
  assert document.persisted?
78
+ assert document.undoable?
74
79
 
75
80
  document.redo
76
81
  assert_equal :destroy, document.action
77
82
  assert_not document.persisted?
83
+ assert document.undoable?
78
84
  end
79
85
 
80
86
 
@@ -85,6 +91,13 @@ class UndoTest < Minitest::Unit::TestCase
85
91
  end
86
92
 
87
93
 
94
+ def test_redoable_equals_to_undoable
95
+ document = Document.create(name: 'foo')
96
+
97
+ assert_equal document.method(:undoable?), document.method(:redoable?)
98
+ end
99
+
100
+
88
101
  def test_localized_attributes
89
102
  document = Localized.create(language: 'English')
90
103
 
@@ -136,6 +149,22 @@ class UndoTest < Minitest::Unit::TestCase
136
149
  end
137
150
 
138
151
 
152
+ def test_around_callback
153
+ document = Document.create(name: 'foo')
154
+ document.update_attributes name: 'bar'
155
+
156
+ tap do |test|
157
+ document.class.around_undo do |document, proc|
158
+ test.assert_equal 'bar', document.name
159
+ proc.call
160
+ test.assert_equal 'foo', document.name
161
+ end
162
+ end
163
+
164
+ document.undo
165
+ end
166
+
167
+
139
168
  def test_disabling_undo_via_callbacks
140
169
  document = Document.create(name: 'foo')
141
170
  document.destroy
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-undo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Uher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-16 00:00:00.000000000 Z
11
+ date: 2014-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -81,9 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.2.0
84
+ rubygems_version: 2.2.2
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Super simple undo for your Mongoid app.
88
88
  test_files: []
89
- has_rdoc: