mongoid-undo 0.9.1 → 0.10.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: 0adf9adc40a0c51746f7c46da47e0208ca8f2652
4
- data.tar.gz: 7b53acaffc646ac028274fb2f256f230ca79460a
3
+ metadata.gz: 733b0d209220876c801f04e9137e83ced99c4fae
4
+ data.tar.gz: 19a73d8202018bb7d3e61bc962aee9d700aaa8d9
5
5
  SHA512:
6
- metadata.gz: 3f8eb1f55953b9be2469cf43592cb2275ee018a1c7fa3855af25db0fcf7ef3f6baa4cd12cbb9bbb647e10f47e196d852f0f1202352d519538aaae29e97faac82
7
- data.tar.gz: 97f70f2d965886002a3a8e35707e7cd685b76d0eb7b6ac5378975c1357b4aa8106d9c8576aee6821554db0e014941d365e590ca58c7523c692b29c52137e5144
6
+ metadata.gz: 8454e97989f4ac081dc30a4f44658e614c217bec5bf5fc2d08a545cfee41ee5f04d8e32f526fd50ad62a8b45383a490dc4d15e413bae2b441d374cb2f56e2931
7
+ data.tar.gz: f9ccebd0c19f711097539050c9b57dc89529d32d76cf235c15ac2c12e8e1ac93a7acc6919b2a1cb26c00ce55dcf309deb35df44e8009d67ebbd73b9900259e24
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.8.0 (March 30, 2014)
2
+ * Adds both callbacks for undo and redo.
3
+
1
4
  # 0.7.0 (February 8, 2014)
2
5
  * Simpler tests.
3
6
 
data/README.md CHANGED
@@ -64,6 +64,23 @@ document.redo
64
64
  document.persisted? #=> false
65
65
  ```
66
66
 
67
+ ### Callbacks
68
+
69
+ Mongoid::Undo defines two callbacks which are called before and after `undo`, respectively `redo`. Both are based on `ActiveModel::Callbacks`which means they behave like the allready known Rails callbacks.
70
+
71
+ ```ruby
72
+ class Document
73
+ include Mongoid::Document
74
+ include Mongoid::Undo
75
+
76
+ before_undo do
77
+ # Do something fancy.
78
+ end
79
+
80
+ before_redo { false } # Don't allow redoing.
81
+ ```
82
+
83
+
67
84
  ## Installation
68
85
 
69
86
  In your Gemfile:
@@ -80,6 +97,7 @@ gem 'mongoid-undo'
80
97
  4. Push to the branch (`git push origin my-new-feature`)
81
98
  5. Create new Pull Request
82
99
 
100
+
83
101
  ## Copyright
84
102
 
85
103
  (The MIT license)
data/lib/mongoid/undo.rb CHANGED
@@ -7,6 +7,7 @@ module Mongoid
7
7
 
8
8
  include Mongoid::Paranoia
9
9
  include Mongoid::Versioning
10
+ include Mongoid::Callbacks
10
11
 
11
12
  included do
12
13
  field :action, type: Symbol, versioned: false
@@ -18,14 +19,18 @@ module Mongoid
18
19
  reload
19
20
  end
20
21
  end
22
+
23
+ define_model_callbacks :undo, :redo
21
24
  end
22
25
 
23
26
  def undo
24
- case action
25
- when :create, :destroy
26
- deleted_at.present? ? restore : delete
27
- when :update
28
- retrieve
27
+ run_callbacks __callee__ do
28
+ case action
29
+ when :create, :destroy
30
+ deleted_at.present? ? restore : delete
31
+ when :update
32
+ retrieve
33
+ end
29
34
  end
30
35
  end
31
36
  alias_method :redo, :undo
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Undo
3
- VERSION = '0.9.1'
3
+ VERSION = '0.10.0'
4
4
  end
5
5
  end
data/test/test_helper.rb CHANGED
@@ -6,7 +6,29 @@ require 'mongoid/undo'
6
6
 
7
7
  Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
8
8
 
9
- class Minitest::Test
9
+ class MiniTest::Unit::TestCase
10
10
  alias_method :assert_not, :refute
11
11
  alias_method :assert_not_equal, :refute_equal
12
+
13
+ # Copied from activesupport/lib/active_support/testing/assertions.rb
14
+ def assert_difference(expression, difference = 1, message = nil, &block)
15
+ expressions = Array(expression)
16
+
17
+ exps = expressions.map { |e|
18
+ e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
19
+ }
20
+ before = exps.map { |e| e.call }
21
+
22
+ yield
23
+
24
+ expressions.zip(exps).each_with_index do |(code, e), i|
25
+ error = "#{code.inspect} didn't change by #{difference}"
26
+ error = "#{message}.\n#{error}" if message
27
+ assert_equal(before[i] + difference, e.call, error)
28
+ end
29
+ end
30
+
31
+ def assert_no_difference(expression, message = nil, &block)
32
+ assert_difference expression, 0, message, &block
33
+ end
12
34
  end
data/test/undo_test.rb CHANGED
@@ -110,4 +110,47 @@ class UndoTest < Minitest::Unit::TestCase
110
110
  document.redo
111
111
  assert_not_equal updated_at, document.updated_at
112
112
  end
113
+
114
+
115
+ def test_callbacks
116
+ document = Document.create(name: 'foo')
117
+ mock = MiniTest::Mock.new
118
+
119
+ document.class.before_undo { mock.before_undo }
120
+ document.class.after_undo { mock.after_undo }
121
+
122
+ mock.expect :before_undo, nil
123
+ mock.expect :after_undo, nil
124
+
125
+ document.undo
126
+ mock.verify
127
+
128
+ document.class.before_redo { mock.before_redo }
129
+ document.class.after_redo { mock.after_redo }
130
+
131
+ mock.expect :before_redo, nil
132
+ mock.expect :after_redo, nil
133
+
134
+ document.redo
135
+ mock.verify
136
+ end
137
+
138
+
139
+ def test_disabling_undo_via_callbacks
140
+ document = Document.create(name: 'foo')
141
+ document.destroy
142
+
143
+ # Disable undoing
144
+ document.class.before_undo proc { false }
145
+
146
+ assert_no_difference 'Document.count' do
147
+ document.undo
148
+ end
149
+ end
150
+
151
+
152
+ def teardown
153
+ Document.reset_callbacks :undo
154
+ Document.reset_callbacks :redo
155
+ end
113
156
  end
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.9.1
4
+ version: 0.10.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-02-17 00:00:00.000000000 Z
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport